PAT 1087. All Roads Lead to Rome (30)

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算法

  1 #include <iostream>
  2 #include <string>
  3 #include <map>
  4 #include <vector>
  5 #include <bitset>
  6 
  7 using namespace std;
  8 
  9 const int INF = 0x7fffffff;
 10 map<string, int> indexMap;
 11 vector<string> city;
 12 int happiness[200];
 13 int cityMap[200][200];
 14 int maxHappiness[200];
 15 int minCost[200];
 16 int cityPast[200];
 17 int minCostRoute[200];
 18 bitset<200> visited;
 19 int pre[200];
 20 
 21 int main()
 22 {
 23     int cityNum, routeNum;
 24     cin >> cityNum >> routeNum;
 25     string start;
 26     cin >> start;
 27     indexMap[start] = 0;
 28     city.push_back(start);
 29     for (int i = 1; i < cityNum; i++)
 30     {
 31         string c;
 32         cin >> c >> happiness[i];
 33         indexMap[c] = i;
 34         city.push_back(c);
 35     }
 36 
 37     for (int i = 0; i < cityNum; i++)    //initial the city map
 38         for (int j = 0; j < cityNum; j++)
 39             cityMap[i][j] = cityMap[j][i] = INF;
 40     for (int i = 0; i < routeNum; i++)
 41     {
 42         string c1, c2;
 43         int cost;
 44         cin >> c1 >> c2 >> cost;
 45         int index1 = indexMap[c1];
 46         int index2 = indexMap[c2];
 47         cityMap[index1][index2] = cityMap[index2][index1] = cost;
 48     }
 49     
 50     for (int i = 0; i < cityNum; i++)
 51     {
 52         maxHappiness[i] = 0;
 53         minCost[i] = INF;
 54     }
 55 
 56     //using dijkstra algorithm
 57     visited.set(0);
 58     int min = 0;
 59     maxHappiness[0] = 0;
 60     minCost[0] = 0;
 61     cityPast[0] = 0;
 62     minCostRoute[0] = 1;
 63     while (1)
 64     {
 65         for (int i = 0; i < cityNum; i++)
 66         {
 67             if (cityMap[min][i] < INF)
 68             {
 69                 if (minCost[min] + cityMap[min][i] < minCost[i])
 70                 {
 71                     pre[i] = min;
 72                     minCost[i] = minCost[min] + cityMap[min][i];
 73                     maxHappiness[i] = maxHappiness[min] + happiness[i];
 74                     cityPast[i] = cityPast[min] + 1;
 75                     minCostRoute[i] = minCostRoute[min];
 76                 }
 77                 else if (minCost[min] + cityMap[min][i] == minCost[i])
 78                 {
 79                     minCostRoute[i] += minCostRoute[min];
 80                     if (maxHappiness[min] + happiness[i] > maxHappiness[i])
 81                     {
 82                         pre[i] = min;
 83                         maxHappiness[i] = maxHappiness[min] + happiness[i];
 84                         cityPast[i] = cityPast[min] + 1;
 85                     }
 86                     else if (maxHappiness[min] + happiness[i] == maxHappiness[i])
 87                     {
 88                         if (cityPast[min] + 1 < cityPast[i])
 89                         {
 90                             pre[i] = min;
 91                             cityPast[i] = cityPast[min] + 1;
 92                         }
 93                     }
 94                 }
 95             }
 96         }
 97 
 98         int cost = INF;
 99         for (int j = 0; j < cityNum; j++)
100         {
101             if (!visited[j] && cost > minCost[j])
102             {
103                 cost = minCost[j];
104                 min = j;
105             }
106         }
107         visited.set(min);
108         if (cost==INF)
109             break;
110     }
111 
112     int ROMIndex = indexMap["ROM"];
113     cout << minCostRoute[ROMIndex] << " " << minCost[ROMIndex] << " " << maxHappiness[ROMIndex] << " "
114         << maxHappiness[ROMIndex] / cityPast[ROMIndex] << endl;
115     vector<string> vec;
116     for (int i = ROMIndex; i != 0; i = pre[i])
117         vec.push_back(city[i]);
118     vec.push_back(start);
119     for (vector<string>::reverse_iterator it = vec.rbegin(); it != vec.rend()-1; it++)
120         cout << *it << "->";
121     cout << vec.front();
122 }

 

转载于:https://www.cnblogs.com/jackwang822/p/4749861.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, based on the given problem, here is a class diagram that represents the entities and their relationships: ``` +-----------------+ +-----------------+ | Country |<>----------| City | +-----------------+ +-----------------+ | | | | | - name | | - name | | - cities | | - roads | | | | | | + addCity() | | + addRoad() | | + removeCity() | | + removeRoad() | +-----------------+ +-----------------+ ^ | | | | | +----------------------+ | TravelingUnit | +----------------------+ | | | - type | | - driver | | - wheels/legs | | - weapons | | - canFire | | - maxSpeed | | | | + move() | | + captureByBandit() | +----------------------+ ^ | | | | | +---------------------+ | Driver | +---------------------+ | | | - nationality | | - name | | | +---------------------+ ``` Explanation: - The `Country` class represents a country and has a relationship with multiple `City` objects through the `cities` attribute. It also has attributes like `name` and methods like `addCity()` and `removeCity()` for managing cities. - The `City` class represents a city and has a relationship with multiple `Road` objects through the `roads` attribute. It has attributes like `name` and methods like `addRoad()` and `removeRoad()` for managing roads. - The `TravelingUnit` class represents a traveling unit and contains attributes like `type`, `driver`, `wheels/legs`, `weapons`, `canFire`, and `maxSpeed`. It has methods like `move()` for moving the unit and `captureByBandit()` for capturing the unit. - The `Driver` class represents a driver and contains attributes like `nationality` and `name`. It is associated with the `TravelingUnit` class. Note: This is a basic representation of the system based on the given problem statement. There might be additional classes or relationships required depending on the specific requirements of the system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值