蒟蒻求助
查看原帖
蒟蒻求助
56007
USS_Lexington楼主2020/11/27 11:33
#include<bits/stdc++.h>
#define N 200005
#define INF 1e10
typedef long long ll;
using namespace std;
struct edge
{
	int to;//这条边到达的点 
	int nxt;//与这条边同起点下一条边的编号 
	int val;//边权 
}e[2*N];
int num,head[N];
int n,m,s,t;
void add(int from,int to,int dis)
{
	e[++num].nxt=head[from];
	e[num].to=to;
	e[num].val=dis;
	head[from]=num;
}
inline int read()
{
	 int x=0,f=1;char ch;ch=getchar();
	 while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
	 while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	 return x*f;
}
struct node
{
	int w,now;
	bool operator <(const node &a) const
	{
		return w>a.w;
	}
};
int dis[N];
bool vis[N];
priority_queue<node> q;
int dijkstra(int s)
{
	for(int i=1;i<=n;i++) dis[i]=INF;
	dis[s]=0;
	q.push((node){0,s});
	/*node o;
    o.w = 0;
    o.now = s;
    q.push(o);
    */
	while(!q.empty())
	{
		node x=q.top();
		q.pop();
		int u=x.now;
		if(vis[u])
		{
			continue;
		} 
		vis[u]=1;
		for(int i=head[u];i;i=e[i].nxt)
		{
			int v=e[i].to;
			if(dis[v]>dis[u]+e[i].val) 
			{
				dis[v]=dis[u]+e[i].val;
				if(!vis[v]) q.push((node){dis[v],v});
			}
		}
	}
}
int main()
{
	
	n=read();m=read();s=read();
	for(int u,v,d,i=1;i<=m;i++)
	{
		u=read();v=read();d=read();
		add(u,v,d);
		add(v,u,d);
	}
	dijkstra(s);
	for(int i=1;i<=n;i++) printf("%d ",dis[i]);
	return 0;
}

2020/11/27 11:33
加载中...