【Dijstra+平均点权和最大+路径】1087 All Roads Lead to Rome (30 分)
2021/9/30 6:12:51
本文主要是介绍【Dijstra+平均点权和最大+路径】1087 All Roads Lead to Rome (30 分),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1087 All Roads Lead to Rome (30 分)
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 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 结尾无空行
Code
//with the least cost while gaining the most happiness. /** N :2~200 number of cities K : the total number of routes between pairs of cities start: name of the starting city. N - 1 lines: happiness K lines : City1 City2 Cost. **/ #include <iostream> #include <cstring> #include <map> #include <vector> #define Inf 10000000 #define Max 400 using namespace std; int n, k; int G[Max][Max]; int d[Max]; int happy[Max]; int h[Max];//到v为止的最大幸福指数 vector<vector<int>> pre;//存储前驱节点 int num[Max];//最短路径条数 bool vis[Max] = {false}; map<string, int> str2int;//存储字符的下标. map<int, string> int2str;//根据下标取出字符串 int idCnt; int getIndex(string s){ if(str2int.find(s) != str2int.end()){ return str2int[s]; }else{ str2int[s] = idCnt;//如果没存在就另其为index++ int2str[idCnt] = s;//把index对应的str存入map中 return idCnt++; } } vector<int> tmpath, path; int maxvalue = 0, maxavg = 0; void dfs(int v){ tmpath.push_back(v); if(v == 0){ //此时遍历到了起点 int value = 0; for(int i = 0; i < tmpath.size(); i++){ value += happy[tmpath[i]]; } double tmpavg = 1.0 * value / (tmpath.size() - 1); if(tmpavg > maxavg){ maxavg = tmpavg; path = tmpath; } tmpath.pop_back(); return; } for(int i = 0; i < pre[v].size(); i++) dfs(pre[v][i]);//遍历所有最大权值一样的情况,求平均权值最大的路径 tmpath.pop_back(); } void djistra(int s){ fill(d, d + Max, Inf); memset(h, 0, sizeof(h)); memset(num, 0, sizeof(num)); d[s] = 0; h[s] = happy[s]; num[s] = 1; for(int i = 0; i < n; i++){ int u = -1, min = Inf; for(int j = 0; j < n; j++){ if(!vis[j] && d[j] < min){ u = j; min = d[j]; } } if(u == -1) return; vis[u] = true; for(int v = 0; v < n; v++){ if(!vis[v] && G[u][v] != Inf){ if(d[u] + G[u][v] < d[v]){ d[v] = d[u] + G[u][v]; h[v] = h[u] + happy[v]; pre[v].clear(); pre[v].push_back(u); num[v] = num[u]; } else if(G[u][v] + d[u] == d[v]){ if(h[u] + happy[v] > h[v]){ h[v] = h[u] + happy[v]; pre[v].clear(); pre[v].push_back(u); }else if(h[u] + happy[v] == h[v]){ pre[v].push_back(u); } num[v] += num[u]; } } } } } int main(){ idCnt = 0; string sname; cin>>n>>k>>sname; pre.resize(n + 1); int sid = getIndex(sname);//获取开始城市的下标 for(int i = 0; i < n - 1; i++){ string name; int score; int id; cin>>name>>score; id = getIndex(name); happy[id] = score;//录下幸福指数 } fill(G[0], G[0] + Max * Max, Inf);//初始化G for(int i = 0; i < k; i++){ string ustr, vstr; int cost; cin>>ustr>>vstr>>cost; int u = getIndex(ustr), v = getIndex(vstr); G[u][v] = cost; G[v][u] = cost; } djistra(sid); int eid = str2int["ROM"]; cout<<num[eid]<<" "<<d[eid]<<" "<<h[eid]<<" "; //求路径以及最低平均分 dfs(eid);//递归获得路径从末尾开始 cout<<int(maxavg)<<endl; for(int i = path.size() - 1; i >= 1; i--) cout<<int2str[path[i]]<<"->"; cout<<"ROM"; }
这篇关于【Dijstra+平均点权和最大+路径】1087 All Roads Lead to Rome (30 分)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-21Vue3教程:新手入门到实践应用
- 2024-12-21VueRouter4教程:从入门到实践
- 2024-12-20Vue3项目实战:从入门到上手
- 2024-12-20Vue3项目实战:新手入门教程
- 2024-12-20VueRouter4项目实战:新手入门教程
- 2024-12-20如何实现JDBC和jsp的关系?-icode9专业技术文章分享
- 2024-12-20Vue项目中实现TagsView标签栏导航的简单教程
- 2024-12-20Vue3入门教程:从零开始搭建你的第一个Vue3项目
- 2024-12-20从零开始学习vueRouter4:基础教程
- 2024-12-20Vuex4课程:新手入门到上手实战全攻略