ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1087 All Roads Lead to Rome (30 分)

2021-09-06 14:30:48  阅读:202  来源: 互联网

标签:1087 ROM int str1 Rome maxn tempPath Roads happiness


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

题目大意:条条大路通罗马,给你图的信息,包括点权happiness和边权cost,找出cost最少的路径,如果有多条则找出happiness最大,还有多条则找出平均happiness最大,然后按要求输出;
分析:Dijkstra+DFS,用map分别存放string->int和int->string,average 是除去起点的average。

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 210;
const int INF = (1 << 30) - 1;
int n, m, st, ed;
int weight[maxn] = { 0 }, G[maxn][maxn], d[maxn];
bool vis[maxn] = { false };
vector<int> pre[maxn];
vector<int> path, tempPath;
map<string, int> cityToIndex;
map<int, string> indexToCity;
void Dijkstra(int s) {
	fill(d, d + maxn, INF);
	d[s] = 0;
	for (int i = 0;i < n;++i) {
		int u = -1, MIN = INF;
		for (int j = 0;j < n;++j) {
			if (vis[j] == false && d[j] < MIN) {
				u = j;
				MIN = d[j];
			}
		}
		if (u == -1) return;
		vis[u] = true;
		for (int v = 0;v < n;++v) {
			if (vis[v] == false && G[u][v] != INF) {
				if (d[u] + G[u][v] < d[v]) {
					d[v] = d[u] + G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if (d[u] + G[u][v] == d[v]) {
					pre[v].push_back(u);
				}
			}
		}
	}
}
int cnt = 0, maxHappi = -1, maxAveHappi = -1;
void DFS(int v) {
	if (v == st) {
		tempPath.push_back(v);
		++cnt;
		int happiness = 0, aveHappiness;
		for (int i = tempPath.size() - 1;i >= 0;--i) {
			int u = tempPath[i];
			happiness += weight[u];
		}
		aveHappiness = happiness / (tempPath.size() - 1);
		if (happiness > maxHappi) {
			maxHappi = happiness;
			maxAveHappi = aveHappiness;
			path = tempPath;
		}
		else if (happiness == maxHappi && aveHappiness > maxAveHappi) {
			maxAveHappi = aveHappiness;
			path = tempPath;
		}
		tempPath.pop_back();
		return;
	}
	tempPath.push_back(v);
	for (int i = 0;i < pre[v].size();++i) {
		DFS(pre[v][i]);
	}
	tempPath.pop_back();
}
int main() {
	string str1, str2;
	cin >> n >> m >> str1;
	cityToIndex[str1] = 0;
	indexToCity[0] = str1;
	fill(G[0], G[0] + maxn * maxn, INF);
	for (int i = 1;i < n;++i) {
		cin >> str1 >> weight[i];
		cityToIndex[str1] = i;
		indexToCity[i] = str1;
	}
	for (int i = 0;i < m;++i) {
		int l;
		cin >> str1 >> str2 >> l;
		int u = cityToIndex[str1], v = cityToIndex[str2];
		G[u][v] = G[v][u] = l;
	}
	st = 0, ed = cityToIndex["ROM"];
	Dijkstra(st);
	DFS(ed);
	cout << cnt << " " << d[ed] << " " << maxHappi << " " << maxAveHappi << endl;
	for (int i = path.size() - 1;i >= 0;--i) {
		if (i < path.size() - 1) cout << "->";
		cout << indexToCity[path[i]];
	}
	return 0;
}

标签:1087,ROM,int,str1,Rome,maxn,tempPath,Roads,happiness
来源: https://blog.csdn.net/weixin_43947762/article/details/120133568

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有