pat 1087. All Roads Lead to Rome (30)

来源:互联网 发布:关于中医的软件 编辑:程序博客网 时间:2024/05/18 00:38

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format "City1 City2 Cost". Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".

Sample Input:
6 7 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1
Sample Output:
3 3 195 97HZH->PRS->ROM

Dijkstra算法。 比较麻烦的的平均快乐值的最大值,其实可以转化成当快乐值相等时,经过的城市数量越少越好。
#include <iostream>#include <map>#include <algorithm>#include<string.h>using namespace std;#include<stdio.h>#include<stack>#define maxn 0x7fffffffint n,m,mat[205][205],num[205];int dist[205],val[205],vis[205],dis[205],path[205];int cnt[205],h[205];map<string,int>v;map<int,string>v1;void dijkstra(int s){   int i,j;   for(i=0;i<n;i++)   {       dist[i]=mat[s][i];       dis[i]=0;       vis[i]=0;       cnt[i]=maxn;       num[i]=0;       if(dist[i]!=maxn)         path[i]=s;       else path[i]=-1;   }   dist[s]=val[i];   num[s]=1;   cnt[s]=1;   for(i=0;i<n;i++)   {       int k=s,u=maxn;       for(j=0;j<n;j++)       {           if(!vis[j]&&dist[j]<u)           {               u=dist[j];               k=j;           }       }       vis[k]=1;       for(j=0;j<n;j++)       {           if(!vis[j]&&mat[k][j]!=maxn)           {               if(dist[j]>dist[k]+mat[k][j])               {                   dist[j]=dist[k]+mat[k][j];                   dis[j]=dis[k]+val[j];//计算快乐值                   cnt[j]=cnt[k]+1;//计算经过的城市数目                   num[j]=num[k];//计算最短路径的条数                   path[j]=k;//记录路径               }               else if(dist[j]==dist[k]+mat[k][j])               {   num[j]=num[j]+num[k];                     if(dis[j]<dis[k]+val[j])                    { dis[j]=dis[k]+val[j];                       cnt[j]=cnt[k]+1;                         path[j]=k;                    }                   else if(dis[j]==dis[k]+val[j])                    {                       if(cnt[j]>cnt[k]+1)                       {                           cnt[j]=cnt[k]+1;                           path[j]=k;                       }                    }               }           }       }   }}void print(int s,int t){    stack<int>q;    int j=0,i;    while(t!=s)    {        q.push(t);        t=path[t];    }    q.push(t);    while(!q.empty())    {        h[j++]=q.top();        q.pop();    }    cout<<v1[h[0]];    for(i=1;i<j;i++)        cout<<"->"<<v1[h[i]];    cout<<endl;}int main(){  int i,j,d;  string s1,s2,s3,s4,s5="ROM";  cin>>n>>m>>s1;  v[s1]=0;  v1[0]=s1;  val[0]=0;  for(i=1;i<n;i++)  {      cin>>s2>>val[i];      v[s2]=i;      v1[i]=s2;  }  for(i=0;i<205;i++)    for(j=0;j<205;j++)       mat[i][j]=maxn;  for(i=0;i<m;i++)  {      cin>>s3>>s4>>d;      if(mat[v[s3]][v[s4]]>d)          mat[v[s3]][v[s4]]=mat[v[s4]][v[s3]]=d;  }  dijkstra(v[s1]);  cout<<num[v[s5]]<<" "<<dist[v[s5]]<<" "<<dis[v[s5]]<<" "<<dis[v[s5]]/(cnt[v[s5]]-1)<<endl;  print(v[s1],v[s5]);  return 0;}


0 0