萌Zn求助
查看原帖
萌Zn求助
119685
Kevin_Wa楼主2021/10/21 18:59

只有16分。。。找不到问题了。。。

#include<bits/stdc++.h>
#define N 1000100
using namespace std;
struct node{
	int x,val;
};
inline bool operator <(const node &x,const node &y)
{
	return x.val<y.val;
}
int dis[N],vis[N],nxt[N],now[N],to[N],value[N],cnt,s,n,m,u,v,w;
template <typename T> void read(T &x) {
x = 0; char c = getchar();int f=1;
for (; !isdigit(c); c = getchar())if (c=='-') f=-1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x*=f;
}
void add(int x,int y,int z)
{
	nxt[++cnt]=now[x];now[x]=cnt;
	to[cnt]=y;value[cnt]=z; 
}
void init()
{
	for (int i=1;i<=n;i++) 
	  dis[i]=0x7fffffff,vis[i]=0;
	dis[s]=0;
}
priority_queue<node> q;
void dij()
{
	init();
	q.push((node){s,0}); 
	while (!q.empty())
	{
		node Node=q.top();
		q.pop();
		if (vis[Node.x]) continue;
		vis[Node.x]=1;
		for (int i=now[Node.x];i;i=nxt[i])
		{
			int To=to[i],va=value[i];
			if (dis[To]>dis[Node.x]+va) 
			  {
			  	dis[To]=dis[Node.x]+va;
			  	if (!vis[To]) q.push((node){To,dis[To]});
			  }
		}
	 } 
}
int main()
{
read(n);read(m);read(s);
for (int i=1;i<=m;i++)
  read(u),read(v),read(w),add(u,v,w);
dij();
for (int i=1;i<=n;i++)
   printf("%d ",dis[i]);
return 0;
}
2021/10/21 18:59
加载中...