1087. All Roads Lead to Rome (30)

来源:互联网 发布:通达信原油看盘软件 编辑:程序博客网 时间:2024/06/06 04:20

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<queue>#include<stack>#include<map>#include<vector>#include<cstdio>using namespace std;map<string ,int > str_int;map<int,string> int_str;int map_len[205][205];const int INF =1<<29;int vis[205];int sum_happy[205];int length[205];int happy[205];int route[205];int path[205];typedef pair <int ,int > P;int n;void dij(int s){    for (int i=0;i<n;i++)    {        vis[i]=0;        length[i]=INF;        sum_happy[i]=0;    }    length[s]=0;    route[s]=1;    priority_queue < P, vector <P>, greater <P> > que;    que.push({length[s],s});    while(que.size())    {        P p=que.top();        que.pop();        int num=p.second;        if (vis[num]==1) continue;        vis[num]=1;        for (int i=0;i<n;i++)        {            if (map_len[num][i]!=INF)            {                if (length[i]>length[num]+map_len[num][i])                {                    route[i]=route[num];                    length[i]=length[num]+map_len[num][i];                    sum_happy[i]=sum_happy[num]+happy[i];                    path[i]=num;                    que.push({length[i],i});                }                else if (length[i]==length[num]+map_len[num][i])                {                    route[i]+=route[num];                    if(sum_happy[i]<sum_happy[num]+happy[i])                    {                        sum_happy[i]=sum_happy[num]+happy[i];                        path[i]=num;                    }                    que.push({length[i],i});                }            }        }    }}int   main(){    string start,city_name;    int happiness,k;    cin>>n>>k>>start;    str_int[start]=0;    int_str[0]=start;    happy[0]=0;    for (int i=1;i<n;i++)    {        cin>>city_name>>happiness;        str_int[city_name]=i;        int_str[i]=city_name;        happy[i]=happiness;    }    for(int i=0;i<n;i++)        for (int j=0;j<n;j++)            map_len[i][j]=INF;    for (int i=0;i<k;i++)    {        string city1,city2;        int dis;        cin>>city1>>city2>>dis;        map_len[str_int[city1]][str_int[city2]]=dis;        map_len[str_int[city2]][str_int[city1]]=dis;    }    dij(0);    int end1=str_int["ROM"];    int fz=end1;    int cnt=1;    stack<string> sta;    while(path[fz]!=0)    {        sta.push(int_str[fz]);        fz=path[fz];        cnt++;    }    sta.push(int_str[fz]);    cout<<route[end1]<<" "<<length[end1]<<" "<<sum_happy[end1];    printf(" %d\n",sum_happy[end1]/cnt);    cout<<start;    while(sta.size())    {        cout<<"->"<<sta.top();        sta.pop();    }    return 0;}