标签:
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
思路:Dij+DFS
最少代价------第一尺度
最多快乐------第二尺度
最大的平均距离------第三尺度
其中本程序第一尺度在Dij中完成,第二和第三尺度在DFS中完成。
1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <vector> 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <algorithm> 9 using namespace std; 10 #define MAX 210 11 const int INF=0x3fffffff; 12 int G[MAX][MAX]; 13 bool visited[MAX]; 14 int d[MAX]; 15 int weight[MAX]; 16 int N,K; 17 string str; 18 map<string,int>str2num; 19 map<int,string>num2str; 20 int cnt=0; 21 int numpath=0; 22 int start,des; 23 vector<int>pre[MAX]; 24 vector<int>tempath,path; //用来解决DFS的遍历问题 25 int hapiness=-1; 26 double avghap=-1; 27 28 int GetId(string str) 29 { 30 if(str2num.find(str)!=str2num.end()) 31 return str2num[str]; 32 else 33 { 34 num2str[cnt]=str; 35 str2num[str]=cnt; 36 return cnt++; 37 } 38 } 39 void Dij() 40 { 41 d[start]=0; 42 //需要进行初始化pre数组???不过这是为什么???? 43 44 for(int i=0;i<N;i++) 45 { 46 int u=-1,min=INF; 47 for(int j=0;j<N;j++) 48 { 49 if(d[j]<min&&!visited[j]) 50 { 51 min=d[j]; 52 u=j; 53 } 54 } 55 if(u==-1) 56 return ; 57 visited[u]=true; 58 for(int v=0;v<N;v++) 59 { 60 if(!visited[v]&&G[u][v]!=INF) 61 { 62 if(d[u]+G[u][v]<d[v]) 63 { 64 pre[v].clear(); 65 pre[v].push_back(u); 66 d[v]=G[u][v]+d[u]; 67 } 68 else if(d[u]+G[u][v]==d[v]) 69 { 70 pre[v].push_back(u); 71 } 72 } 73 } 74 } 75 } 76 void DFS(int id) 77 { 78 if(id==start) 79 { 80 numpath++; 81 double wei=0; 82 tempath.push_back(id); 83 for(int i=0;i<tempath.size();i++) 84 { 85 wei+=weight[tempath[i]]; 86 } 87 if(wei>hapiness) 88 { 89 hapiness=wei; 90 avghap=(wei*1.0)/(tempath.size()-1); 91 path=tempath; 92 } 93 else if(wei==hapiness) 94 { 95 if((wei*1.0)/(tempath.size()-1)>avghap) 96 { 97 avghap=(wei*1.0)/(tempath.size()-1); 98 path=tempath; 99 } 100 } 101 tempath.pop_back(); 102 return; 103 } 104 tempath.push_back(id); 105 for(int i=0;i<pre[id].size();i++) 106 { 107 DFS(pre[id][i]); 108 } 109 tempath.pop_back(); 110 } 111 112 int main(int argc, char *argv[]) 113 { 114 cin>>N>>K>>str; 115 fill(G[0],G[0]+MAX*MAX,INF); 116 fill(d,d+MAX,INF); 117 memset(visited,false,sizeof(visited)); 118 start=GetId(str); 119 des=GetId("ROM"); 120 weight[start]=0; 121 for(int i=0;i<N-1;i++) 122 { 123 string tem; 124 int hapiness; 125 cin>>tem>>hapiness; 126 int id=GetId(tem); 127 weight[id]=hapiness; 128 } 129 for(int i=0;i<K;i++) 130 { 131 string str1,str2; 132 int length; 133 cin>>str1>>str2>>length; 134 int id1=GetId(str1); 135 int id2=GetId(str2); 136 G[id1][id2]=G[id2][id1]=length; 137 } 138 Dij(); 139 DFS(des); 140 printf("%d %d %d %d\n",numpath,d[des],hapiness,(int)avghap); 141 142 for(int i=path.size()-1;i>=0;i--) 143 { 144 string tem=num2str[path[i]]; 145 cout<<tem; 146 if(i!=0) 147 cout<<"->"; 148 else 149 cout<<endl; 150 } 151 return 0; 152 }
PAT1087. All Roads Lead to Rome
标签:
原文地址:http://www.cnblogs.com/GoFly/p/4306645.html