pat A 1087

比以前写的好多了,也过了


#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<sstream>
#include<map>
#include<algorithm>
#include<unordered_map>
#include<regex>
using namespace std;
const int inf = 9999999;
unordered_map<string, int> str2int;
unordered_map<int, string> int2str;
int cnt = 1;
void getint(string s)
{
	if (str2int[s] == 0) {
		str2int[s] = cnt++;
		int2str[str2int[s]] = s;
	}
}
int happy[250];
int g[250][250];
int isok[250], dist[250];
vector <vector<int>> pre(250);
int st;
vector<int>tmpath, path;
int maxhap = 0, maxavghap = 0, dif = 0;
void dfs(int e,int hap)
{

	if (e == st) {
		dif++;
		if (hap > maxhap) {
			path.clear();
			path = tmpath;
			maxhap = hap;
			maxavghap = maxhap /( path.size()-1);
		}
		else if (hap == maxhap) {
			int tmpmaxavg = hap / (tmpath.size()-1);
			if (tmpmaxavg > maxavghap) {
				maxavghap = tmpmaxavg;
				path.clear();
				path = tmpath;
			}
		}
		return;
	}
	for (int i = 0; i < pre[e].size(); ++i)
	{
		tmpath.push_back(pre[e][i]);
		dfs(pre[e][i], hap + happy[pre[e][i]]);
		tmpath.pop_back();
	}
}
int main()
{
//	freopen("test.txt", "r", stdin);
	int n, k;
	string start;
	cin >> n >> k >> start;
	getint(start);
	for (int i = 1; i < n; ++i) {
		string city;
		int hap;
		cin >> city >> hap;
		getint(city);
		happy[str2int[city]] = hap;
	}
	for (int i = 0; i < 250; ++i)
		for (int j = 0; j < 250; ++j)
			g[i][j] = inf;
	for (int i = 0; i < k; ++i)
	{
		string s, e;
		int cost;
		cin >> s >> e >> cost;
		int sint = str2int[s];
		int eint = str2int[e];
		g[sint][eint] = cost;
		g[eint][sint] = cost;
	}
	int rom = str2int["ROM"];
	st = str2int[start];
	fill(dist, dist + 250, inf);
	dist[st] = 0;
	while (true)
	{
		int minindex = -1, mindist = inf;
		for (int i = 1; i < cnt; ++i)
		{
			if (isok[i] == 0 && dist[i] < mindist)
			{
				minindex = i;
				mindist = dist[i];
			}
		}
		if (minindex == -1)break;
		isok[minindex] = 1;
		for (int i = 1; i < cnt; ++i)
		{
			if (isok[i] == 0)
			{
				if (mindist + g[minindex][i] < dist[i])
				{
					dist[i] = mindist + g[minindex][i];
					pre[i].clear();
					pre[i].push_back(minindex);
				}
				else if (mindist + g[minindex][i] == dist[i])
				{
					pre[i].push_back(minindex);
				}
			}
		}
	}
	tmpath.push_back(rom);
	dfs(rom,happy[rom]);
	int cur = rom;
	printf("%d %d %d %d\n", dif, dist[rom], maxhap, maxavghap);
	cout << start;
	for (int i = path.size()-2; i >=0; --i)
	{
		cout << "->" << int2str[path[i]];
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值