hoj1460 heavy cargo

/*This Code is Submitted by billforum for Problem 1460 at 2012-02-15 17:20:22*/
#include <iostream>
#include <stdio.h>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;

const int N=205;
const int Max=20000;
int n,ns;
int a[N],d[N][N],total;
bool f[N];

int min(int x,int y)
{
    return(x<y?x:y);
}
void path()
{
    int k;
    f[ns]=1;
    k=ns;
   // total=0;
    for(int i=2;i<=n;i++)
    {
        int mcost=-1;
        for(int j=1;j<=n;j++)
        {
            if(!f[j]&&(a[j]>mcost))
            {
                mcost=a[j];
                k=j;
            }
        }
        f[k]=1;
       // total+=a[k];
        for(int t=1;t<=n;t++)
        {
            if(!f[t]&&(min(d[k][t],a[k])>a[t]))
            a[t]=min(d[k][t],a[k]);
        }
    }
    return;
}

int main()
{

    int m,weight,id=0,test=0;
    string tmp,t1,t2,st,de;
    map<string,int> name;
   // map<string,int>::iterator iter;
   while(cin>>n>>m)
   {
       if(n==0&&m==0) break;
       id=0;
       test++;
       map<string,int> name;
      for(int i=1;i<=n;i++)
      {
          f[i]=0;
          a[i]=-1;
          for(int j=i+1;j<=n;j++)
          {
              d[i][j]=-1;
              d[j][i]=-1;
          }
      }
      for(int i=1;i<=m;i++)
      {
          int f,s;
         cin>>t1>>t2>>weight;
         if(name.find(t1)==name.end())
         {
             id++;
             name.insert(pair<string,int>(t1,id));
         }
         if(name.find(t2)==name.end())
         {
             id++;
             name.insert(pair<string,int>(t2,id));
         }

         f=name.find(t1)->second;
         s=name.find(t2)->second;
         d[f][s]=weight;
         d[s][f]=weight;
      }
      cin>>st>>de;
      ns=name.find(st)->second;
      int nd=name.find(de)->second;

      for(int i=1;i<=n;i++)
      a[i]=d[ns][i];
      a[ns]=0;
      path();
      cout<<"Scenario #"<<test<<endl;
      cout<<a[nd]<<" tons"<<endl<<endl;
   }
    return 0;
}

 

posted @ 2012-02-15 17:22  wuzhibin  阅读(208)  评论(0)    收藏  举报