PAT 甲级 1087 All Roads Lead to Rome

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遍历所有最短路径是比较各个因素,用map存储每个城市对于的编号,直接将城市按照1-N进行编号也行。

代码:

#include<cstdio>
#include<map>
#include<vector>
#include<iostream>
#include<string>
using namespace std;
const int INF = 100000000;
const int MAXV = 30000;
struct node {
	int n;
	int dis;
};
int N, K, S;
string start, END;
int change(string s) {
	int ans = (s[0] - 'A') * 26 * 26 + (s[1] - 'A') * 26 + (s[2] - 'A');
	return ans;
}
int happiness[30000];
int vis[MAXV] = { 0 };
int d[MAXV];
map<string, int> s_int;
map<int, string> int_s;
vector<node> Adj[MAXV];
vector<int> pre[MAXV];
void Dijkstra(int s) {
	fill(d, d + MAXV, INF);
	d[s] = 0;
	for (int i = 0; i < N; i++) {
		int u = -1, MIN = INF;
		for (int j = 0; j < 30000; j++) {
			if (vis[j] == 0 && d[j] < MIN) {
				MIN = d[j];
				u = j;
			}
		}
		if (u == -1)	return;
		vis[u] = 1;
		for (int j = 0; j < Adj[u].size(); j++) {
			node v = Adj[u][j];
			if (d[u] + v.dis < d[v.n]) {
				d[v.n] = d[u] + v.dis;
				pre[v.n].clear();
				pre[v.n].push_back(u);
			}
			else if (d[u] + v.dis == d[v.n]) {
				pre[v.n].push_back(u);
			}
		}
	}
}
vector<int> temppath, path;
int maxhappiness = 0;
int sumroad = 0;
int avghappiness = 0;
void DFS(int s, int v) {
	int tempsum = 0, tempavg = 0;
	if (s == v) {
		sumroad++;
		temppath.push_back(v);
		for (int i = 0; i < temppath.size(); i++) {
			int pos = temppath[i];
			tempsum += happiness[pos];
		}
		if (tempsum > maxhappiness) {
			maxhappiness = tempsum;
			path = temppath;
			avghappiness = tempsum / (temppath.size() - 1);
		}
		else if (tempsum == maxhappiness) {
			tempavg = tempsum / (temppath.size() - 1);
			if (tempavg > avghappiness) {
				avghappiness = tempavg;
				path = temppath;
			}
		}
		temppath.pop_back();
	}
	temppath.push_back(v);
	for (int i = 0; i < pre[v].size(); i++) {
		DFS(s, pre[v][i]);
	}
	temppath.pop_back();
}
int main(void) {
	cin >> N >> K >> start;
	int S = change(start);
	int E = change("ROM");
	int_s[E] = "ROM";
	int_s[S] = start;
	happiness[S] = 0;
	for (int i = 0; i < N - 1; i++) {
		string temp;
		int hap;
		cin >> temp >> hap;
		int pos = change(temp);
		happiness[pos] = hap;
		int_s[pos] = temp;
	}
	for (int i = 0; i < K; i++) {
		string A, B;
		int length;
		cin >> A >> B >> length;
		int a = change(A);
		int b = change(B);
		node Node;
		Node.n = b;
		Node.dis = length;
		Adj[a].push_back(Node);
		Node.n = a;
		Node.dis = length;
		Adj[b].push_back(Node);
	}
	Dijkstra(S);
	DFS(S,E);
	printf("%d %d %d %d\n", sumroad, d[E], maxhappiness, avghappiness);
	for (int i = path.size() - 1; i >= 0; i--) {
		int v = path[i];
		cout << int_s[v];
		if (i != 0)
			cout << "->";
	}
}
发布了69 篇原创文章 · 获赞 5 · 访问量 2036

猜你喜欢

转载自blog.csdn.net/lovingcgq/article/details/104731251
今日推荐