POJ2263-Heavy Cargo

http://poj.org/problem?id=2263

莫非当初随便百度了个

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<map>
#define N 202
using namespace std;
int gra[N][N],dis[N],vis[N],n;
void dijkstra(int beg,int end)
{
     int i,j,k,temp,m;
     for(i=1;i<=n;i++)
     {
         dis[i]=gra[beg][i];
         vis[i]=false;
     }
     vis[beg]=true;
     for(i=2;i<=n;i++)
     {
         k=0;
         temp=0;
         for(j=1;j<=n;j++)
         {
             if(!vis[j]&&dis[j]>temp)
             {
                 temp=dis[j];
                 k=j;
             }
         }
         if(k==end)
         {
             printf("%d tons\n",temp);
             return;
         }
         vis[k]=true;
         for(j=1;j<=n;j++)
         {
             if(!vis[j])
             {
                 m=min(temp,gra[k][j]);
                 if(m>dis[j])
                     dis[j]=m;
             }
         }
     }
}
int main(void)
{
     int i,j,k,m,h,count=1;
     string s1,s2;
     map<string,int>mp;
     while(scanf("%d%d",&n,&m),n||m)
     {
         k=1;
         for(i=1;i<=n;i++)
             for(j=1;j<=n;j++)
                 gra[i][j]=0;
         while(m--)
         {
             cin>>s1>>s2>>h;
             if(mp.find(s1)==mp.end())
                 mp[s1]=k++;
             if(mp.find(s2)==mp.end())
                 mp[s2]=k++;
             i=mp[s1];
             j=mp[s2];
             if(h>gra[i][j])
                 gra[i][j]=gra[j][i]=h;
         }
         cin>>s1>>s2;
         printf("Scenario #%d\n",count++);
         dijkstra(mp[s1],mp[s2]);
         printf("\n");
     }
     return 0;
}
posted @ 2012-08-30 19:39  Yogurt Shen  阅读(121)  评论(0编辑  收藏  举报