PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 条条大路通罗马 Dijkstra+DFS

来源:互联网 发布:减肥变速跑和慢跑知乎 编辑:程序博客网 时间:2024/06/07 13:09

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
用两个map进行城市名与id(0到N-1)的相互映射。
幸福指数这个变量不参与求最短路径过程中的修正。可直接用一个map进行id到happy的映射,便于最后的方案比较。
用Dijkstra算法确定ROM到起点的最短路径,在此过程中使用一个前驱数组记录结点前驱。
用前驱数组DFS出所有路径并存在结果数组中,该数组存储每个结点的id。
对所有最短路径通过结点id获取幸福指数,进行方案比较。总幸福指数一样时途径城市少的路径更优。
/*2015.7.30cyq*/#include <iostream>#include <vector>#include <string>#include <fstream>#include <map>#include <algorithm>using namespace std;//ifstream fin("case1.txt");//#define cin finconst int MAX=2147483647;struct city{int num;int length;city(int n,int l):num(n),length(l){}};bool cmp(const city &a,const city &b){return a.length<b.length;}//从ROM开始DFS出所有最短路径,存在result中void dfs(vector<vector<int> > &preCity,int &start,int &end,vector<int> &path,vector<vector<int> > &result){path.push_back(start);if(start==end){result.push_back(path);}else{for(auto it=preCity[start].begin();it!=preCity[start].end();++it)dfs(preCity,*it,end,path,result);}path.pop_back();}int main(){int N,K;string s;cin>>N>>K>>s;vector<city> AB,UD;//AB为确定集合,UD为未确定集合vector<vector<int> > roads(N,vector<int>(N,0));map<int,string> id2str;//城市名与id相互映射map<string,int> str2id;map<int,int> id2happy;id2happy[0]=0;str2id[s]=0;id2str[0]=s;city cur(0,0);//起点城市AB.push_back(cur);int ha;int D;//终点ROM的idfor(int i=1;i<=N-1;i++){cin>>s>>ha;if(s=="ROM")D=i;str2id[s]=i;id2str[i]=s;id2happy[i]=ha;UD.push_back(city(i,MAX));//到起点距离为MAX}string s1,s2;int a,b,c;for(int i=0;i<K;i++){cin>>s1>>s2>>c;a=str2id[s1];b=str2id[s2];roads[a][b]=c;roads[b][a]=c;}//Dijkstarvector<vector<int> > preCity(N);//前驱城市while(cur.num!=D){for(auto it=UD.begin();it!=UD.end();it++){if(roads[cur.num][(*it).num]>0){//有路int tmp=cur.length+roads[cur.num][(*it).num];if(tmp<(*it).length){(*it).length=tmp;preCity[(*it).num].clear();preCity[(*it).num].push_back(cur.num);}else if(tmp==(*it).length){preCity[(*it).num].push_back(cur.num);}}}sort(UD.begin(),UD.end(),cmp);cur=UD[0];UD.erase(UD.begin());AB.push_back(cur);}//用前驱数组dfs出ROM到起点的所有最短路径,存储在result中  vector<int> path;vector<vector<int> > result;int start=D;int end=0;dfs(preCity,start,end,path,result);//方案比较int COST=cur.length;//总费用int BEST=-1;//最好路线序号,i表示resule[i]int MHP=0;  //最大幸福指数int COUNT=0;//路径上的城市数int mhp,count;for(int it=0;it<result.size();it++){mhp=0;count=0;for(auto ik=result[it].begin();ik!=result[it].end();ik++){count++;mhp+=id2happy[*ik];}if(mhp>MHP){MHP=mhp;BEST=it;COUNT=count;}else if(mhp==MHP){//总幸福相等时if(count<COUNT){//途径城市少,平均幸福指数就高BEST=it;COUNT=count;}}}cout<<result.size()<<" "<<COST<<" "<<MHP<<" "<<MHP/(COUNT-1)<<endl;int len=result[BEST].size();cout<<id2str[result[BEST][len-1]];for(int i=len-2;i>=0;i--)cout<<"->"<<id2str[result[BEST][i]];return 0;}


0 0
原创粉丝点击