Not Only Algorithm,不仅仅是算法,关注数学、算法、数据结构、程序员笔试面试以及一切涉及计算机编程之美的内容 。。
你的位置:NoAlGo博客 » 题解 » 

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

解答

#include <cstdio>
#include <climits>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

int main()
{
	int n, m; scanf("%d%d", &n, &m);
	char tem[10]; scanf("%s", tem); string src(tem);

	map<string, int> name2num; 
	map<int, string> num2name;
	name2num[src] = 0; num2name[0] = src;

	const int mx = 210;
	int w[mx] = {0}, g[mx][mx] = {0};

	for (int i = 1; i < n; i++)
	{
		char t[10]; int tw; scanf("%s%d", t, &tw); string s(t);
		name2num[s] = i; num2name[i] = s; w[i] = tw;
	}

	for (int i = 0; i < m; i++)
	{
		char t1[10], t2[10]; int tc; scanf("%s%s%d", t1, t2, &tc);
		int n1 = name2num[string(t1)], n2 = name2num[string(t2)];
		g[n1][n2] = g[n2][n1] = tc;
	}

	int dis[mx], vs[mx] = {0}, pre[mx], sum[mx] = {0}, cnt[mx] = {0}, mncnt[mx] = {0};
	for (int i = 0; i < n; i++)
		dis[i] = g[0][i] ? g[0][i] : 1000000000, vs[i] = 0, pre[i] = 0, sum[i] = w[i], cnt[i] = 1, mncnt[i] = 1;

	dis[0] = 0, vs[0] = 1, pre[0] = -1, cnt[0] = 1, mncnt[0] = 1;
	for (int i = 1; i < n; i++)
	{
		int mn = INT_MAX, id;
		for (int j = 0; j < n; j++) if (!vs[j] && dis[j] < mn) mn = dis[j], id = j;

		vs[id] = 1;
		for (int j = 0; j < n; j++)
			if (g[id][j] && !vs[j])
			{
				bool flag = 0;
				if (dis[id] + g[id][j] < dis[j]) //距离更短
					flag = 1, mncnt[j] = mncnt[id]; //最短路数量不变
				else if(dis[id] + g[id][j] == dis[j]) //距离相同
				{
					mncnt[j] += mncnt[id]; //最短路数量增加
					if (sum[id] + w[j] > sum[j] || (sum[id]+w[j]==sum[j] && cnt[id]+1<cnt[j])) //权值和更大 || 权值和相同&&长度更小
							flag = 1;
				}
				if (flag)
				{
					dis[j] = dis[id] + g[id][j];
					sum[j] = sum[id] + w[j];
					cnt[j] = cnt[id] + 1;
					pre[j] = id;
				}
			}
	}

	int dst = name2num["ROM"];
	printf("%d %d %d %d\n", mncnt[dst], dis[dst], sum[dst], sum[dst]/cnt[dst]);

	stack<int> stk;	stk.push(dst);
	while (pre[stk.top()] != -1) stk.push(pre[stk.top()]);

	printf("%s", num2name[stk.top()].c_str()); stk.pop();
	while (!stk.empty())
	{
		printf("->%s", num2name[stk.top()].c_str()); stk.pop();
	}
	printf("\n");

	return 0;
}
上一篇: 下一篇:

我的博客

NoAlGo头像编程这件小事牵扯到太多的知识,很容易知其然而不知其所以然,但真正了不起的程序员对自己程序的每一个字节都了如指掌,要立足基础理论,努力提升自我的专业修养。

站内搜索

最新评论