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 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
解题思路:
利用迪杰斯特拉算法求得起点到ROM的最短路径,利用vector保留每个结点的前驱路径(可能有多个)
然后利用DFS从ROM开始回溯到起点,求得路径的条数,互相比较得出幸福度最高的那条路径
这里注意平均值要用double来保存,在每次得到最短路径后求平均路径,取平均值最大得那条,输出时要转int
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
using namespace std;
const int INF = 0x3fffffff;
const int MAXV = 210;
int N, K, startS;
int RG[MAXV][MAXV];
int Rd[MAXV],Rh[MAXV];
bool Rvisited[MAXV] = { false };
int maxHappiness = 0;
int optPathes = 0;
double aveHappy = 0.0;
//保留路径操作
vector<int> Rpres[MAXV];
vector<int> bestPath, tempPath; //用来回溯保留
//Dijkstra算法
void RDijkstra(int s) {
fill(Rd, Rd + MAXV, INF);
Rd[s] = 0;
for (int i = 0; i < N; ++i) {
int u = -1, MMin = INF;
for (int j = 0; j < N; ++j) {
if (!Rvisited[j] && Rd[j] < MMin) {
MMin = Rd[j];
u = j;
}
}
if (u == -1) return;
Rvisited[u] = true;
for (int v = 0; v < N; ++v) {
if (!Rvisited[v] && RG[u][v] != INF) {
if (RG[u][v] + Rd[u] < Rd[v]) {
Rd[v] = RG[u][v] + Rd[u];
Rpres[v].clear();
Rpres[v].push_back(u); //更改前驱结点
}
else if (RG[u][v] + Rd[u] == Rd[v]) {
Rpres[v].push_back(u); //增加前驱结点
}
}
}
}
}
void RDFS(int v) {
if (v == startS) {
tempPath.push_back(v);
int opthappy = 0;
optPathes++;
for (int i = tempPath.size() - 1; i >= 0; --i) {
opthappy += Rh[tempPath[i]];
}
double tempAve = 1.0*opthappy / (tempPath.size()-1);
if (opthappy > maxHappiness) {
maxHappiness = opthappy;
bestPath = tempPath;
aveHappy = tempAve;
}
tempPath.pop_back();
return;
}
tempPath.push_back(v);
for (int i = 0; i < Rpres[v].size(); ++i) {
RDFS(Rpres[v][i]);
}
tempPath.pop_back();
}
int main() {
fill(RG[0], RG[0] + MAXV * MAXV, INF);
map<string, int> strTonode;
map<int, string> nodeToStr;
string sName;
cin >> N >> K >> sName;
strTonode[sName] = 0; //从0开始
nodeToStr[0] = sName;
Rh[0] = 0;
string cityName;
int nhappy;
for (int i = 1; i < N; ++i) {
cin >> cityName >> nhappy;
strTonode[cityName] = i;
nodeToStr[i] = cityName;
Rh[i] = nhappy;
}
string city1, city2;
int cost;
for (int i = 0; i < K; ++i) {
cin >> city1 >> city2 >> cost;
RG[strTonode[city1]][strTonode[city2]] = cost;
RG[strTonode[city2]][strTonode[city1]] = cost;
}
//开始寻路
startS = 0;
RDijkstra(startS);
RDFS(strTonode["ROM"]);
printf("%d %d %d %d\n",Rd[strTonode["ROM"]], optPathes, maxHappiness, (int)aveHappy);
for (int i = bestPath.size() - 1; i >= 0; --i) {
cout << nodeToStr[bestPath[i]];
if (i > 0) cout << "->";
}
system("PAUSE");
return 0;
}