PAT (Advanced Level) Practice 1087

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

分析:Dijkstra + DFS。用DFS得到所有最短路径前驱,储存在vector<int>的pre数组中。之后用DFS遍历所有前驱节点,寻找符合题意的路径。

代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#define MAXN 402
#define INF 0x3fffffff
using namespace std;
int G[MAXN][MAXN] = { 0 };
int d[MAXN];
int visit[MAXN] = { 0 };
int C[MAXN] = { 0 };
vector<int> pre[MAXN];
map<int, string> intToString;
map<string, int> stringToInt;
int N, K;
string starting;
int numCity = 0;
int S, D;
int num[MAXN];
vector<int> path, tempPath;
int mostHappiness = -1;
void dfs(int v) {
	tempPath.push_back(v);
	if (v == S) {
		int happiness = 0;
		for (int i = 0; i < tempPath.size(); i++) {
			happiness += C[tempPath[i]];
		}
		if (happiness > mostHappiness) {
			mostHappiness = happiness;
			path = tempPath;
		} else if (happiness == mostHappiness) {
			if (happiness / (tempPath.size() - 1) > mostHappiness / (path.size() - 1)) {
				path = tempPath;
			}
		}
	}
	for (int i = 0; i < pre[v].size(); i++) {
		dfs(pre[v][i]);
	}
	tempPath.pop_back();

}
void dijkstra() {
	for (int i = 0; i < N; i++) {
		num[i] = 1;
	}
	d[S] = 0;
	for (int i = 0; i < N; i++) {
		int u = -1, minN = INF;
		for (int j = 0; j < N; j++) {
			if (d[j] < minN && visit[j] == 0) {
				u = j;
				minN = d[j];
			}
		}
		if (u == -1) {
			return;
		}
		visit[u] = 1;
		for (int v = 0; v < N; v++) {
			if (G[u][v] != INF && visit[v] == 0) {
				if (d[v] > d[u] + G[u][v]) {
					d[v] = d[u] + G[u][v];
					num[v] = num[u];
					pre[v].clear();
					pre[v].push_back(u);
				} else if (d[v] == d[u] + G[u][v]) {
					num[v] += num[u];
					pre[v].push_back(u);
				}
			}
		}
	}
}
int getID(string name) {
	if (stringToInt.count(name) == 0) {
		stringToInt[name] = numCity;
		intToString[numCity] = name;
		return numCity++;
	} else {
		return stringToInt[name];
	}
}
int main() {
	fill(G[0], G[0] + MAXN * MAXN, INF);
	fill(d, d + MAXN, INF);
	cin >> N >> K >> starting;
	S = getID(starting);
	D = getID("ROM");
	getchar();
	for (int i = 0; i < N - 1; i++) {
		string City;
		int temp;
		cin >> City >> temp;
		int id = getID(City);
		C[id] = temp;
	}
	for (int i = 0; i < K; i++) {
		string City1, City2;
		int temp;
		cin >> City1 >> City2 >> temp;
		int id1 = getID(City1);
		int id2 = getID(City2);
		G[id1][id2] = G[id2][id1] = temp;
	}
	dijkstra();
	dfs(D);
	cout << num[D] << ' ' << d[D] << ' ' << mostHappiness << ' ' << mostHappiness / (path.size() - 1) << endl;
	for (int i = path.size() - 1; i >= 0; i--) {
		if (i != 0) {
			cout << intToString[path[i]] << "->";
		} else {
			cout << intToString[path[i]] << endl;
		}
	}
	return 0;
}

 

点赞