PAT-1087

遍历的同时记录路径。

想着先试试不处理字符串到整型的映射,直接拿字符串当做索引会不会超时,结果稳得一笔。

#include <iostream>
#include <math.h>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <list>
#include <set>
#define DEBUG //IMPORTANT
using namespace std;




/*--------Globle--------*/
int N, K;
map<string, int> happinesses;
map <string, map<string, int>> costs;
string startCity;
/*----------------------*/
int main() {
#ifdef DEBUG
	freopen("C:/Users/ALIENWARE/Desktop/data.in", "r", stdin);
#endif
	/*-------Solution-------*/
	cin >> N >> K >> startCity;


	for (int i = 0; i < N - 1; i++)
	{
		string temp;
		cin >> temp;
		cin >> happinesses[temp];
	}
	for (int i = 0; i < K; i++)
	{
		string temp1, temp2;
		cin >> temp1 >> temp2;
		cin >> costs[temp1][temp2];
		costs[temp2][temp1] = costs[temp1][temp2];
	}


	map<string, bool> visited;
	map<string, int[3]> costAndHappiness = map<string, int[3]>();
	map<string, vector<list<string>>> roats; //本题实际上英文是唯一路径,所以可以是 map<string, list<string>> roats; 
	roats[startCity].push_back(list<string>());
	(roats[startCity][0]).push_back(startCity);
	for (auto i : happinesses)
	{
		costAndHappiness[i.first][0] = 999999;
	}
	costAndHappiness[startCity][2] = 1;
	happinesses[startCity] = 0;
	string now = startCity;
	while (true) {
		string next = "0";
		int min = 999999;
		for (auto i : happinesses)
		{
			if (visited[i.first]) continue;
			if (costAndHappiness[i.first][0] < min) {
				min = costAndHappiness[i.first][0];
				next = i.first;
			}
		}
		if (next == "0") break;
		now = next;
		visited[now] = true;
		for (auto i : costs[now])
		{
			if (costAndHappiness[now][0] + costs[now][i.first] < costAndHappiness[i.first][0]) {
				costAndHappiness[i.first][0] = costAndHappiness[now][0] + costs[now][i.first];
				costAndHappiness[i.first][1] = costAndHappiness[now][1] + happinesses[i.first];
				costAndHappiness[i.first][2] = costAndHappiness[now][2];
				roats[i.first] = roats[now];
				for (int j = 0; j < roats[i.first].size(); j++)
				{
					roats[i.first][j].push_back(i.first);
				}
			}
			else if (costAndHappiness[now][0] + costs[now][i.first] == costAndHappiness[i.first][0]) {
				costAndHappiness[i.first][2] += costAndHappiness[now][2];
				if (costAndHappiness[i.first][1] < costAndHappiness[now][1] + happinesses[i.first]) {
					costAndHappiness[i.first][1] = costAndHappiness[now][1] + happinesses[i.first];
					roats[i.first] = roats[now];
					for (int j = 0; j < roats[i.first].size(); j++)
					{
						roats[i.first][j].push_back(i.first);
					}
				}
			}
		}


	}
	cout << costAndHappiness["ROM"][2] << ' ' << costAndHappiness["ROM"][0] << ' ' <<
		costAndHappiness["ROM"][1] << ' ' << costAndHappiness["ROM"][1] / (roats["ROM"][0].size() - 1) << endl;
	cout << roats["ROM"][0].front();
	roats["ROM"][0].remove(roats["ROM"][0].front());
	for (auto i : roats["ROM"][0])
	{
		cout << "->" << i;
	}


	/*----------------------*/
	system("PAUSE");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值