PAT 1087
2016-05-29 19:50
316 查看
1087. All Roads Lead to Rome (30)
时间限制200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
题意:求最短路径,以及路数,和路径数量,以及平均最大幸福度
难点:不再是想以前给个数字,可以直接用dij算法,用c++中 map将字符串与数字对应起来比较方便,不然用c写太累了;
#include <stdio.h> #include <map> #include <algorithm> #include <iostream> #include <vector> #include <string.h> #include <string> #include <limits.h> using namespace std; int n,k; string start; map<string,int> happy; map<int ,string> duiying; map<string,int> rduiying; int garph[200][200]; int visit[201]={0}; int path[201]={-1}; int dist[200]; int road[201]={0}; int cc[201]={0}; int renshu[201]={0};
void di() { visit[0]=1; for(int i=1;i<n;i++) { if(garph[0][i]!=-1) { dist[i]=garph[0][i]; path[i]=0; road[i]=1; renshu[i]=1; } cc[i]=happy[duiying[i]]; } dist[0]=0; while(1) { int mindist=1<<30; int temp; for(int i=1;i<n;i++) { if(dist[i]<mindist&&visit[i]==0) { mindist=dist[i]; temp=i; } } if(mindist==1<<30) break; visit[temp]=1; for(int i=1;i<n;i++) { if(garph[temp][i]!=-1&&garph[temp][i]+dist[temp]<dist[i]) { dist[i]=dist[temp]+garph[temp][i]; road[i]=road[temp]; path[i]=temp; renshu[i]=renshu[temp]+1; cc[i]=cc[temp]+happy[duiying[i]]; } else if(garph[temp][i]!=-1&&garph[temp][i]+dist[temp]==dist[i]) { road[i]+=road[temp]; if(cc[i]<cc[temp]+happy[duiying[i]]) { cc[i]=cc[temp]+happy[duiying[i]]; path[i]=temp; } else if(cc[i]==cc[temp]+happy[duiying[i]]) { if(renshu[i]>renshu[temp]+1) renshu[i]=renshu[temp]+1; } } } } } int main() { memset(garph,-1,sizeof(garph)); memset(dist,INT_MAX,sizeof(dist)); for(int i=0;i<200;i++) dist[i]=1<<30; cin>>n>>k>>start; duiying[0]=start; rduiying.insert(make_pair(start,0)); for(int i=1;i<n;i++) { string s1; int hap; cin>>s1>>hap; duiying[i]=s1; rduiying.insert(make_pair(s1,i)); happy.insert(make_pair(s1,hap)); } for(int i=0;i<k;i++) { string c1,c2; int cost; cin>>c1>>c2>>cost; int n1=rduiying[c1]; int n2=rduiying[c2]; garph[n1][n2]=garph[n2][n1]=cost; } di(); int kk=rduiying["ROM"]; vector<int> v; for(int i=kk;i!=0;) { v.push_back(i); i=path[i]; } //cout<<v.size(); cout<<road[kk]<<" "<<dist[kk]<<" "<<cc[kk]<<" "<<cc[kk]/v.size()<<endl; cout<<start; for(int i=v.size()-1;i>=0;i--) cout<<"->"<<duiying[v[i]]; cout<<endl; return 0; }
相关文章推荐
- PAT 1090
- 浙大PAT1086
- PAT 1087
- 浙大PAT 1099
- 浙大PAT 1102
- 浙大PAT1106
- 浙大PAT 1065 A+B and C (64bit) (20)
- 浙大PAT1064
- 浙大PAT 1063
- 浙大PAT 1058
- 浙大PAT1059 Prime Factors
- 浙大PAT1053
- 浙大PAT 1051
- 浙大pat1030
- 浙大PAT 1020. Tree Traversals (25)
- 4-1 简单输出整数
- 4-2 多项式求值
- PAT 1034
- Java 实现PAT乙级(Basic Level)1001-1010 解题报告(一)
- 浙大pat | 浙大pat乙级 1001~1004