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 HZH ROM 100 PKN 40 GDN 55 PRS 95 BLN 80 ROM GDN 1 BLN ROM 1 HZH PKN 1 PRS ROM 2 BLN HZH 2 PKN GDN 1 HZH PRS 1Sample Output:
3 3 195 97 HZH->PRS->ROM
1 #include<stdio.h> 2 #include<map> 3 #include<string> 4 #include<string.h> 5 #include<stack> 6 using namespace std; 7 #define MAX 210 8 int INF = 1000000; 9 int HappyVal[210]; 10 int visit[MAX]; 11 int Grap[MAX][MAX]; 12 int d[MAX]; 13 int h[MAX]; 14 int num[MAX]; 15 int pre[MAX]; 16 int Count[MAX]; 17 18 void Dijkstra(int Begin,int NodeNum) 19 { 20 d[Begin] = 0; 21 h[Begin] = HappyVal[Begin]; 22 num[Begin] = 1; 23 Count[Begin] = 0; 24 for(int i = 0;i < NodeNum ;i++) 25 { 26 int index = -1; 27 int MIN = INF; 28 for(int j = 0 ;j <NodeNum ;j++) 29 { 30 if(!visit[j] && d[j] < MIN) 31 { 32 index = j; 33 MIN = d[j]; 34 } 35 } 36 37 if(index == -1) return ; 38 visit[index] = true; 39 for(int v = 0 ;v <NodeNum ;v++) 40 { 41 if(!visit[v] && Grap[index][v]!=INF) 42 { 43 if(d[index]+Grap[index][v]<d[v]) 44 { 45 d[v] = d[index]+Grap[index][v]; 46 num[v] = num[index]; 47 h[v] = h[index] + HappyVal[v]; 48 pre[v] = index; 49 Count[v] = Count[index] +1; 50 } 51 else if(d[index]+Grap[index][v]==d[v]) 52 { 53 num[v] = num[v] + num[index]; 54 55 if(h[v] < h[index] + HappyVal[v]) 56 { 57 h[v] = h[index] + HappyVal[v]; 58 Count[v] = Count[index] +1; 59 pre[v] = index; 60 } 61 else if( h[v] == h[index] + HappyVal[v] && (double)(h[index] + HappyVal[v])/(Count[index]+1) > (double)h[v]/Count[v]) 62 { 63 Count[v] = Count[index] +1; 64 pre[v] = index; 65 } 66 } 67 } 68 } 69 } 70 71 } 72 73 int main() 74 { 75 int i,j,N,K,happy,ROM; 76 char Begin[4],tem[4]; 77 scanf("%d%d%s",&N,&K,Begin); 78 map<string,int> mm; 79 map<int,string> mm2; 80 mm[Begin] = 0; 81 mm2[0] = Begin ; 82 HappyVal[mm[Begin]] = 0; 83 for(i = 1 ; i < N ;i++) 84 { 85 scanf("%s%d",tem,&happy); 86 if(strcmp("ROM",tem)==0) ROM = i; 87 mm[tem] = i; 88 mm2[i] = tem; 89 HappyVal[i] = happy; 90 } 91 92 char x[4],y[4]; 93 94 for(i = 0 ; i < N ;i++) 95 { 96 for(j = 0 ; j < N ;j++) 97 { 98 Grap[i][j] = INF; 99 } 100 d[i] = h[i] = INF; 101 pre[i] = -1; 102 Count[i] = 0; 103 } 104 105 for(i = 0 ; i < K ;i++) 106 { 107 scanf("%s%s",x,y); 108 scanf("%d",&Grap[mm[x]][mm[y]]); 109 Grap[mm[y]][mm[x]] = Grap[mm[x]][mm[y]]; 110 } 111 112 Dijkstra( mm[Begin] , N); 113 114 printf("%d %d %d %d ",num[mm["ROM"]],d[mm["ROM"]],h[mm["ROM"]],h[mm["ROM"]]/Count[mm["ROM"]]); 115 116 stack<int> ss; 117 i= mm["ROM"]; 118 while(i != -1) 119 { 120 ss.push(i); 121 i = pre[i]; 122 } 123 int fir = 1; 124 while(!ss.empty()) 125 { 126 if(fir == 1) 127 { 128 fir = 0; 129 printf("%s",mm2[ss.top()].c_str()); 130 } 131 else printf("->%s",mm2[ss.top()].c_str()); 132 ss.pop(); 133 } 134 135 printf(" "); 136 137 return 0; 138 }