1087. All Roads Lead to Rome

来源:互联网 发布:轩辕传奇辅助软件 编辑:程序博客网 时间:2024/05/23 07:23

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 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1
Sample Output:
3 3 195 97HZH->PRS->ROM
忍住用dfs的想法,用dijkstra,这种要记录路径的,要再添一个数组记录父节点。
写的慢,考前得专门再找图的题练习。
特别注意一下替换时的路径总数等于前一个节点的路径总数而不是1,添加的也是加上前一个节点的路径总数而不是+1。
#include<iostream>#include<vector>#include<algorithm>#include<map>#include<string>using namespace std;const int INF=0x7fffffff;typedef struct node{string name;int hp;int dis;int cnt;bool f;int sum;};node city[200];map <string,int> num;int n,k;int d[200][200];int h[200];int last[200];void dijkstra(){while(1){int minc=INF;int minn=-1;for(int i=0;i<n;++i)if(!city[i].f&&city[i].dis<minc){minn=i;minc=city[i].dis;}if(minn==-1) break;city[minn].f=true;for(int i=0;i<n;++i){if(d[minn][i]<INF){if(city[i].dis>minc+d[minn][i]){last[i]=minn;city[i].dis=minc+d[minn][i];city[i].hp=h[i]+city[minn].hp;city[i].cnt=city[minn].cnt+1;city[i].sum=city[minn].sum;}else if(city[i].dis==minc+d[minn][i]){city[i].sum+=city[minn].sum;if(city[i].hp<h[i]+city[minn].hp){last[i]=minn;city[i].hp=h[i]+city[minn].hp;city[i].cnt=city[minn].cnt+1;}else if(city[i].hp==h[i]+city[minn].hp&&city[i].cnt>city[minn].cnt+1){last[i]=minn;city[i].cnt=city[minn].cnt+1;}}}}}}int main(){for(int i=0;i<200;++i){city[i].dis=INF;city[i].f=false;city[i].hp=0;for(int j=0;j<200;j++)d[i][j]=INF;}string name;cin>>n>>k>>name;num[name]=0;city[0].name=name;city[0].cnt=0;city[0].dis=0;city[0].sum=1;for(int i=1;i<n;++i){cin>>name>>h[i];num[name]=i;city[i].name=name;}for(int i=0;i<k;++i){string c1,c2;int dis;cin>>c1>>c2>>dis;d[num[c1]][num[c2]]=d[num[c2]][num[c1]]=dis;}dijkstra();last[0]=-1;vector<int>ans;int s=num["ROM"];cout<<city[s].sum<<" "<<city[s].dis<<" "<<city[s].hp<<" "<<city[s].hp/city[s].cnt<<endl;while(s!=-1){ans.push_back(s);s=last[s];}reverse(ans.begin(),ans.end());cout<<city[ans[0]].name;for(int i=1;i<ans.size();++i)cout<<"->"<<city[ans[i]].name;return 0;}


0 0
原创粉丝点击