为什么优先队列求最小生成树不能完全过,有无懂哥看一下
查看原帖
为什么优先队列求最小生成树不能完全过,有无懂哥看一下
1219010
shadow_leader楼主2024/4/10 20:56
#include<iostream>
#include<vector>
#include<queue>
const int N = 1e4 + 5;
using namespace std;
vector<int>e[N], w[N];
struct point {
	int x, d, y;
	point(int a = 0, int c = 0, int b = -1) {
		x = a, d = c, y = b;
	}
	bool operator <(const point& t)const {
		return d > t.d;
	}
};
int n, m, s, t, vis[N];
priority_queue<point,vector<point> >q;
void bfs()
{
	while (!q.empty())
	{
		point a = q.top();
		int x = a.y;
		q.pop();
		vis[a.x] = 1;
		if (a.x == t)
		{
			cout << a.y;
			return;
		}
		for (int i = 0; e[a.x].size() && i <= e[a.x].size() - 1; i++)
		{
			int v = e[a.x][i], w1 = w[a.x][i];
			if (!vis[v])
			{
				if (a.y == -1)
					x= w1;
				q.push(point(v, a.d + w1, max(x, w1)));
			}
		}
	}
}
int main()
{
	cin >> n >> m >> s >> t;
	vis[s] = 1;
	q.push(point(s));
	for (int i = 1; i <= m; i++)
	{
		int u, v, w1;
		cin >> u >> v >> w1;
		e[u].push_back(v);
		w[u].push_back(w1);
		e[v].push_back(u);
		w[v].push_back(w1);
	}
	bfs();
}
2024/4/10 20:56
加载中...