模板题,求调,悬赏关注
查看原帖
模板题,求调,悬赏关注
406124
Yzj2010小号楼主2023/10/20 11:26
#include<bits/stdc++.h>
using namespace std;
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define dep(a,b,c) for(int a=b;a>=c;--a)
#define MAX(a,b,c) max(max(a,b),c)
#define MIN(a,b,c) min(min(a,b),c)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
const int N = 2e6+7;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
int n,m,s,t;
struct edge{
	int v,w;
};
vector < edge > e[N];
int d[N];
bool p[N];
priority_queue< pair < int , int > > q;
inline void dijkstra()
{
	memset(d,inf,sizeof(d));
	memset(p,false,sizeof(p));
	d[s]=0;
    q.push(make_pair(s,0));
	while(!q.empty())
	{
		int f=q.top().first;
		q.pop();
		if(p[f]==true) continue;
		p[f]=true;
		rep(i,0,e[f].size()-1)
		{
			int nex=e[f][i].v;
			if(d[f]+e[f][i].w<d[nex])
			{
				d[nex]=d[f]+e[f][i].w;
				if(!p[nex]) q.push(make_pair(nex,-d[nex]));
			}
		}
	}
}
signed main()
{
	ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	cin>>n>>m>>s;
	rep(i,1,m)
	{
		int x,y,z;
		cin>>x>>y>>z;
		e[x].push_back({y,z});
	}
	dijkstra();
	rep(i,1,n)
	{
		if(d[i]==inf) cout<<"2147483647 ";
		else cout<<d[i]<<" ";
	}
	return 0;
}
2023/10/20 11:26
加载中...