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.ios
Input Specification:api
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:app
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.spa
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".code
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
分析:這是一道圖的遍歷題目,可是涉及到多個判斷標準。首先,路徑長度(花費)最小。其次,得到的happy值最大。再次,happy的平均值最大(不包括開始的城市).雖然增長了判斷標準,可是作法仍是同樣的。該題基於圖的深度遍歷DFS來作,每次遍歷到目的節點時更新判斷標準。另外,圖的DFS與數的DFS不一樣的地方在於,圖的DFS須要增長一個vis數組用於表示某個節點是否訪問過,而樹不須要,由於樹是不含環的。另外,該題給的是字符串表示的節點,咱們能夠用map來實現字符串和int型之間的映射,方便編寫代碼。orm
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <map> using namespace std; const int maxn=210; const int INF=1e9; int g[maxn][maxn]; int happy[maxn]; int vis[maxn]={false}; map<string ,int> str2int; map<int,string> int2str; int st,ed; vector<int> tmppath,path; int min_cost=INF; int max_happy=0; int avg_happy=0; int lest_num=0; int n; void dfs(int s,int cost,int hy) { vis[s]=true; if(s==ed) { if(cost<min_cost) { min_cost=cost; path=tmppath; lest_num=1; max_happy=hy; avg_happy=hy/(path.size()-1); } else if(cost==min_cost) { lest_num+=1; if(hy>max_happy) { max_happy=hy; path=tmppath; avg_happy=hy/(path.size()-1); } else if(hy==max_happy) { if(tmppath.size()<path.size()&&tmppath.size()>1) { path=tmppath; int down=path.size()-1; avg_happy=hy/down; } } } return ; } for(int v=0;v<n;v++) { if(vis[v]==false&&g[s][v]!=INF) { tmppath.push_back(v); dfs(v,cost+g[s][v],hy+happy[v]); vis[v]=false; tmppath.pop_back(); } } } int main() { fill(g[0],g[0]+maxn*maxn,INF); int k; string begin; cin>>n>>k>>begin; str2int.insert(make_pair(begin,0)); int2str.insert(make_pair(0,begin)); for(int i=1;i<n;i++) { string str; int h; cin>>str>>h; if(str=="ROM") ed=i; str2int.insert(make_pair(str,i)); int2str.insert(make_pair(i,str)); happy[i]=h; } for(int i=0;i<k;i++) { string u,v; int cost; cin>>u>>v>>cost; int uu,vv; uu=str2int[u]; vv=str2int[v]; g[uu][vv]=g[vv][uu]=cost; } tmppath.push_back(0); dfs(0,0,0); cout<<lest_num<<" "<<min_cost<<" "<<max_happy<<" "<<avg_happy<<endl; for(int i=0;i<path.size();i++) { if(i>0) cout<<"->"; cout<<int2str[path[i]]; } }