1087. All Roads Lead to Rome (30) <Dijkstra优先队列>

来源:互联网 发布:林黛玉 知乎 编辑:程序博客网 时间:2024/05/21 15:04

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
。。。。。贼烦,最后输出route和len的时候输出倒了,还给了15分。无线改代码(N长的时间)

最后百度看别人的代码先输出route再输出len。。真想把自己砍了

len代表最短长度,sum代表最大happiness,route代表到达方案。path纪录路径前驱

用Dijkstra求len在

if(len[f]>len[num]+ditu[num][f]) 就相当于这个点的所有信息全部被替换,覆盖

route直接等于上一个节点的route

sum值直接更新

path相应的更新


if(len[f]==len[num]+ditu[num][f])

这时候route是累加的。因为路径长度相同

sum需要判断,因为路径长度相同,需要判断更新sum的值

path对应更新


按照样例画个图,根据图来理解

最后输出path中的路径(路径是反的,纪录的是前驱)


#include<cstdio>#include<cmath>#include<algorithm>#include<iostream>#include<cstring>#include<queue>#include<vector>#include<set>#include<map>#include<stack>using namespace std;const int maxn=205;const int INF=1<<29;int n,m;static int ditu[maxn][maxn];int value[maxn]={0};vector<int> ma[maxn]; int v[maxn];int path[maxn];int len[maxn];int route[maxn]={0};int sum[maxn];map<int,string> mm;map<string,int> mmm;typedef pair<int,int> P;void Dijkstra(int x){for(int i=0;i<maxn;i++)v[i]=0,len[i]=INF,sum[i]=0;priority_queue<P,vector<P>,greater<P> > que;len[x]=0;route[x]=1;que.push({len[x],x});while(que.size()){P p=que.top();que.pop();int num=p.second;if(v[num]) continue;v[num]=1;for(int i=0;i<ma[num].size();i++){int f=ma[num][i];if(len[f]>len[num]+ditu[num][f]){len[f]=len[num]+ditu[num][f];route[f]=route[num];sum[f]=sum[num]+value[f]; path[f]=num;//cout<<f<<" "<<num<<endl; que.push({len[f],f});}else if(len[f]==len[num]+ditu[num][f]){route[f]+=route[num];if(sum[f]<sum[num]+value[f]){   sum[f]=sum[num]+value[f];    // cout<<f<<" "<<num<<endl;  path[f]=num; }que.push({len[f],f});}}}}int main(){string start;cin>>n>>m>>start;for(int i=0;i<maxn;i++) for(int j=0;j<maxn;j++) ditu[i][j]=INF;for(int i=0;i<n-1;i++){string s;int num;cin>>s>>num;mmm[s]=i+1;mm[i+1]=s;value[i+1]=num;}for(int i=0;i<m;i++){string s1,s2;int num;cin>>s1>>s2>>num;//scanf("%s %s %d",s1,s2,&num);int fz1=mmm[s1];int fz2=mmm[s2];//cout<<fz1<<" "<<fz2<<endl;ditu[fz1][fz2]=ditu[fz2][fz1]=num;ma[fz1].push_back(fz2);ma[fz2].push_back(fz1);}Dijkstra(0);int end=mmm["ROM"];int fz=end;int cnt=1;stack<string> sta;while(path[fz]!=0){sta.push(mm[fz]);fz=path[fz];cnt++;}sta.push(mm[fz]);cout<<route[end]<<" "<<len[end]<<" "<<sum[end];printf(" %d\n",sum[end]/cnt);cout<<start;while(sta.size()){cout<<"->"<<sta.top();sta.pop();}return 0;}