PAT 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 recommended 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

 典型的最短路径问题,采用dijkstra算法

  1 #include <iostream>
  2 #include <string>
  3 #include <map>
  4 #include <vector>
  5 #include <bitset>
  6 
  7 using namespace std;
  8 
  9 const int INF = 0x7fffffff;
 10 map<string, int> indexMap;
 11 vector<string> city;
 12 int happiness[200];
 13 int cityMap[200][200];
 14 int maxHappiness[200];
 15 int minCost[200];
 16 int cityPast[200];
 17 int minCostRoute[200];
 18 bitset<200> visited;
 19 int pre[200];
 20 
 21 int main()
 22 {
 23     int cityNum, routeNum;
 24     cin >> cityNum >> routeNum;
 25     string start;
 26     cin >> start;
 27     indexMap[start] = 0;
 28     city.push_back(start);
 29     for (int i = 1; i < cityNum; i++)
 30     {
 31         string c;
 32         cin >> c >> happiness[i];
 33         indexMap[c] = i;
 34         city.push_back(c);
 35     }
 36 
 37     for (int i = 0; i < cityNum; i++)    //initial the city map
 38         for (int j = 0; j < cityNum; j++)
 39             cityMap[i][j] = cityMap[j][i] = INF;
 40     for (int i = 0; i < routeNum; i++)
 41     {
 42         string c1, c2;
 43         int cost;
 44         cin >> c1 >> c2 >> cost;
 45         int index1 = indexMap[c1];
 46         int index2 = indexMap[c2];
 47         cityMap[index1][index2] = cityMap[index2][index1] = cost;
 48     }
 49     
 50     for (int i = 0; i < cityNum; i++)
 51     {
 52         maxHappiness[i] = 0;
 53         minCost[i] = INF;
 54     }
 55 
 56     //using dijkstra algorithm
 57     visited.set(0);
 58     int min = 0;
 59     maxHappiness[0] = 0;
 60     minCost[0] = 0;
 61     cityPast[0] = 0;
 62     minCostRoute[0] = 1;
 63     while (1)
 64     {
 65         for (int i = 0; i < cityNum; i++)
 66         {
 67             if (cityMap[min][i] < INF)
 68             {
 69                 if (minCost[min] + cityMap[min][i] < minCost[i])
 70                 {
 71                     pre[i] = min;
 72                     minCost[i] = minCost[min] + cityMap[min][i];
 73                     maxHappiness[i] = maxHappiness[min] + happiness[i];
 74                     cityPast[i] = cityPast[min] + 1;
 75                     minCostRoute[i] = minCostRoute[min];
 76                 }
 77                 else if (minCost[min] + cityMap[min][i] == minCost[i])
 78                 {
 79                     minCostRoute[i] += minCostRoute[min];
 80                     if (maxHappiness[min] + happiness[i] > maxHappiness[i])
 81                     {
 82                         pre[i] = min;
 83                         maxHappiness[i] = maxHappiness[min] + happiness[i];
 84                         cityPast[i] = cityPast[min] + 1;
 85                     }
 86                     else if (maxHappiness[min] + happiness[i] == maxHappiness[i])
 87                     {
 88                         if (cityPast[min] + 1 < cityPast[i])
 89                         {
 90                             pre[i] = min;
 91                             cityPast[i] = cityPast[min] + 1;
 92                         }
 93                     }
 94                 }
 95             }
 96         }
 97 
 98         int cost = INF;
 99         for (int j = 0; j < cityNum; j++)
100         {
101             if (!visited[j] && cost > minCost[j])
102             {
103                 cost = minCost[j];
104                 min = j;
105             }
106         }
107         visited.set(min);
108         if (cost==INF)
109             break;
110     }
111 
112     int ROMIndex = indexMap["ROM"];
113     cout << minCostRoute[ROMIndex] << " " << minCost[ROMIndex] << " " << maxHappiness[ROMIndex] << " "
114         << maxHappiness[ROMIndex] / cityPast[ROMIndex] << endl;
115     vector<string> vec;
116     for (int i = ROMIndex; i != 0; i = pre[i])
117         vec.push_back(city[i]);
118     vec.push_back(start);
119     for (vector<string>::reverse_iterator it = vec.rbegin(); it != vec.rend()-1; it++)
120         cout << *it << "->";
121     cout << vec.front();
122 }

 

转载于:https://www.cnblogs.com/jackwang822/p/4749861.html

基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip 个人大四的毕业设计、课程设计、作业、经导师指导并认可通过的高分设计项目,评审平均分达96.5分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 [资源说明] 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设或者课设、作业,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96.5分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),供学习参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值