PAT 1087. All Roads Lead to Rome (30)
来源:互联网 发布:大麦dm1016破解网络锁 编辑:程序博客网 时间:2024/05/22 09:32
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 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1Sample Output:
3 3 195 97HZH->PRS->ROM
#include <iostream>#include<stdio.h>#include<queue>#include<algorithm>#include<string.h>#include<string>#include<vector>#include<ctype.h>#include<set>#include<map>using namespace std;int n,k;string start;int p=0;int road[202][202];int happy[202];int thappy[202];int mark[202];int how[202];int dist[202];map<int,string> L;map<string,int> M;int same[202];int total=0;int last[202];int pp=0;void dijkstra() { int np=0; mark[0]=1; how[0]=0; same[0]=1; memset(dist,-1,sizeof(dist)); dist[0]=0; for(int i=1;i<n;i++) { for(int j=0;j<n;j++) { if(mark[j]||road[np][j]==-1) continue; if(dist[j]==-1||dist[j]>dist[np]+road[np][j]) { dist[j]=dist[np]+road[np][j]; thappy[j]=thappy[np]+happy[j]; last[j]=np; same[j]=same[np]; //容易错 how[j]=how[np]+1; } else if(dist[j]==dist[np]+road[np][j]) { same[j]+=same[np]; //容易错 if(thappy[np]+happy[j]>thappy[j]) { thappy[j]=thappy[np]+happy[j]; last[j]=np; how[j]=how[np]+1; } else if(how[np]+1<how[j]) { last[j]=np; how[j]=how[np]+1; } } } int maxx=99999; for(int j=0;j<n;j++) { if(dist[j]==-1||mark[j]) continue; if(dist[j]<maxx) { maxx=dist[j]; np=j; } } mark[np]=1; } }int main(){ cin>>n>>k>>start; M[start]=p++; string tmp; L[0]=start; L[1]="ROM"; memset(road,-1,sizeof(road)); tmp="ROM"; M[tmp]=p++; for(int i=0;i<n-1;i++) { cin>>tmp; int num; if(M.find(tmp)==M.end()) { num=p; L[num]=tmp; M[tmp]=p++; } else num=M[tmp]; scanf("%d",&happy[num]); } for(int i=0;i<k;i++) { string a,b; int x,y,l; cin>>a>>b>>l; x=M[a]; y=M[b]; road[x][y]=road[y][x]=l; } dijkstra(); int zzz=thappy[1]/how[1]; cout<<same[1]<<' '<<dist[1]<<' '<<thappy[1]<<' '<<zzz<<endl; int llast[202]; pp=1; int hhh=0; while(pp!=0) { llast[hhh++]=pp; pp=last[pp]; } llast[hhh++]=0; cout<<L[0]; int x=hhh-2; while(x>=0) { cout<<"->"<<L[llast[x]]; x--; } return 0;}
- 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) 解答
- PAT(A) - 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】【Advanced Level】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)
- 北大新班子正式起航,带领北大航船驶向何方?
- Lintcode114 Unique Paths solution 题解
- Lintcode115 Unique Paths || solution 题解
- read file write
- Adobe Acrobat Reader DC 离线安装包
- PAT 1087. All Roads Lead to Rome (30)
- 【Leetcode】【python】Next Permutation
- FTPrep, 57 Insert interval
- 2017-9-7-第一篇博客
- android使用xml定义背景边框
- PHPforeach()跳出本次或当前循环与终止循环方法
- 单道进程
- 常用html标签复习
- 机器学习中的k-means聚类及其Python实例