1087 All Roads Lead to Rome

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.

题目大意:现在要前往罗马,给定出发点,要求找出代价最小的一条路,如果不止一条,则找出获得最大快乐的路线,若还有多种选择,则找到经过最少个数的城市。对了,还要记录满足代价最小的路线的个数。总之四个要求,最小代价、最多快乐、最少城市、代价最小的路线个数。只要把这四个条件弄明白,并想出对应解决方法,那么问题也就迎刃而解了。

分析:很显然,这是一个Dijkstra算法的应用,只是使用过程中,需要考虑的条件过多罢了!
代价最小当然很容易求得,再加一个获得最大快乐的条件,此时再加一个记录就行。然后又有经过城市个数的条件,这时再加一个记录城市的数组就行了。
那么路线个数如何计算?代码中有说明:

参考代码:

#include<vector>
#include<iostream>
#include<string>
#include<map>
#define INF 999999
using namespace std;
int n, m, sc, dc;
string start_city;
vector<int>path, dis, hap, nums, weight,roads;//路径、距离、权重和、结点个数,代价最小的路径个数
vector<bool>visited;
int graph[210][210];
void Dijkstra() {
	for (int i = 0; i <= n; i++) {
		int vec = -1, minn = INF;
		for (int j = 0; j <= n; j++) {
			if (!visited[j]&&dis[j] < minn) {
				vec = j; minn = dis[j];
			}
		}
		if (minn == INF)break;
		visited[vec] = true;
		if (vec == dc)break;
		for (int u = 0; u <= n; u++) {
			if (!visited[u] && graph[vec][u] != 0) {
				if (dis[vec] + graph[vec][u] < dis[u]) {   //代价更小
					path[u] = vec;
					dis[u] = dis[vec] + graph[vec][u];
					hap[u] = hap[vec] + weight[u];
					nums[u] = nums[vec] + 1;
					roads[u] = roads[vec];        //代价最小的路线个数与前一个点相同
				}
				else if (dis[vec] + graph[vec][u] == dis[u]) {
					roads[u] += roads[vec];   //代价最小的路线个数
					if (hap[vec] + weight[u] > hap[u]) {  //更多快乐
						path[u] = vec;
						hap[u] = hap[vec] + weight[u];
						nums[u] = nums[vec] + 1;
						
					}
					else if (hap[vec] + weight[u]==hap[u]) {
						if (nums[vec] + 1 < nums[u]) {  //更少城市
							path[u] = vec;
							nums[u] = nums[vec] + 1;
						}
					}

				}
			}
		}
	}
}
int main() {
	string s1, s2;
	int w;
	cin >> n >> m >> start_city;
	map<int, string>num_tocity;
	map<string, int>city_tonum;
	num_tocity[0] = start_city;
	city_tonum[start_city] = 0;
	sc = 0;  //出发地
	path.resize(n + 2, -1);  //路径
	dis.resize(n + 2, INF);
	dis[0] = 0;
	hap.resize(n + 2, 0);
	nums.resize(n + 2, 0);
	weight.resize(n + 2, 0);
	visited.resize(n + 2, false);
	roads.resize(n + 2, 0);
	roads[0] = 1;
	for (int i = 1; i <= n - 1; i++) {  //n-1个城市		
		cin >> s1 >> w;
		if (s1 == "ROM")dc = i;
		num_tocity[i] = s1;
		city_tonum[s1] = i;
		weight[i] = w;   //在某城市中能够获得的快乐
	}
	for (int i = 1; i <= m; i++) {
		int a, b, x;
		cin >> s1 >> s2 >> x;
		a = city_tonum[s1];
		b = city_tonum[s2];
		graph[a][b] = graph[b][a] = x;
	}
	Dijkstra();
	printf("%d %d %d %d\n", roads[dc], dis[dc], hap[dc], hap[dc] / nums[dc]);
	vector<int>finalpath;
	int temp = dc;
	while (temp != -1) {
		finalpath.push_back(temp);
		temp = path[temp];
	}
	for (int i = finalpath.size() - 1; i >= 0; i--) {
		cout << num_tocity[finalpath[i]];
		if (i != 0)cout << "->";
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值