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

作者

CHEN, Yue

單位

浙江大學

代碼長度限制

16 KB

時間限制

200 ms

內存限制

64 MB

題解:

直接dijkstra 要保存更新的數據比較多.

#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 220;

unordered_map<string, int> mp;
int n, k;
int w[N], d[N][N], dist[N], cnt[N], cost[N], sum[N], pre[N];
string city[N];
bool st[N];

void Dijkstra() {
	memset(dist, 0x3f, sizeof(dist));
	dist[1] = 0, cnt[1] = 1;
	for (int i = 0; i < n; i++)
	{
		int t = -1;
		for (int j = 1; j <= n; j++)
			if (!st[j] && (t == -1 || dist[t] > dist[j]))
				t = j;
		st[t] = true;

		for (int j = 1; j <= n; j++)
		{
			if (dist[j] > dist[t] + d[t][j])
			{
				dist[j] = dist[t] + d[t][j];
				cnt[j] = cnt[t];
				cost[j] = cost[t] + w[j];
				sum[j] = sum[t] + 1;
				pre[j] = t;
			}
			else if (dist[j] == dist[t] + d[t][j])
			{
				cnt[j] += cnt[t];
				if (cost[j]<cost[t]+w[j])
				{
					cost[j] = cost[t] + w[j];
					sum[j] = sum[t] + 1;
					pre[j] = t;
				}
				else if (cost[j] == cost[t] + w[j])
				{
					if (sum[j] > sum[t] + 1) {
						sum[j] = sum[t] + 1;
						pre[j] = t;
					}
				}
			}
		}
	}
}

int main() {
	cin >> n >> k >> city[1];
	mp[city[1]] = 1;
	for (int i = 2; i <= n; i++)
	{
		cin >> city[i] >> w[i];
		mp[city[i]] = i;
	}
	memset(d, 0x3f, sizeof(d));
	for (int i = 0; i < k; i++)
	{
		string c1, c2;
		int c;
		cin >> c1 >> c2 >> c;
		d[mp[c1]][mp[c2]] = d[mp[c2]][mp[c1]] = c;
	}
	Dijkstra();

	int rom = mp["ROM"];
	cout << cnt[rom] << ' ' << dist[rom] << ' ' << cost[rom] << ' ' << cost[rom] / sum[rom] << endl;

	vector<int> path;
	for (int i = rom; i != 1; i = pre[i]) path.push_back(i);

	cout << city[1];
	for (int i = path.size() - 1; i >= 0; i--)
		cout << "->" << city[path[i]];
	cout << endl;
  return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章