这题和之前一题紧急救援有点像
这里用的变形的dijkstra 每个节点设置一个vector,遇到相同最短路的,就在vector记录来自哪个节点;
这样 dijkstra结束后,就可以从终点往前推出 所有来的最短路径;再对这些路径进行dfs,找到快乐值最大的路径即可;
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 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 1Sample Output:
3 3 195 97 HZH->PRS->ROM
#include<iostream> #include<algorithm> #include<queue> #include<string> #include<map> #include<vector> using namespace std; int n,k,mincost; int route[200][200],happy[200]; vector<int> vr[200]; string *city; map<string,int> m_city; int coun=999,maxhappy=0,ways=0; vector<int> path,pa; class node{ public:int a,b,dis; bool operator<(const node n2)const{ return dis>n2.dis; } }; int dijkstra(int rom){ int dis[200]; for(int i=0;i<200;i++) dis[i]=99999; node n1,now; n1.a=-1;n1.b=0; n1.dis=0; priority_queue<node> q; q.push(n1); while(!q.empty()){ now=q.top(); q.pop(); if(now.dis==dis[now.b]) vr[now.b].push_back(now.a); else if(now.dis<dis[now.b]) { vr[now.b].clear(),vr[now.b].push_back(now.a); dis[now.b]=now.dis; //cout<<city[now.b]<<" "<<dis[now.b]<<endl; if(now.b==rom) continue; if(now.dis>dis[rom]) return dis[rom]; for(int i=0;i<n;i++) {if(route[now.b][i]&&dis[i]>=dis[now.b]+route[now.b][i]) {n1.a=now.b;n1.b=i; n1.dis=dis[now.b]+route[now.b][i]; q.push(n1); } } } } return dis[rom]; } int dfs(int ct,int cnt,int sumhappy){ if(ct==0){ ways++; if(sumhappy>maxhappy) pa=path,coun=cnt,maxhappy=sumhappy; else if(sumhappy==maxhappy&&coun>cnt) pa=path,coun=cnt; return 0; } for(int i=0;i<(int)vr[ct].size();i++) { path.push_back(vr[ct][i]); dfs(vr[ct][i],cnt+1,sumhappy+happy[vr[ct][i]]); path.pop_back(); } return 0; } int main(){ cin>>n>>k; city=new string [n]; string s; cin>>s; city[0]=s; m_city[s]=0; for(int i=1;i<n;i++) { int h; cin>>s; cin>>h; happy[i]=h; city[i]=s; m_city[s]=i; } for(int i=0;i<k;i++) { int a,b,c; string s1,s2; cin>>s1>>s2>>c; a=m_city[s1]; b=m_city[s2]; route[a][b]=c; route[b][a]=c; } int rom=m_city["ROM"]; mincost=dijkstra(rom); dfs(rom,0,happy[rom]); cout<<ways<<" "<<mincost<<" "<<maxhappy<<" "<<maxhappy/coun<<endl; vector<int> ::iterator it; for(it=pa.end()-1;it!=pa.begin();it--) cout<<city[*it]<<"->"; cout<<city[*it]<<"->"; cout<<"ROM"; }