PAT_A1087#All Roads Lead to Rome

Source:

PAT A1087 All Roads Lead to Rome (30 分)

Description:

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), 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 N1 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 Klines 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 recommanded. 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 recommanded 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 1

Sample Output:

3 3 195 97
HZH->PRS->ROM

Keys:

Attention:

  • 结点编号从1开始,因为映射判定的条件是结点编号==0

Code:

  1 /*
  2 Data: 2019-08-27 19:12:20
  3 Problem: PAT_A1087#All Roads Lead to Rome
  4 AC: 37:38
  5 
  6 题目大意:
  7 从起点至终点ROM花费最少且最幸福的一条路,
  8 若不唯一,给出平均幸福指数最高的路径
  9 
 10 输入:
 11 第一行给出,城市数N,路径数K,起点城市
 12 接下来N-1行,城市名,幸福指数
 13 接下来K行,v1,v2,cost
 14 输出:
 15 最短路径数目,花费,幸福指数,平均幸福指数(整数部分,除去起点)
 16 起点至终点的路径
 17 */
 18 #include<cstdio>
 19 #include<string>
 20 #include<vector>
 21 #include<map>
 22 #include<iostream>
 23 #include<algorithm>
 24 using namespace std;
 25 const int M=1e3,INF=1e9;
 26 int n,m,v1,v2,t,grap[M][M],d[M],w[M],vis[M],pt=1,optH=0,cnt=0;
 27 vector<int> pre[M],path,optPath;
 28 map<string,int> mp;
 29 string city[M];
 30 
 31 int ToInt(string s)
 32 {
 33     if(mp[s] == 0)
 34     {
 35         city[pt]=s;
 36         mp[s]=pt++;
 37     }
 38     return mp[s];
 39 }
 40 
 41 void Dijskra(int s)
 42 {
 43     fill(vis,vis+M,0);
 44     fill(d,d+M,INF);
 45     d[s]=0;
 46     for(int i=1; i<=n; i++)
 47     {
 48         int u=-1,Min=INF;
 49         for(int j=1; j<=n; j++)
 50         {
 51             if(vis[j]==0 && d[j]<Min)
 52             {
 53                 u = j;
 54                 Min=d[j];
 55             }
 56         }
 57         if(u==-1)
 58             break;
 59         vis[u]=1;
 60         for(int v=1; v<=n; v++)
 61         {
 62             if(vis[v]==0 && grap[u][v]!=INF)
 63             {
 64                 if(d[u]+grap[u][v]<d[v])
 65                 {
 66                     pre[v].clear();
 67                     pre[v].push_back(u);
 68                     d[v] = d[u]+grap[u][v];
 69                 }
 70                 else if(d[u]+grap[u][v]==d[v])
 71                     pre[v].push_back(u);
 72             }
 73         }
 74     }
 75 }
 76 
 77 void DFS(int v)
 78 {
 79     if(v == 1)
 80     {
 81         cnt++;
 82         int happy=0;
 83         for(int i=0; i<path.size(); i++)
 84             happy+=w[path[i]];
 85         if(happy > optH)
 86         {
 87             optH = happy;
 88             optPath = path;
 89         }
 90         else if(happy==optH && path.size()<optPath.size())
 91             optPath = path;
 92         return;
 93     }
 94     path.push_back(v);
 95     for(int i=0; i<pre[v].size(); i++)
 96         DFS(pre[v][i]);
 97     path.pop_back();
 98 }
 99 
100 int main()
101 {
102 #ifdef ONLINE_JUDGE
103 #else
104     freopen("Test.txt", "r", stdin);
105 #endif // ONLINE_JUDGE
106 
107     string s1,s2;
108     cin >> n >> m >> s1;
109     ToInt(s1);
110     for(int i=1; i<n; i++)
111     {
112         cin >> s1;
113         v1 = ToInt(s1);
114         scanf("%d", &w[v1]);
115         if(s1 == "ROM")
116             t = v1;
117     }
118     fill(grap[0],grap[0]+M*M,INF);
119     for(int i=0; i<m; i++)
120     {
121         cin >> s1 >> s2;
122         v1 = ToInt(s1);
123         v2 = ToInt(s2);
124         scanf("%d", &grap[v1][v2]);
125         grap[v2][v1]=grap[v1][v2];
126     }
127     Dijskra(1);
128     DFS(t);
129     printf("%d %d %d %d\n", cnt,d[t],optH,optH/optPath.size());
130     printf("%s", city[1].c_str());
131     for(int i=optPath.size()-1; i>=0; i--)
132         printf("->%s", city[optPath[i]].c_str());
133 
134     return 0;
135 }

 

posted @ 2019-06-18 15:52  林東雨  阅读(339)  评论(0编辑  收藏  举报