【PAT 1002 All Roads Lead to Rome】+ dfs + dijkstra
来源:互联网 发布:ucpaas java短信发送 编辑:程序博客网 时间:2024/06/16 15:55
All Roads Lead to Rome (30)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
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.
输入描述:
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.
输出描述:
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”.
输入例子:
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
输出例子:
3 3 195 97
HZH->PRS->ROM
题意 : 找出 s 到 ROM 的最短路,最短路相同时, 选幸福值和大的路径,最短路径和幸福值都相同时,选平均幸福值大的,
输出 和最短路径相同的路径数,最短路径,最大幸福值,平均幸福值
思路 : dijkstra 更新到 ROM 的所有最优路径,dfs 按照 最短路径,最大幸福值,平均幸福值 的优选级更新最优解
AC代码:
#include<cstdio>#include<map>#include<vector>#include<cmath>#include<queue>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAX = 210;const int INF = 0x3f3f3f3f / 2;typedef long long LL;typedef pair <int,int> p;int d[MAX],vis[MAX],xf[MAX],n,m,c,ans,sum,cut,dis[MAX][MAX];struct node{ int t,vl;};map <string,int> ma;map <int,int> mm;vector <int> vv[MAX],vl,vll;vector <node> v[MAX];string s,ss[MAX],a,b;void dj(){ priority_queue<p, vector <p> ,greater<p> > q; fill(d,d + n,INF); d[ma["ROM"]] = 0; q.push(p(0,ma["ROM"])); while(!q.empty()){ p w = q.top(); q.pop(); int u = w.second; if(d[u] < w.first) continue; for(int i = 0; i < v[u].size(); i++){ node z = v[u][i]; if(d[z.t] > d[u] + z.vl){ d[z.t] = d[u] + z.vl; vv[z.t].clear(); vv[z.t].push_back(u); q.push(p(d[z.t],z.t)); } else if(d[z.t] == d[u] + z.vl) vv[z.t].push_back(u); } }}void dfs(int x){ vll.push_back(x); if(x == ma["ROM"]){ int h = 0,l = 0; for(int i = 1; i < vll.size(); i++) h += xf[vll[i]],l += dis[vll[i]][vll[i - 1]]; mm[l]++; if(l < cut) cut = l,ans = h,sum = h / (vll.size() - 1),vl = vll; else if(l == cut && h > ans) ans = h,sum = h / (vll.size() - 1),vl = vll; else if(l == cut && h == ans && sum < h / (vll.size() -1)) sum = h / (vll.size() - 1),vl = vll; return ; } for(int i = 0; i < vv[x].size(); i++) dfs(vv[x][i]),vll.pop_back();}int main(){ cin >> n >> m >> s; ma[s] = 0; ss[0] = s; for(int i = 1; i < n; i++) cin >> ss[i] >> xf[i] ,ma[ss[i]] = i; while(m--){ cin >> a >> b >> c; int x = ma[a],y = ma[b]; dis[x][y] = dis[y][x] = c; node o; o.t = y,o.vl = c; v[x].push_back(o); o.t = x; v[y].push_back(o); } dj(); ans = -INF,sum = -INF,cut = INF; vll.clear(); mm.clear(); dfs(ma[s]); printf("%d %d %d %d\n",mm[cut],cut,ans,sum); for(int i = 0; i < vl.size() - 1; i++) cout << ss[vl[i]] << "->"; puts("ROM"); return 0;}
- 【PAT 1002 All Roads Lead to Rome】+ dfs + dijkstra
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 条条大路通罗马 Dijkstra+DFS
- 1087. All Roads Lead to Rome (30)-PAT甲级真题-Dijkstra + DFS
- PAT 1087. All Roads Lead to Rome
- 【PAT】1087. All Roads Lead to Rome
- PAT-All Roads Lead to Rome
- PAT练习(2)-1002 All Roads Lead to Rome
- 1002 All Roads Lead to Rome
- 1087. All Roads Lead to Rome (30)[dijkstra/dfs回溯剪枝]
- 1087. All Roads Lead to Rome (30) -- Dijkstra经典
- 1087. All Roads Lead to Rome (30) <Dijkstra优先队列>
- pat 1087. All Roads Lead to Rome (30)
- PAT 1087. All Roads Lead to Rome (30)
- PAT A 1087. All Roads Lead to Rome (30)
- [PAT]1087. All Roads Lead to Rome (30)
- Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)
- PAT 1087. All Roads Lead to Rome (30)
- 【PAT】1087. All Roads Lead to Rome (30)
- 树和树的表示
- Android Launcher3 禁止用户拖动图标创建文件夹,控制拖动图标到另一个图标时的动画效果
- HDU-1869-六度分离(Floyd算法)
- Kotlin webview的坑
- 存储过程transaction事务
- 【PAT 1002 All Roads Lead to Rome】+ dfs + dijkstra
- springMVC的环境搭建
- Oralce数据库自动启动shell脚本
- root cause org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "XXX")
- mysql数据库进行连接查询时在join on 后的and 和where 区别
- Bitmap和Base64之间的转换以及Base64 encode
- 软件测试人员易遗漏的一些隐藏缺陷
- 实习笔记之个人设置界面
- 搬家了-博客园