1087. All Roads Lead to Rome (30)

1.求单源最短路径,如果不唯一,则输出happiness最高的路线。

2.使用dijkstra求出最小耗费,以这个最小耗费作为约束条件,在后面遍历的时候进行剪枝。

1087

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

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

 
AC代码:

//#include<string>
//#include <iomanip>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;
struct cityNode
{
	vector<pair<string, int>> list;
	bool visited;
	bool sured;
	int happiness;
	int cost;
	cityNode() :list(0), visited(false), sured(false), happiness(-1), cost(INT_MAX){};
};

void dfs(string now, int minCost, int nowCost, vector<pair<string, int>>&path, vector<vector<pair<string, int>>>&ans, map<string, bool>&used, map<string, cityNode>&city)
{
	if (now == "ROM"&&nowCost == minCost)
	{
		ans.push_back(path);
	}
	else if (nowCost > minCost)
		return;
	else
	{
		for (int i = 0; i < city[now].list.size(); i++)
		{
			string q = city[now].list[i].first;
			if (!used[q])
			{
				used[q] = true;
				path.push_back({ q, city[q].happiness });
				dfs(q, minCost, nowCost + city[now].list[i].second, path, ans, used, city);
				path.pop_back();
				used[q] = false;
			}
		}
	}
}
bool cmp(const vector<pair<string, int>>&a, const vector<pair<string, int>>&b)
{
	int aHappiness = 0;
	for (int i = 0; i < a.size(); i++)
	{
		aHappiness += a[i].second;
	}
	int bHappiness = 0;
	for (int i = 0; i < b.size(); i++)
	{
		bHappiness += b[i].second;
	}
	if (aHappiness > bHappiness) return true;
	else if (aHappiness == bHappiness && aHappiness / a.size() > bHappiness / b.size())
		return true;
	else return false;
}
int main(void)
{
	int n, k;
	string src;
	cin >> n >> k >> src;
	string target = "ROM";
	map<string, cityNode> city;

	for (int i = 0; i < n - 1; i++)
	{
		string str;
		int happiness;
		cin >> str >> happiness;
		city[str].happiness = happiness;
	}

	for (int i = 0; i < k; i++)
	{
		string a, b;
		int cost;
		cin >> a >> b >> cost;
		city[a].list.push_back({ b, cost });
		city[b].list.push_back({ a, cost });
	}
	city[src].visited = true;
	city[src].cost = 0;
	while (1)
	{
		string p = "";
		for (map<string, cityNode>::iterator ite = city.begin(); ite != city.end(); ite++)
		{
			if (p == ""&&ite->second.visited&&!ite->second.sured)
				p = ite->first;
			else if (p != ""&&ite->second.visited&&!ite->second.sured&& ite->second.cost < city[p].cost)
				p = ite->first;
		}
		if (p == "") break;
		city[p].sured = true;
		if (city[target].sured) break;
		for (int i = 0; i < city[p].list.size(); i++)
		{
			string q = city[p].list[i].first;
			if (!city[q].sured&&city[p].cost + city[p].list[i].second < city[q].cost)
			{
				city[q].visited = true;
				city[q].cost = city[p].cost + city[p].list[i].second;
			}
		}
	}
	int minCost = city[target].cost;

	map<string, bool> used;
	vector<pair<string, int>>path;
	vector<vector<pair<string, int>>>ans;
	dfs(src, minCost, 0, path, ans, used, city);
	sort(ans.begin(), ans.end(), cmp);
	int totalHappiness = 0;
	int avgHappiness = 0;
	for (int i = 0; i < ans[0].size(); i++)
	{
		totalHappiness += ans[0][i].second;
	}
	avgHappiness = totalHappiness / ans[0].size();
	printf("%d %d %d %d\n", ans.size(), minCost, totalHappiness, avgHappiness);
	cout << src << "->";
	for (int i = 0; i < ans[0].size(); i++)
	{
		cout << ans[0][i].first;
		if (i != ans[0].size() - 1)
			cout << "->";
	}
	cout << endl;
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注