准备PAT之All Roads Lead to Rome Dijikstra算法变型
来源:互联网 发布:wifi连接上没有网络 编辑:程序博客网 时间:2024/05/22 09:59
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
#include<iostream>#include<algorithm>#include<vector>#include<string>#include<cstring>#include<map>using namespace std;const int INF = 0x3f3f3f3f;const int max_city = 200 + 5;map<string, int>city;//保证可以通过城市i的名字就可以找到它的编号map<int, string>r_city;//保证知道城市编号就可以知道它的名字int cost[max_city][max_city], city_num, road_num;int dist[max_city];//最小距离int happiness[max_city], have_happiness[max_city];//城市i本身的快乐;到城市i你拥有的快乐int if_visit[max_city];int pass_city_num[max_city], pre[max_city];//到达城市i时所通过的最小城市数int arrive_road_num[max_city];//到达城市i的最短路径条数void Init() { for (int i = 0; i <= city_num; i++) { happiness[i] = 0; for (int j = 0; j <= city_num; j++) { cost[i][j] = INF; } }}void Input() { string star_city; cin >> city_num >> road_num >> star_city; Init(); city[star_city] = 0; r_city[0] = star_city; happiness[city[star_city]] = 0; for (int i = 1; i < city_num; i++) { string city_name; cin >> city_name >> happiness[i]; city[city_name] = i; r_city[i] = city_name; } for (int i = 0; i < road_num; i++) { string from, to; int happy; cin >> from >> to >> happy; cost[city[from]][city[to]] = min(cost[city[from]][city[to]], happy); cost[city[to]][city[from]] = min(cost[city[to]][city[from]], happy); }}void Dijikstra() { for (int i = 0; i <= city_num; i++) { if_visit[i] = have_happiness[i] = pass_city_num[i] = 0; dist[i] = INF; } dist[0] =0; pre[0] = -1; arrive_road_num[0] = 1; for (int i = 0; i < city_num; i++) { int v = -1; //找到一个未被访问过的点 for (int u = 0; u < city_num; u++) { if (!if_visit[u] && (v == -1 || dist[u] < dist[v])) { v = u; } } if (v == -1) break; if_visit[v] = 1; //从该未被访问的点v依次访问与其连接且未被访问的点j for (int j = 0; j < city_num; j++){ if (!if_visit[j] && cost[v][j] < INF) { int temp = dist[v] + cost[v][j]; if (temp < dist[j]) { //若从点v到点j距离更小则更新 dist[j] = temp; have_happiness[j] = have_happiness[v] + happiness[j];//加上城市j的快乐 arrive_road_num[j] = arrive_road_num[v];//到达点j的最短路径条数就是到达点v的路径条数 pass_city_num[j]=pass_city_num[v]+1;//通过的城市num加1 pre[j] = v;//记录路径——把v加进去 } else if (temp == dist[j]) { arrive_road_num[j] += arrive_road_num[v]; int _happyiness = happiness[j] + have_happiness[v]; int _pass_num = pass_city_num[v] + 1; if (_happyiness > have_happiness[j] || (_happyiness == have_happiness[j] && _pass_num < pass_city_num[j])) { have_happiness[j] = _happyiness; pass_city_num[j] = _pass_num; pre[j] = v; } } } } }}void Output() { int n = city["ROM"]; cout << arrive_road_num[n] << " " << dist[n] << " " << have_happiness[n] << " " << have_happiness[n] / pass_city_num[n] << endl; cout << r_city[0]; vector<string>path; //依次把经过的城市的名字保存到数组里 for (int t = city["ROM"]; t != -1; t = pre[t]) { path.push_back(r_city[t]); } for (int i = path.size() - 2; i >= 0; i--) {//因为最后一个是ROM,但要保证输出统一格式 cout << "->" << path[i]; //所以从倒数第二开始 } cout << endl;}int main() { Input(); Dijikstra(); Output(); return 0;}
“`
- 准备PAT之All Roads Lead to Rome Dijikstra算法变型
- PAT 1087. All Roads Lead to Rome
- 【PAT】1087. All Roads Lead to Rome
- PAT-All Roads Lead to Rome
- 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)
- pat 1087. All Roads Lead to Rome (30)
- pat 1087 All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30) PAT
- pat 1087. All Roads Lead to Rome (30) 解答
- PAT(A) - 1087. All Roads Lead to Rome (30)
- PAT A1087 All Roads Lead to Rome(30)
- PAT甲级练习1087. All Roads Lead to Rome (30)
- 分类中常见的类别不平衡问题解决方法
- .net core 2.0学习笔记(六):Remoting核心类库RealProxy迁移
- 维优尚品——全国首家C2M电子商务平台
- jQuery如何追加tr到table中任意位置--向Table中指定位置添加tr或td(jQuery)
- java的工厂模式
- 准备PAT之All Roads Lead to Rome Dijikstra算法变型
- 高效实现List反序功能的实现
- Iterator、AbstractCollection、AbstractList
- C#路径中获取文件全路径、目录、扩展名、文件名称
- Android开发显示之前获取view的宽高方法
- 直接继承View来自定义控件时,需要重写onMeasure()方法并设置wrap_content时的大小 原理分析
- [日推荐]『拉了吗』出门必备
- 如何设置connect超时时间
- restful API接口规范