PAT 1087 All Roads Lead to Rome (30)

1087 All Roads Lead to Rome (30)(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的时候同时维护4个值(最小距离,最短路径条数,最大happiness值和经过城市数),还要记录最短路径。其实并不难,只是有点麻烦,要注意细节。有一个测试点过不去,暂时还没找到bug。找到一个思路和我十分相似的题解,可以过所有测试点,在这里码一下

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>  
#include <set>
using namespace std;

#define INF 0x3f3f3f3f

struct city
{
	int dis;   //到达该城市的最小距离
	int cnt;   //到达该城市的最短路径条数
	int maxh;  //到达该城市的最短路径对应的最大happiness
	int num;   //到达该城市的最短路径并且获得最大happiness所要经过的城市个数(不算起点)
	int vis;
	int pre;
};

map<string, int> s2i;
map<int, string> i2s;
vector<city> v;
int g[205][205], happ[205];

void dijkstra(int a,int n)
{
	for (int i = 0; i < n; i++)
	{
		v[i].dis = g[a][i];
		v[i].cnt = 0;
		v[i].maxh = 0;
		v[i].num = 0;
		v[i].vis = 0;
		v[i].pre = -1;
	}
	v[a].dis = 0;
	v[a].cnt = 1;
	for (int i = 0; i < n; i++)
	{
		int min = INF, k = -1;
		for (int j = 0; j < n; j++)
		{
			if (!v[j].vis && v[j].dis < min)
			{
				min = v[j].dis;
				k = j;
			}
		}
		if (k == -1)
			return;
		v[k].vis = 1;
		for (int j = 0; j < n; j++)
		{
			if (!v[j].vis && g[k][j] != INF)
			{
				if (v[k].dis + g[k][j] < v[j].dis)
				{
					v[j].dis = v[k].dis + g[k][j];
					v[j].cnt = v[k].cnt;
					v[j].maxh = v[k].maxh + happ[j];
					v[j].num = v[k].num + 1;
					v[j].pre = k;
				}
				else if (v[k].dis + g[k][j] == v[j].dis)
				{
					v[j].cnt += v[k].cnt;
					if (v[k].maxh + happ[j] > v[j].maxh)
					{
						v[j].maxh = v[k].maxh + happ[j];
						v[j].num = v[k].num + 1;
						v[j].pre = k;
					}
					else if (v[k].maxh + happ[j] == v[j].maxh)
					{
						if ((1.0*(v[k].maxh + happ[j]) / (v[k].cnt + 1)) > (1.0*v[j].maxh / v[j].cnt))
						{
							v[j].num = v[k].num + 1;
							v[j].pre = k;
						}
					}
				}
			}
		}
	}
}

void print_path(int x, int start)
{
	if (x == start)
		return;
	print_path(v[x].pre, start);
	cout << "->" << i2s[x];
}

int main()
{
	int n, k;
	cin >> n >> k;
	v.resize(n);
	string city1, city2;
	cin >> city1;
	s2i[city1] = 0;
	i2s[0] = city1;
	happ[0] = 0;
	for (int i = 1; i < n; i++)
	{
		cin >> city1;
		s2i[city1] = i;
		i2s[i] = city1;
		cin >> happ[i];
	}
	memset(g, INF, sizeof(g));
	for (int i = 0; i < k; i++)
	{
		cin >> city1 >> city2;
		int a = s2i[city1], b = s2i[city2];
		cin >> g[a][b];
		g[b][a] = g[a][b];
	}
	dijkstra(0, n);
	int id = s2i["ROM"];
	cout << v[id].cnt << " " << v[id].dis << " " << v[id].maxh << " " << v[id].maxh / v[id].num << endl;
	cout << i2s[0];
	print_path(id, 0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ryo_218/article/details/82465591