PAT (Advanced Level) Practise 1087 All Roads Lead to Rome (30)
来源:互联网 发布:淘宝联盟结算时间 编辑:程序博客网 时间:2024/06/05 09:59
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 <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;map<string,int>mp;int x[205],n,m;int s[205],nt[80009],e[80009],w[80009];int vis[205],dis[205],sum[205],p[205],path[205],ave[205];char s1[5],s2[5],ch[205][5];struct node{ int id,k,dis; friend bool operator <(node a,node b) { return a.dis>b.dis; }}pre,nt1;void Dijkstra(int k){ memset(vis,0,sizeof vis); memset(dis,INF,sizeof dis); memset(sum,0,sizeof sum); memset(ave,0,sizeof ave); dis[1]=0,path[1]=1,p[1]=-1; pre.id=1,pre.dis=0,pre.k=1; priority_queue<node>q; q.push(pre); while(!q.empty()) { pre=q.top(); q.pop(); vis[pre.id]=1; if(pre.id==k) break; for(int i=s[pre.id];~i;i=nt[i]) { int ee=e[i],ww=w[i]; if(vis[ee]) continue; if(dis[ee]>dis[pre.id]+ww) { dis[ee]=dis[pre.id]+ww; p[ee]=pre.id; sum[ee]=sum[pre.id]+x[ee]; path[ee]=path[pre.id]; ave[ee]=sum[ee]/pre.k; nt1.k=pre.k+1,nt1.id=ee,nt1.dis=dis[ee]; q.push(nt1); } else if(dis[ee]==dis[pre.id]+ww) { path[ee]+=path[pre.id]; if(sum[ee]<sum[pre.id]+x[ee]) { sum[ee]=sum[pre.id]+x[ee]; ave[ee]=sum[ee]/pre.k; p[ee]=pre.id; } else if(sum[ee]==sum[pre.id]+x[ee]&&ave[ee]<sum[ee]/pre.k) { ave[ee]=sum[ee]/pre.k; p[ee]=pre.id; } } } } printf("%d %d %d %d\n",path[k],dis[k],sum[k],ave[k]);}int main(){ while(~scanf("%d%d%s",&n,&m,ch[1])) { mp.clear(); mp[ch[1]]=1; int cnt=1,ee; memset(s,-1,sizeof s); for(int i=2;i<=n;i++) { scanf("%s%d",ch[i],&x[i]); mp[ch[i]]=i; if(!strcmp(ch[i],"ROM")) ee=mp[ch[i]]; } for(int i=1;i<=m;i++) { int ww; scanf("%s%s%d",s1,s2,&ww); int uu=mp[s1],vv=mp[s2]; nt[cnt]=s[uu],s[uu]=cnt,e[cnt]=vv,w[cnt++]=ww; nt[cnt]=s[vv],s[vv]=cnt,e[cnt]=uu,w[cnt++]=ww; } Dijkstra(ee); int k=ee,flag=1; stack<int>s; while(k!=-1) { s.push(k); k=p[k]; } while(!s.empty()) { int pre=s.top(); s.pop(); if(flag) printf("%s",ch[pre]); else printf("->%s",ch[pre]); flag=0; } printf("\n"); } return 0;}
- PAT (Advanced Level) Practise 1087 All Roads Lead to Rome (30)
- PAT (Advanced Level) Practise 1087 All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise
- Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)
- 【PAT】【Advanced Level】1087. All Roads Lead to Rome (30)
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 条条大路通罗马 Dijkstra+DFS
- pat 1087 All Roads Lead to Rome (30)
- PAT 1087. All Roads Lead to Rome
- 【PAT】1087. All Roads Lead to Rome
- PAT-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
- 用jquery动态生成的元素绑定事件
- [bzoj4152][AMPPZ2014]The Captain dijkstra
- 理解shallow heap 和 retained heap
- 二叉树按层遍历
- java之回调函数
- PAT (Advanced Level) Practise 1087 All Roads Lead to Rome (30)
- 设计模式的学习--(3)工厂方法模式
- ggg
- linux tr命令详解
- 数组转list的两个方法
- 语音识别 声纹识别 音频搜索 视频搜索 广电监控 媒资管理 中文视频搜索
- 用JS制作一个倒计时的按钮
- word2vec
- 教你pc端如何快速进行排名。