1087. All Roads Lead to Rome (30)

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


1087. All Roads Lead to Rome (30)_第1张图片


AC代码:

//#include
//#include 
//#include
//#include
//#include 
//#include "func.h"
//#include 
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include
using namespace std;
struct cityNode
{
	vector> 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>&path, vector>>&ans, map&used, map&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>&a, const vector>&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 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::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 used;
	vector>path;
	vector>>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;
}


你可能感兴趣的:(PAT,单源最短路径,遍历,dijkstra)