1087 All Roads Lead to Rome (30分)--PAT甲级真题

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

题目大意:给出每个城市的快乐值,以及城市之间道路的距离;现在要找出起点到终点罗马的最短路径,如果最短路径有多条,则找出经过的城市快乐值总和最高的那条;如果仍然有多条,则找出平均快乐值最高的道路;最后给出最短路径的条数,已经找出的那条路径。
分析:这道题目不仅仅要求最短路径长度,而且还要按照快乐值总和,快乐值平均值来挑选路径;所以单单用迪杰斯特拉算法不能解决问题,还要加上dfs来搜寻符合要求的那条道路;

#include<iostream>
#include<vector>
#include<map>
#include<stack>
#include<string>
using namespace std;
map<string, int> toi;
map<int, string> tos;
struct node {
	int next = -1, c = -1;
};
vector<node>E[202];
stack<int> Ans, curAns;
int N, K, S, Disti, index = 0, maxH = -1, ways = 0, minCnt = 123123123;
double maxAvH = -1;
int happy[202];
bool visit[202];
int Dis[202];
void dfs(int n, int c, int hp, int cnt) {
	if (n == S && c == Dis[Disti] && (hp > maxH || hp == maxH && cnt < minCnt)) {
		Ans = curAns;
		maxH = hp;
		minCnt = cnt;
	}
	if (n == S && c == Dis[Disti])
		ways++;
	if (n == S)
		return;
	for (int i = 0; i < E[n].size(); i++) {
		if (visit[E[n][i].next] == true)
			continue;
		visit[E[n][i].next] = true;
		curAns.push(E[n][i].next);
		dfs(E[n][i].next, c + E[n][i].c, hp + happy[n], cnt + 1);
		curAns.pop();
		visit[E[n][i].next] = false;
	}
}

int main() {
	string start, city;
	cin >> N >> K >> start;
	toi[start] = index++;
	tos[toi[start]] = start;
	S = toi[start];
	for (int i = 1; i < N; i++) {
		cin >> city;
		toi[city] = index++;
		tos[toi[city]] = city;
		cin >> happy[toi[city]];
	}
	for (int i = 0; i < K; i++) {
		string sa, sb;
		int c;
		cin >> sa >> sb >> c;
		node tmp;
		tmp.c = c;
		tmp.next = toi[sb];
		E[toi[sa]].push_back(tmp);
		tmp.next = toi[sa];
		E[toi[sb]].push_back(tmp);
	}
	for (int i = 0; i < N; i++) {
		Dis[i] = -1;
		visit[i] = false;
	}
	Dis[S] = 0;
	visit[S] = true;
	int newP = S;
	for (int i = 1; i < N; i++) {
		int u = newP;
		for (int j = 0; j < E[u].size(); j++) {
			int v = E[u][j].next;
			int c = E[u][j].c;
			if (visit[v] == true)
				continue;
			if (Dis[v] == -1 || Dis[v] > Dis[u] + c)
				Dis[v] = Dis[u] + c;
		}
		int min = 123123123;
		for (int j = 0; j < N; j++) {
			if (visit[j] == true || Dis[j] == -1)
				continue;
			if (Dis[j] < min) {
				min = Dis[j];
				newP = j;
			}
		}
		visit[newP] = true;
	}
	Disti = toi["ROM"];
	for (int i = 0; i < N; i++)
		visit[i] = false;
	dfs(Disti, 0, 0, 0);
	printf("%d %d %d %d\n", ways, Dis[Disti], maxH, maxH / minCnt);
	while (!Ans.empty()) {
		printf("%s->", tos[Ans.top()].c_str());
		Ans.pop();
	}
	printf("ROM");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值