1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise
来源:互联网 发布:台湾人眼中的大陆知乎 编辑:程序博客网 时间:2024/05/21 20:35
题目信息
1087. All Roads Lead to Rome (30)
时间限制200 ms
内存限制65536 kB
代码长度限制16000 B
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算法
AC代码
#include <iostream>#include <stdio.h>#include <string>#include <map>#include <vector>using namespace std;#define MAX 66666666#define SIZE 203vector<int> resPath;int routeSum=0,shortest = MAX,resHap=0,avgHap = 0;map<string,int> hapiness1;map<int,string> hapiness2;map<int,int> hapSet;int graph[SIZE][SIZE];int dij[SIZE];int visit[SIZE];int n,k;void dfs(int start,int end,vector<int> &path,int hapSum,int pathSum);void dijkstra(int s);int main(){ int i,j,index1,tmp1,tmp2,tmp3; int dest = 0; string city1,city2,city3,city4; cin>>n>>k>>city1; index1= 0; hapiness1[city1] = index1; hapiness2[index1] = city1; hapSet[index1] = 0; for(i=1;i<n;i++) { cin>>city2>>tmp1; if(city2.compare("ROM")==0) { dest = i; } hapiness1[city2] = i; hapiness2[i] = city2; hapSet[i] = tmp1; } for(i=0;i<n;i++) { dij[i] = MAX; for(j=0;j<n;j++) { graph[i][j] = MAX; } } for(i=0;i<k;i++) { cin>>city2>>city3>>tmp1; tmp2 = hapiness1[city2]; tmp3 = hapiness1[city3]; if(tmp1<graph[tmp2][tmp3]) { graph[tmp2][tmp3] = tmp1; graph[tmp3][tmp2] = tmp1; } } dijkstra(0); for(i=0;i<n;i++) { visit[i] = 0; } shortest = dij[dest]; vector<int> myPath(1,0); visit[0] = 1; dfs(0,dest,myPath,0,0); printf("%d %d %d %d\n",routeSum,shortest,resHap,avgHap); cout<<city1; for(i=1;i<resPath.size();i++) { cout<<"->"<<hapiness2[resPath[i]]; } cout<<endl; return 0;}void dfs(int start,int end,vector<int> &path,int hapSum,int pathSum){ int i,j; if(pathSum>shortest) { return ; } if(start==end) { if(pathSum>shortest) { return ; } routeSum ++; if(hapSum<resHap) { return ; } int ta = hapSum/(path.size()-1); if(ta>avgHap) { resPath = path; avgHap = ta; resHap = hapSum; } return ; } for(i=0;i<n;i++) { if(!visit[i] && graph[start][i]!=MAX) { path.push_back(i); visit[i] = 1; dfs(i,end,path,hapSum + hapSet[i],pathSum+graph[start][i]); path.pop_back(); visit[i] = 0; } }}void dijkstra(int s){ int i,j,tmp,so; for(i=0;i<n;i++) { dij[i] = graph[s][i]; visit[i] = 0; } dij[s] = 0; visit[s] = 1; for(i=1;i<n;i++) { tmp = MAX; so = s; for(j=0;j<n;j++) { if(!visit[j] && dij[j]<tmp) { tmp = dij[j]; so = j; } } visit[so] = 1; for(j=0;j<n;j++) { if(!visit[j] && dij[so]+graph[so][j]<dij[j]) { dij[j] = dij[so]+graph[so][j]; } } }}
- 1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise
- 【PAT】【Advanced Level】1087. All Roads Lead to Rome (30)
- PAT (Advanced Level) Practise 1087 All Roads Lead to Rome (30)
- PAT (Advanced Level) Practise 1087 All Roads Lead to Rome (30)
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 条条大路通罗马 Dijkstra+DFS
- Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)
- PAT 1087. All Roads Lead to Rome (最短路变形_好题)
- PAT 1087. All Roads Lead to Rome
- 【PAT】1087. 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 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) 解答
- 115. Distinct Subsequences dp算法
- 2016"百度之星" - 资格赛(Astar Round1)Problem D(排序+map)
- 系统错误修复精灵绿色免费版
- 数据结构_静态链表_哈夫曼
- 一句话明白排序算法之二——冒泡排序
- 1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise
- JVM 垃圾回收器工作原理及使用实例介绍(原文已发表于IBM开发者论坛)
- IP聚合
- PTA 5-31 字符串循环左移
- 使用nexus在linux部署私有仓库
- PAT B 1018. 锤子剪刀布
- 一个经典例子让你彻彻底底理解java回调机制
- 关于事件冒泡和默认行为的学习
- 数据结构上机测试1:顺序表的应用