PAT_A1087#All Roads Lead to Rome

博客围绕PAT A1087题目展开,要为客户找到从所在城市到罗马成本最低且幸福值最高的旅游路线。介绍了输入输出规格,指出关键在于最短路径,还给出注意事项,代码转载自其他博客。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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-06-18 15:47:57
  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<vector>
 20 #include<string>
 21 #include<iostream>
 22 #include<map>
 23 #include<algorithm>
 24 using namespace std;
 25 const int M=210,INF=1e9;
 26 int grap[M][M],vis[M],d[M],w[M];
 27 int n,s,t,pt=1,cnt=0,optValue=0,optAvg=0;
 28 map<string,int> mp;
 29 string city[M],ROM="ROM";
 30 vector<int> pre[M],temp,opt;
 31 
 32 int Str(string s)
 33 {
 34     if(mp[s]==0)
 35     {
 36         mp[s]=pt;
 37         return pt++;
 38     }
 39     else
 40         return mp[s];
 41 }
 42 
 43 void Dijskra(int st)
 44 {
 45     fill(d,d+M,INF);
 46     fill(vis,vis+M,0);
 47     d[st]=0;
 48     for(int i=1; i<=n; i++)
 49     {
 50         int u=-1,Min=INF;
 51         for(int j=1; j<=n; j++)
 52         {
 53             if(vis[j]==0 && d[j]<Min)
 54             {
 55                 u = j;
 56                 Min = d[j];
 57             }
 58         }
 59         if(u==-1)   return;
 60         vis[u]=1;
 61         for(int v=1; v<=n; v++)
 62         {
 63             if(vis[v]==0 && grap[u][v]!=INF)
 64             {
 65                 if(d[u]+grap[u][v] < d[v])
 66                 {
 67                     d[v] = d[u] + grap[u][v];
 68                     pre[v].clear();
 69                     pre[v].push_back(u);
 70                 }
 71                 else if(d[u]+grap[u][v] == d[v])
 72                     pre[v].push_back(u);
 73             }
 74         }
 75     }
 76 }
 77 
 78 void DFS(int v)
 79 {
 80     if(v == s)
 81     {
 82         cnt++;
 83         int value=0;
 84         for(int i=temp.size()-1; i>=0; i--)
 85             value += w[temp[i]];
 86         if(value > optValue)
 87         {
 88             optValue = value;
 89             optAvg = value/temp.size();
 90             opt = temp;
 91         }
 92         else if(value == optValue)
 93         {
 94             if(value/temp.size() > optAvg)
 95             {
 96                 optAvg = value/temp.size();
 97                 opt = temp;
 98             }
 99         }
100         return;
101     }
102 
103     temp.push_back(v);
104     for(int i=0; i<pre[v].size(); i++)
105         DFS(pre[v][i]);
106     temp.pop_back();
107 }
108 
109 int main()
110 {
111 #ifdef ONLINE_JUDGE
112 #else
113     freopen("Test.txt", "r", stdin);
114 #endif // ONLINE_JUDGE
115 
116     fill(grap[0],grap[0]+M*M,INF);
117     fill(w,w+M,0);
118 
119     string name;
120     int v1,v2,m;
121     scanf("%d%d", &n,&m);
122     cin >> name;
123     s = Str(name);
124     city[s]=name;
125     for(int i=1; i<n; i++)
126     {
127         cin >> name;
128         v1 = Str(name);
129         city[v1]=name;
130         if(name==ROM)
131             t = v1;
132         scanf("%d", &w[v1]);
133     }
134     for(int i=0; i<m; i++)
135     {
136         cin >> name;
137         v1 = Str(name);
138         cin >> name;
139         v2 = Str(name);
140         scanf("%d", &grap[v1][v2]);
141         grap[v2][v1]=grap[v1][v2];
142     }
143     Dijskra(s);
144     DFS(t);
145     printf("%d %d %d %d\n", cnt,d[t],optValue,optAvg);
146     cout << city[s];
147     for(int i=opt.size()-1; i>=0; i--)
148         cout << "->" << city[opt[i]];
149 
150     return 0;
151 }

 

转载于:https://www.cnblogs.com/blue-lin/p/11045608.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值