1087. All Roads Lead to Rome (30)

来源:互联网 发布:mac口红如何辨别真假 编辑:程序博客网 时间:2024/05/22 04:49

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
#include <iostream>#include <string>#include <map>#include <vector>using namespace std;struct edge{int u,d;edge(int _u,int _d):u(_u),d(_d){}};const int maxn = 205;const int INF= 999999999;vector<edge> G[maxn];int dist[maxn],pathcnt[maxn],citycnt[maxn],happycnt[maxn],happy[maxn],path[maxn],vis[maxn];map<string,int> str2int;map<int,string> int2str;int n,m,id = 0,kase=0;int s2i(const string & s){auto itr = str2int.find(s);if(itr == str2int.end()){int t = id++;int2str[t] = s;return str2int[s] = t;}return str2int[s];}string i2s(int x){return int2str[x];}void print_path(int x){if(x != -1){print_path(path[x]);if(kase++)cout<<"->";cout<<i2s(x);}}void dijkstra(int r){for(int i=0;i<n;i++){dist[i] = citycnt[i] = INF; pathcnt[i] = 1;path[i] = -1;happycnt[i] = -1;vis[i] = 0;}dist[r] = 0;happycnt[r] = 0;citycnt[r] = 0;for(int j=0;j<n;j++){int idx = -1,mind = INF;for(int i=0;i<n;i++)if(vis[i] == 0 && mind > dist[i])mind = dist[idx = i];vis[idx] = 1;for(int i=0;i<G[idx].size();i++){int v = G[idx][i].u,d = G[idx][i].d;if(dist[v] > dist[idx] + d){dist[v] = dist[idx] + d;pathcnt[v] = pathcnt[idx];citycnt[v] = citycnt[idx] + 1;happycnt[v] = happycnt[idx] + happy[v];path[v] = idx;}else if(dist[v] == dist[idx] + d){pathcnt[v] = pathcnt[v] + pathcnt[idx];if(happycnt[v] < happycnt[idx] + happy[v]){happycnt[v] = happycnt[idx] + happy[v];citycnt[v] = citycnt[idx] + 1;path[v] = idx;}else if(happycnt[v] == happycnt[idx] + happy[v] && citycnt[v] > citycnt[idx] + 1){citycnt[v] = citycnt[idx] + 1;path[v] = idx;}}}}}int main(){string s,c1,c2;int d,a,b,t;cin>>n>>m>>s;t = s2i(s);happy[t] = 0;for(int i=0;i<n-1;i++){cin>>c1>>d;a = s2i(c1);happy[a] = d;}for(int i=0;i<m;i++){cin>>c1>>c2>>d;a = s2i(c1);b = s2i(c2);G[a].push_back(edge(b,d));G[b].push_back(edge(a,d));}int e = s2i("ROM");dijkstra(t);cout<<pathcnt[e]<<" "<<dist[e]<<" "<<happycnt[e]<<" "<<happycnt[e]/citycnt[e]<<endl;print_path(e);return 0;}


0 0
原创粉丝点击