[PAT] 1087. All Roads Lead to Rome (30)
/*************************************** * *********************** 1087. all Roads Lead to Rome (30) Time Limit 200 ms memory limit 65536 kB code length limit 16000 B discriminant program Standard author CHEN, YueIndeed there are unsupported 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 paween IRS 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, doesn't 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 ****************************** * *********************************/# include
# Include
# Include
# Define INF 1 <30 using namespace std; int mat [210] [210]; int dist [210]; bool mark [210]; // record the happy value int happiness [210]; // record the total happy value int sumhap [210]; // record the average happy value int avg [210]; int rout [210]; // record the number of Shortest Paths int difr [210]; // record path length int ct [210]; string cities [210]; map
M; int num =-1; int n; int ston (string s) {if (m. find (s )! = M. end () return m [s]; else {num ++; m [s] = num; return num ;}} void init () {for (int I = 0; I
Dist [p] + mat [p] [j]) {dist [j] = dist [p] + mat [p] [j]; rout [j] = p; sumhap [j] = sumhap [p] + happiness [j]; ct [j] = ct [p] + 1; avg [j] = sumhap [j]/ct [j]; difr [j] = difr [p];} else if (dist [j] = dist [p] + mat [p] [j]) {difr [j] + = difr [p]; if (sumhap [j]
";}Int main () {int k; string scity; string city; int hp; string sa, sb; int ia, ib, cost; while (cin> n> k> scity) {init (); num = ston (scity); cities [num] = scity; for (int I = 0; I
> City> hp; num = ston (city); happiness [num] = hp; cities [num] = city;} for (int I = 0; I
> Sa> sb> cost; ia = ston (sa); ib = ston (sb ); mat [ia] [ib] = mat [ib] [ia] = cost;} dist [0] = 0; difr [0] = 1; dijk (); ib = ston ("ROM"); cout <