PAT 1087.All Roads Lead to Rome

1087. All Roads Lead to Rome (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

从指定起点开始进行深度优先遍历,若遍历至终点判断该结果是否符合条件进行计数。

最佳路径条件为:

1.长度即cost最小

2.cost相等,happyness总和最大

3.happyness总和也相等,happyness总和/(节点总数-1)最大,即经过节点数最少

需要记录符合条件1的记录总数,路径只输出最佳路径

节点名与编号通过map<string,int>和vector<string>来记录

 1 #include<iostream>
 2 #include<string>
 3 #include<map>
 4 #include<vector>
 5 
 6 using namespace std;
 7 
 8 int n, k;
 9 map<string, int> list;
10 int happy[200];
11 int road[200][200];
12 bool choose[200];
13 vector<int> path, minpath;
14 vector<string> name;
15 int cost, sumhappy, mincost, minsumhappy, minnum;
16 int rom;
17 
18 void dfs(int start){
19     choose[start] = true;
20     path.push_back(start);
21     if (start == rom){
22         if (cost < mincost){
23             mincost = cost;
24             minnum = 1;
25             minsumhappy = sumhappy;
26             minpath = path;
27         }
28         else if (cost == mincost){
29             minnum++;
30             if (sumhappy>minsumhappy || (sumhappy == minsumhappy&&path.size() < minpath.size())){
31                 minsumhappy = sumhappy;
32                 minpath = path;
33             }
34         }
35     }
36     else{
37         int i;
38         for (i = 1; i < n; i++){
39             if (!choose[i] && road[start][i] != 0){
40                 cost += road[start][i];
41                 sumhappy += happy[i];
42                 dfs(i);
43                 sumhappy -= happy[i];
44                 cost -= road[start][i];
45             }
46         }
47     }
48     path.pop_back();
49     choose[start] = false;
50 }
51 
52 int main(void){
53     cin >> n >> k;
54     int i;
55     mincost = 99999;
56     string city;
57     cin >> city;
58     name.push_back(city);
59     list[city] = 0;
60     for (i = 1; i < n; i++){
61         cin >> city >> happy[i];
62         name.push_back(city);
63         list[city] = i;
64         if (city == "ROM"){
65             rom = i;
66         }
67     }
68     string city1, city2;
69     int num1, num2;
70     int cost;
71     for (i = 0; i < k; i++){
72         cin >> city1 >> city2 >> cost;
73         num1 = list[city1];
74         num2 = list[city2];
75         road[num1][num2] = cost;
76         road[num2][num1] = cost;
77     }
78     dfs(0);
79     cout << minnum << " " << mincost << " " << minsumhappy << " " << minsumhappy / (minpath.size() - 1) << endl;
80     cout << name[0];
81     for (i = 1; i < minpath.size(); i++){
82         cout << "->" << name[minpath[i]];
83     }
84     cout << endl;
85     return 0;
86 }

 

posted @ 2018-01-20 11:45  GrayWind  阅读(115)  评论(0)    收藏  举报