1087 All Roads Lead to Rome (30)

Posted mr-stn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 1

Sample Output:

3 3 195 97
HZH->PRS->ROM
 题目大意:找到花费最少,幸福值最高的路径
思路:用dijkstra算法找到花费最少的路径, 然后再用dfs找幸福值最高的路径
 1 #include<iostream>
 2 #include<vector>
 3 #include<map>
 4 using namespace std;
 5 const int inf=99999999;
 6 int cost[205][205], weight[205], w[205];
 7 vector<int> pre[205], temp, path, vis(205, false);
 8 map<string, int> index;
 9 map<int, string> index1;
10 int maxavg=-1, maxx=-1, cnt=0, money=0;
11 void dfs(int v){
12   temp.push_back(v);
13   if(v==0){
14     cnt++;
15     int tempsum=0, avg=0;
16     for(int j=0; j<temp.size()-1; j++) tempsum += weight[temp[j]];
17     avg = tempsum/(temp.size()-1);
18     if(tempsum>maxx){
19       path=temp;
20       maxx = tempsum;
21       maxavg = avg;
22     }else if(tempsum==maxx&&avg>maxavg){
23       path=temp;
24       maxavg = avg;
25     }
26     temp.pop_back();
27     return;
28   }
29   for(int i=0; i<pre[v].size(); i++) dfs(pre[v][i]);
30   temp.pop_back();
31 }
32 int main(){
33   int n, k, i, happiness, c;
34   string s, s1, s2;
35   fill(cost[0], cost[0]+205*205, inf);
36   fill(w, w+205, inf);
37   cin>>n>>k>>s;
38   index[s]=0;  index1[0]=s;
39   for(i=1; i<n; i++){
40     cin>>s1>>happiness;
41     weight[i]=happiness;
42     index[s1]=i;
43     index1[i]=s1;
44   }
45   for(i=0; i<k; i++){
46     cin>>s1>>s2>>c;
47     int ia=index[s1], ib=index[s2];
48     cost[ia][ib]=cost[ib][ia]=c;
49   }
50   pre[0].push_back(0);   w[0]=0;
51   for(i=0; i<n; i++){
52     int u=-1, minn=inf;
53     for(int j=0; j<n; j++){
54       if(!vis[j] && w[j]<minn){
55         minn=w[j];
56         u=j;
57       }
58     }
59     if(u==-1) break;
60     vis[u] = true;
61     for(int v=0; v<n; v++){
62       if(!vis[v] && cost[u][v]!=inf){
63         if(w[v]>w[u]+cost[u][v]){
64           w[v]=w[u]+cost[u][v];
65           pre[v].clear();
66           pre[v].push_back(u);
67         }else if(w[v]==w[u]+cost[u][v]){
68           pre[v].push_back(u);
69         }
70       }
71     }
72   }
73   dfs(index["ROM"]);
74   for(i=path.size()-1; i>0; i--) money += cost[path[i]][path[i-1]];
75   cout<<cnt<<" "<<money<<" "<<maxx<<" "<<maxavg<<endl;
76   for(i=path.size()-1; i>=0; i--){
77     if(i==path.size()-1) cout<<index1[path[i]];
78     else cout<<"->"<<index1[path[i]];
79   }
80   return 0;
81 }

 

以上是关于1087 All Roads Lead to Rome (30)的主要内容,如果未能解决你的问题,请参考以下文章

PAT1087. All Roads Lead to Rome

1087. All Roads Lead to Rome (30)最短路——PAT (Advanced Level) Practise

PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

1087 All Roads Lead to Rome (30 分)难度: 一般 / Dijkstra

PAT (Advanced Level) 1087 All Roads Lead to Rome

(学英语学算法)PAT甲级--All Roads Lead to Rome