PAT 1087. All Roads Lead to Rome (30)
2015-08-22 11:19
323 查看
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算法
#include <iostream> #include <string> #include <map> #include <vector> #include <bitset> using namespace std; const int INF = 0x7fffffff; map<string, int> indexMap; vector<string> city; int happiness[200]; int cityMap[200][200]; int maxHappiness[200]; int minCost[200]; int cityPast[200]; int minCostRoute[200]; bitset<200> visited; int pre[200]; int main() { int cityNum, routeNum; cin >> cityNum >> routeNum; string start; cin >> start; indexMap[start] = 0; city.push_back(start); for (int i = 1; i < cityNum; i++) { string c; cin >> c >> happiness[i]; indexMap[c] = i; city.push_back(c); } for (int i = 0; i < cityNum; i++) //initial the city map for (int j = 0; j < cityNum; j++) cityMap[i][j] = cityMap[j][i] = INF; for (int i = 0; i < routeNum; i++) { string c1, c2; int cost; cin >> c1 >> c2 >> cost; int index1 = indexMap[c1]; int index2 = indexMap[c2]; cityMap[index1][index2] = cityMap[index2][index1] = cost; } for (int i = 0; i < cityNum; i++) { maxHappiness[i] = 0; minCost[i] = INF; } //using dijkstra algorithm visited.set(0); int min = 0; maxHappiness[0] = 0; minCost[0] = 0; cityPast[0] = 0; minCostRoute[0] = 1; while (1) { for (int i = 0; i < cityNum; i++) { if (cityMap[min][i] < INF) { if (minCost[min] + cityMap[min][i] < minCost[i]) { pre[i] = min; minCost[i] = minCost[min] + cityMap[min][i]; maxHappiness[i] = maxHappiness[min] + happiness[i]; cityPast[i] = cityPast[min] + 1; minCostRoute[i] = minCostRoute[min]; } else if (minCost[min] + cityMap[min][i] == minCost[i]) { minCostRoute[i] += minCostRoute[min]; if (maxHappiness[min] + happiness[i] > maxHappiness[i]) { pre[i] = min; maxHappiness[i] = maxHappiness[min] + happiness[i]; cityPast[i] = cityPast[min] + 1; } else if (maxHappiness[min] + happiness[i] == maxHappiness[i]) { if (cityPast[min] + 1 < cityPast[i]) { pre[i] = min; cityPast[i] = cityPast[min] + 1; } } } } } int cost = INF; for (int j = 0; j < cityNum; j++) { if (!visited[j] && cost > minCost[j]) { cost = minCost[j]; min = j; } } visited.set(min); if (cost==INF) break; } int ROMIndex = indexMap["ROM"]; cout << minCostRoute[ROMIndex] << " " << minCost[ROMIndex] << " " << maxHappiness[ROMIndex] << " " << maxHappiness[ROMIndex] / cityPast[ROMIndex] << endl; vector<string> vec; for (int i = ROMIndex; i != 0; i = pre[i]) vec.push_back(city[i]); vec.push_back(start); for (vector<string>::reverse_iterator it = vec.rbegin(); it != vec.rend()-1; it++) cout << *it << "->"; cout << vec.front(); }
相关文章推荐
- 部分真题整理4
- Class org.apache.struts2.json.JSONWriter can not access a member of class oracle.jdbc.driver.Physica
- SQL 获取连续编号中断号的最小值
- Go语言中的单元测试及反向代理
- 将求最长子序列的算法由O(n²)改进为O(n).
- [Angularjs]ng-repeat中使用ng-model遇到的问题
- 洛谷1004 方格取数
- swift 创建桥接头文件
- LeetCode "Shortest Word Distance II"
- 虚拟机下安装CentOS无法上网的解决方案
- Zabbix Agent出现ZBX_TCP_READ() failed: [4] Interrupted system call问题解决
- jquery ajax 解决跨域访问问题
- Oracle第二弹--SQL语言概述
- 第87讲:Scala中使用For表达式做查询
- OLTP和OLAP的区别
- Struts(19)Struts集成
- unity3d渲染到CubeMap的贴图保存工具
- CentOSserverMysql主从复制集群结构
- juniper交换机配置
- linux下的文件压缩与打包,compress,gzip,zcat,bzip2,bzcat,tar,find,tarfile,tarball