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 1
Sample Output:
3 3 195 97
HZH->PRS->ROM
//Dijkstra 算法 #include<cstdio> #include<iostream> #include<algorithm> #include<map> #include<cstring> #include<string> using namespace std; const int maxn = 210; const int INF = 1000000000; int n,k,st; int G[maxn][maxn],d[maxn],weight[maxn]; int w[maxn],pt[maxn],pre[maxn],num[maxn]; bool vis[maxn] = {false}; map<string,int> cityToindex; map<int,string> indexTocity; void Dijkstra(int s){ fill(d,d+maxn,INF); memset(w,0,sizeof(w)); memset(num,0,sizeof(num)); memset(pt,0,sizeof(pt)); for(int i = 0; i < n; i++) pre[i] = i; d[s] = 0; num[s] = 1; w[s] = weight[s]; //s可以换成0 for(int i = 0 ; i < n; i++){ int u = -1, MIN = INF; for(int j = 0; j < n; j++){ if(vis[j] == false && MIN > d[j]){ u = j; MIN = d[j]; } } if( u == -1) return ; vis[u] = true; //错误1 for(int v = 0; v < n; v++){ if(vis[v] == false && G[u][v] != INF){ if(d[v] > d[u] + G[u][v]){ d[v] = d[u] + G[u][v]; w[v] = w[u] + weight[v]; num[v] = num[u]; pt[v] = pt[u] + 1; pre[v] = u; }else if(d[v] == d[u] + G[u][v]){ num[v] += num[u]; if(w[v] < w[u] + weight[v]){ w[v] = w[u] + weight[v]; pt[v] = pt[u] + 1; pre[v] = u; }else if(w[v] == w[u] + weight[v]){ //疑问1 double uAvg = 1.0 * (weight[v] + w[u]) / (pt[u] + 1); //错误2 double vAvg = 1.0 * w[v] / pt[v] ; if(uAvg > vAvg){ pt[v] = pt[u] + 1; pre[v] = u; } } } } } } } void printPath(int v) { if(v == 0){ cout << indexTocity[v]; return; } printPath(pre[v]); cout << "->" << indexTocity[v]; } int main(){ string city1,city2,start; cin >> n >> k >> start; cityToindex[start] = 0; indexTocity[0] = start; for(int i = 1; i <= n-1; i++){ cin >> city1 >> weight[i]; cityToindex[city1] = i; indexTocity[i] = city1; } fill(G[0],G[0]+maxn*maxn,INF); for(int i = 0; i < k; i++){ cin >> city1 >> city2; int c1 = cityToindex[city1], c2 = cityToindex[city2]; cin >> G[c1][c2]; G[c2][c1] = G[c1][c2]; } Dijkstra(0); int rom = cityToindex["ROM"]; //输出的分别是,最少花费路径条数,最短花费,幸福值(权重),平均权重 printf("%d %d %d %d ",num[rom],d[rom],w[rom],w[rom]/pt[rom]); printPath(rom); return 0; }
//dijkstra+DFS #include<cstdio> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<map> using namespace std; const int maxn = 210; const int INF = 1000000000; int n,k; int weight[maxn],G[maxn][maxn],d[maxn],w[maxn]; map<string,int> cityToindex; map<int,string>indexTocity; bool vis[maxn] = {false}; vector<int> pre[maxn]; vector<int> temp,path; int num = 0,maxW = 0; double maxAvg = 0; void Dijkstra(int s){ fill(d,d+maxn,INF); // 不用队前驱数组初始化。 d[s] = 0; for(int i = 0; i < n; i++){ int u = -1,MIN = INF; for(int j = 0; j < n; j++){ if(vis[j] == false && MIN > d[j]){ u = j; MIN = d[j]; } } if(u == -1) return; vis[u] = true; for(int v = 0; v < n; v++){ if(vis[v] == false && G[u][v] != INF){ if(d[v] > d[u] + G[u][v]){ d[v] = d[u] + G[u][v]; pre[v].clear(); pre[v].push_back(u); }else if(d[v] == d[u] + G[u][v]){ pre[v].push_back(u); } } } } } void DFS(int v){ if(v == 0){ temp.push_back(v); num++; // 最短路径条数 int tempW = 0; for(int i = temp.size() - 1; i >= 0; i--){ int id = temp[i]; tempW += weight[id]; //错误1 id } double tempAvg = 1.0 * tempW / (temp.size() - 1); //点权最大,若相同,选平均点权最大的 if(tempW > maxW){ maxW = tempW; maxAvg = tempAvg; path = temp; }else if(tempW == maxW && tempAvg > maxAvg){ maxAvg = tempAvg; path = temp; } temp.pop_back(); return; } temp.push_back(v); for(int i = 0; i < pre[v].size(); i++){ DFS(pre[v][i]); } temp.pop_back(); } int main(){ string start,city1,city2; cin >> n >> k >> start; cityToindex[start] = 0; indexTocity[0] = start; for(int i = 1; i <= n-1; i++){ cin >> city1 >> weight[i]; cityToindex[city1] = i; indexTocity[i] = city1; } fill(G[0],G[0]+maxn*maxn,INF); for(int i = 0; i < k; i++){ cin >> city1 >> city2; int c1 = cityToindex[city1],c2 = cityToindex[city2]; cin >> G[c1][c2]; G[c2][c1] = G[c1][c2]; } Dijkstra(0); int rom = cityToindex["ROM"]; DFS(rom); printf("%d %d %d %d ",num,d[rom],maxW,(int)maxAvg); for(int i = path.size() - 1; i >= 0; i--){ cout << indexTocity[path[i]]; if(i > 0) cout << "->"; } return 0; }