直捣黄龙题解

原题链接  直捣黄龙

//朴素版本
#include <iostream>
#include <cstring>
#include <string>
#include <stack>
#include <unordered_map>
using namespace std;

const int N = 205, INF = 0x3f3f3f;
int n, k, c, g[N][N], num[N];
string s, e, a, b;
unordered_map<string, int> name;
unordered_map<int, string> code;

int d[N], cnt[N], number[N], fa[N];
int city[N];//判断经过的城市个数
bool st[N];

void Dijkstra(int s)
{
	//要求为路径最短,经过城市最多,打倒敌人最多
	fill(d, d + N, INF);
	cnt[s] = 1;//到自己路径数为1
	fa[s] = -1;//自己为根节点
	d[s] = 0; //到自己距离为0
	city[s] = 0; //经过城市为0

	for (int i = 0; i < n; i++)
	{
		int x = -1, m = INF;
		//找到最近的结点,扩展子网
		for (int y = 0; y < n; y++)
		{
			if (!st[y])
			{
				if (m > d[y])
				{
					x = y;
					m = d[y];
				}
			}
		}
		st[x] = true;

		for (int y = 0; y < n; y++)
		{
			if (!st[y] && g[x][y] != INF)
			{
				if (d[y] > m + g[x][y]) //更新距离
				{
					d[y] = m + g[x][y]; //更新距离
					fa[y] = x; //更新父节点
					number[y] = number[x] + num[y]; //更新歼敌数量
					cnt[y] = cnt[x]; //只有一条路径,所以相等
					city[y] = city[x] + 1; //城市+1
				}
				else if (d[y] == m + g[x][y]) //距离相等更新条数
				{
					cnt[y] += cnt[x];//条数相加
					//判断经过的城市的多少
					if (city[y] < city[x] + 1)
					{
						city[y] = city[x] + 1; //更新城市
						fa[y] = x; //更新父节点
						number[y] = number[x] + num[y]; //更新歼敌数量
					}
					else if (city[y] == city[x] + 1)
					{
						if (number[y] < number[x] + num[y])
						{
							fa[y] = x; //更新父节点
							number[y] = number[x] + num[y]; //更新歼敌数量
						}
					}
				}
			}
		}
	}

}

void dfs(int e)
{
	if (fa[e] == -1)
	{
		cout << code[e];
	}
	else
	{
		dfs(fa[e]);
		cout << code[e] << "->";
	}
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> k >> s >> e;
	name[s] = 0;
	code[0] = s;
	num[0] = 0;

	for (int i = 1; i < n; i++)
	{
		cin >> a >> c;
		name[a] = i;
		code[i] = a;
		num[i] = c;
	}

	memset(g, 0x3f, sizeof(g));
	for (int i = 1; i <= k; i++)
	{
		cin >> a >> b >> c;
		int start = name[a], end = name[b];
		g[start][end] = c;
		g[end][start] = c;
	}

	Dijkstra(name[s]);
	//输出路径
	int end = name[e];
	int i = end;
	stack<int> s;
	while (i != -1)
	{
		s.push(i);
		i = fa[i];
	}
	while (s.size())
	{
		int t = s.top();
		s.pop();
		cout << code[t];
		if (s.size())
			cout << "->";
	}
	cout << endl << cnt[end] << " " << d[end] << " " << number[end];
	return 0;
}


//堆优化版本
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;

typedef pair<int, int> PII;
const int N = 205, INF = 0x3f3f3f;
int n, k, c, g[N][N], num[N];
string s, e, a, b;
unordered_map<string, int> name;
unordered_map<int, string> code;

int d[N], cnt[N], number[N], fa[N];
int city[N];//判断经过的城市个数
bool st[N];

void Dijkstra(int s)
{
	//要求为路径最短,经过城市最多,打倒敌人最多
	fill(d, d + N, INF);
	priority_queue<PII, vector<PII>, greater<PII>> heap; //小根堆申请
	heap.push({0, s});
	cnt[s] = 1;//到自己路径数为1
	fa[s] = -1;//自己为根节点
	d[s] = 0; //到自己距离为0
	city[s] = 0; //经过城市为0

	while (heap.size())
	{
		//找到最近的结点,扩展子网
		int x = heap.top().second, m = heap.top().first;
		heap.pop();

		if (st[x])
			continue;
		st[x] = true;

		for (int y = 0; y < n; y++)
		{
			if (!st[y] && g[x][y] != INF)
			{
				if (d[y] > m + g[x][y]) //更新距离
				{
					d[y] = m + g[x][y]; //更新距离
					fa[y] = x; //更新父节点
					number[y] = number[x] + num[y]; //更新歼敌数量
					cnt[y] = cnt[x]; //只有一条路径,所以相等
					city[y] = city[x] + 1; //城市+1
					heap.push({d[y], y});
				}
				else if (d[y] == m + g[x][y]) //距离相等更新条数
				{
					cnt[y] += cnt[x];//条数相加
					//判断经过的城市的多少
					if (city[y] < city[x] + 1)
					{
						city[y] = city[x] + 1; //更新城市
						fa[y] = x; //更新父节点
						number[y] = number[x] + num[y]; //更新歼敌数量
					}
					else if (city[y] == city[x] + 1)
					{
						if (number[y] < number[x] + num[y])
						{
							fa[y] = x; //更新父节点
							number[y] = number[x] + num[y]; //更新歼敌数量
						}
					}
				}
			}
		}
	}

}

void dfs(int e)
{
	if (fa[e] == -1)
	{
		cout << code[e];
	}
	else
	{
		dfs(fa[e]);
		cout << "->" << code[e];
	}
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> k >> s >> e;
	name[s] = 0;
	code[0] = s;
	num[0] = 0;

	for (int i = 1; i < n; i++)
	{
		cin >> a >> c;
		name[a] = i;
		code[i] = a;
		num[i] = c;
	}

	memset(g, 0x3f, sizeof(g));
	for (int i = 1; i <= k; i++)
	{
		cin >> a >> b >> c;
		int start = name[a], end = name[b];
		g[start][end] = c;
		g[end][start] = c;
	}

	Dijkstra(name[s]);
	//输出路径
	int end = name[e];
	dfs(end);
	cout << endl << cnt[end] << " " << d[end] << " " << number[end];
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值