dij 90pts
查看原帖
dij 90pts
649751
Blued楼主2023/7/15 19:47
#include <bits/stdc++.h>
#define int long long
#define fir first
#define sec second
using namespace std;

const int N = 5e5 + 5;

struct node
{
	int to , v , pre;
}a[N];

int head[N];

int cnt;

int n , m , s;

int dis[N];

int x , y , z;

bool vis[N];

void add (int x , int y , int z)
{
	a[++ cnt].v = z;
	a[cnt].to = y;
	a[cnt].pre = head[x];
	head[x] = cnt;
}

priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > q;

void dij (int s)
{
	memset (dis , 127 , sizeof dis);
	
//	cout << dis[1] << '\n';
	
	dis[s] = 0;
	
	q.push (make_pair (dis[s] , s));
	
	while (! q.empty ())
	{
		int now = q.top ().sec;
		
		q.pop ();
		
		if (vis[now])
			continue;
		
		vis[now] = true;
		
		for (int i = head[now];i;i = a[i].pre)
			if (dis[a[i].to] > dis[now] + a[i].v)
				dis[a[i].to] = dis[now] + a[i].v , q.push (make_pair (dis[a[i].to] , a[i].to));
	}
	
	return ;
}

main ()
{
	
	
	cin >> n >> m >> s;
	
	for (int i = 1;i <= m;i ++)
	{
		cin >> x >> y >> z;
		
		add (x , y , z);
	}
	
	dij (s);
	
	for (int i = 1;i <= n;i ++)
		if (dis[i] != LONG_LONG_MAX)
			cout << dis[i] << ' ';
		else
			cout << INT_MAX << ' ';
	
	puts ("");
}
2023/7/15 19:47
加载中...