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. 

输入描述:

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.

输出描述:

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

输入例子:

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

 

输出例子:

3 3 195 97
HZH->PRS->ROM

算法分析:

DFS+Dijkstra

图的边权重表示cost,图的顶点表示城市,顶点的权重表示该城市的幸福度;

注意:最后一个输出average happiness,不包括起始城市。如输出样例中195/2 =97.5(舍弃0.5)

 

AC代码

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1000000000;
const int N = 202;
const string destination = "ROM";

map<string, int> cityToint;	//城市-->序号
map<int, string> intTocity;	//序号-->城市
int happiness[N], num[N], e[N][N], dist[N], bypass[N];	//从起点s到终点i,对应的cost(dist),happiness,最短路径方案数目num...
bool visit[N];
int cityhappiness[N];
string name[N];
int pre[N];
int n, k;
string start;

void Dijkstra(int s);	//s是起点
void print(int x);		//逆序打印路径

int main(void) {
	fill(e[0], e[0] + N*N, inf);
	fill(dist, dist + N, inf);
	fill(visit, visit + N, false);

	cin >> n >> k >> start;
	cityToint[start] = 0; intTocity[0] = start;
	string city; int happy;
	for (int i = 1;i < n; i++) {
		cin >> city >> happy;
		cityToint[city] = i; intTocity[i] = city;
		cityhappiness[i] = happy;
	}
	cityhappiness[0] = 0;
	string a, b; int c;
	for (int i = 1; i <= k; i++) {
		cin >> a >> b >> c;
		int id1 = cityToint[a]; int id2 = cityToint[b];
		e[id1][id2] = c;
		e[id2][id1] = e[id1][id2];	//无向图
	}

	int d = cityToint[destination];		//终点id
	Dijkstra(0);
	cout << num[d] << " " << dist[d] << " " << happiness[d] << " " << happiness[d] / bypass[d] << endl;
	//逆序打印路径
	print(d);
	puts("");
	//system("pause");
	return 0;
}

void Dijkstra(int s) {
	dist[s] = 0;
	fill(bypass, bypass + N, inf);
	bypass[s] = 0;
	fill(num, num + N, 0);
	num[s] = 1;
	fill(happiness, happiness + N, 0);

	for (int i = 0; i < n; i++) {
		int u = -1, mind = inf;
		for (int j = 0; j < n; j++)
			if (false == visit[j] && mind > dist[j]) {
				mind = dist[j];
				u = j;
			}
		if (-1 == u) return;	//end
		visit[u] = true;
		for (int j = 0; j < n; j++) {
			if (false == visit[j] && e[u][j] < inf) {
				if (e[u][j] + mind < dist[j]) {
					dist[j] = e[u][j] + mind;
				//	pre[j].clear();
					pre[j] = u;
					happiness[j] = happiness[u] + cityhappiness[j];
					bypass[j] = bypass[u] + 1;		//路过的城市数+1
					num[j] = num[u];				//方案数目
				}
				else if (dist[j] == e[u][j] + mind) {
					num[j] += num[u];
					int temph = happiness[u] + cityhappiness[j];
					int tempb = bypass[u] + 1;
					if ((temph > happiness[j]) ||		//the maximum happiness
						(temph == happiness[j] && tempb < bypass[j])) {	//the maximum average happiness 
						happiness[j] = temph;
						bypass[j] = tempb;
						pre[j] = u;
					}
				}
			}
		}
	}
}

void print(int x) {
	if (x) {
		print(pre[x]);
		cout << "->";
	}
	cout << intTocity[x];
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值