WA on#3
查看原帖
WA on#3
1152025
MATAIN楼主2025/1/18 11:39
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f,N=500005;
int n,m,s,cnt,head[N],vis[N],dis[N];
struct node
{
	int dis,pos;
	bool operator <(const node &x)const
	{
		return x.dis<dis;//优先队列,默认大根堆,distance优先 
	}
};
struct edge
{
	int next,to,dis;//领边(BFS),下个点,权值(长度) 
}e[N];
void add(int u,int v,int w)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	e[cnt].dis=w;
	head[u]=cnt;
}
priority_queue<node> q;
void dijkstra()
{
	for(int i=1;i<=n;i++)dis[i]=inf;
	dis[s]=0; 
	q.push((node){0,s});
	while(!q.empty())
	{
		node tmp=q.top();
		q.pop();
		int d=tmp.dis,pos=tmp.pos;
		if(vis[pos]) continue;
		vis[pos]=1;
		for(int i=head[pos];i;i=e[i].next)
		{
			int v=e[i].to;
			if(dis[pos]+e[i].dis<dis[v])
			{
				dis[v]=dis[pos]+e[i].dis;//更新最短路 
				if(!vis[v]) q.push((node){dis[v],v});
			}
		}
	}
}
int main()
{
	cin>>n>>m>>s;
	for(int i=1;i<=m;i++)
	{
		int u,v,w;
		cin>>u>>v>>w;
		add(u,v,w);
	}
	dijkstra();
	for(int i=1;i<=n;i++) cout<<dis[i]<<" ";



    return 0;
}
2025/1/18 11:39
加载中...