p4779求调,玄关,急急急
  • 板块灌水区
  • 楼主Leo11
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/6/15 20:20
  • 上次更新2024/6/15 21:57:30
查看原帖
p4779求调,玄关,急急急
990387
Leo11楼主2024/6/15 20:20
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 1, inf = 1e16 + 7;
struct node
{
	int x, y;
	bool operator < (const node &z) const
	{
		return y > z.y;
	}
};
priority_queue<node>q;
vector<node>v[N];
int n, m, s, vis[N], ans[N];
signed main()
{
	cin >> n >> m >> s;
	for(int i = 0; i <= 100000; i++)
	{
		ans[i] = inf;
	}
	ans[s] = 0;
	while(m--)
	{
		int x, y, z;
		cin >> x >> y >> z;
		v[x].push_back({y, z});
		v[y].push_back({x, z});
	}
	q.push({s, 0});
	while(!q.empty())
	{
		int now = q.top().x;
		q.pop();
		if(vis[now])
		{
			continue;
		}
		vis[now] = 1;
		for(int i = 0; i < v[now].size(); i++)
		{
			if(ans[v[now][i].x] > ans[now] + v[now][i].y)
			{
				ans[v[now][i].x] = ans[now] + v[now][i].y;
				if(!vis[v[now][i].x])
				{
					q.push({v[now][i].x, ans[v[now][i].x]});
				}
			}
		}
	}
	for(int i = 1; i <= n; i++)
	{
		if(ans[i] >= inf)
		{
			cout << -1 << " ";
		}
		else
		{
			cout << ans[i] << " ";
		}
	}
}

为了复习重新打了一遍,为什么52?求大佬挑错

2024/6/15 20:20
加载中...