当前位置 : 主页 > 手机开发 > ROM >

PAT甲级——A1087 All Roads Lead to Rome【30】

来源:互联网 收集:自由互联 发布时间:2021-06-10
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 eac

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), 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 N1 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 recommanded. 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 recommanded 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 #include<iostream>
 2 #include<string>
 3 #include<unordered_map>
 4 using namespace std;
 5 int graph[205][205];
 6 int city[205], dis[205], happiness[205];//每个城市的幸福值、到达每个城市时的距离和幸福值
 7 int pathnum[205], past[205], pastnum[205];//到达每个城市时的最短路径条数、每个城市的前驱城市、到达每个城市前经过的城市
 8 bool visit[205];//每个城市是否被访问过
 9 unordered_map<string, int>STOI;//将城市字符串映射到整数
10 string ITOS[205];//将整数映射到字符串
11 int N, K;
12 void Dijkstra(int vend) {
13     while (!visit[vend]) {//当未遍历到终点城市时
14         int v = -1, MIN = INT_MAX;
15         for (int i = 0; i < N; ++i)//找出当前未被访问的距离最小的城市
16             if (!visit[i] && MIN > dis[i]) {
17                 v = i;
18                 MIN = dis[i];
19             }
20         if (v == -1)//图不连通直接返回
21             return;
22         visit[v] = true;//当前城市已访问
23         for (int i = 0; i < N; ++i)//遍历当前城市能到达的城市
24             if (!visit[i] && graph[v][i] != 0 && dis[i] > dis[v] + graph[v][i]) {//能到达的当前城市未被访问过且距离可更新
25                 dis[i] = dis[v] + graph[v][i];//更新到达该城市的距离
26                 happiness[i] = happiness[v] + city[i];//更新到达该城市的幸福值
27                 pastnum[i] = pastnum[v] + 1;//更新到达该城市前遍历过的城市数
28                 past[i] = v;//更新到达该城市的前驱城市
29                 pathnum[i] = pathnum[v];//更新到达该城市的最短路径条数
30             }
31             else if (graph[v][i] != 0 && dis[i] == dis[v] + graph[v][i]) {//到达该城市时的距离与该城市储存的距离相等
32                 pathnum[i] += pathnum[v];//更新到达该城市的最短路径条数
33                 if (happiness[i] < happiness[v] + city[i] || (happiness[i] == happiness[v] + city[i] && pastnum[i] > pastnum[v] + 1)) {
34                     past[i] = v;//更新到达该城市的前驱城市
35                     happiness[i] = happiness[v] + city[i];//更新到达该城市的幸福值
36                     pastnum[i] = pastnum[v] + 1;//更新到达该城市的最短路径条数
37                 }
38             }
39     }
40 }
41 void DFS(int v) {
42     if (v == 0) {
43         cout << ITOS[v];
44         return;
45     }
46     DFS(past[v]);
47     cout << "->" << ITOS[v];
48 }
49 int main() {
50     scanf("%d%d", &N, &K);
51     cin >> ITOS[0];
52     STOI.insert({ ITOS[0],0 });
53     for (int i = 1; i < N; ++i) {
54         cin >> ITOS[i];
55         STOI.insert({ ITOS[i],i });
56         cin >> city[i];
57     }
58     while (K--) {
59         int a;
60         string s1, s2;
61         cin >> s1 >> s2 >> a;
62         graph[STOI[s1]][STOI[s2]] = graph[STOI[s2]][STOI[s1]] = a;
63     }
64     int vend = STOI["ROM"];//将ROM设置为终点城市
65     fill(dis + 1, dis + N, INT_MAX);//距离初始化为INT_MAX
66     pathnum[0] = 1;//起点城市最短路径条数设置为1
67     Dijkstra(vend);
68     printf("%d %d %d %d\n", pathnum[vend], dis[vend], happiness[vend], happiness[vend] / pastnum[vend]);
69     DFS(vend);
70     return 0;
71 }
网友评论