R了五个点,求助
查看原帖
R了五个点,求助
220676
Coral_Swallow楼主2020/12/3 09:41

RT

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define inf 2147483647
#define N 100010
#define M 200010
#define LL long long
#define rep(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
struct edge{
	int to,next,val;
}e[N];
struct node{
	int pos;
	LL dis;
	bool operator <(const node &x)const{
		return x.dis<dis;
	}
};
priority_queue<node> q;
int n,m,s;
int u,v,w;
int head[N],cnt=0;
LL dis[N];
bool book[N];
void add_edge(int u,int v,int w)
{
	e[++cnt].to=v;
	e[cnt].val=w;
	e[cnt].next=head[u];
	head[u]=cnt;
}
void init()
{
	rep(i,1,n)dis[i]=inf;
	dis[s]=0;
}
int main()
{
	memset(book,0,sizeof(book));
	memset(head,-1,sizeof(head));
	scanf("%d%d%d",&n,&m,&s);
	init();
	rep(i,1,m)
	{
		scanf("%d%d%lld",&u,&v,&w);
		add_edge(u,v,w);
	}
	q.push((node){s,0});
	while(!q.empty())
	{
		node tmp=q.top();
		q.pop();
		int p=tmp.pos;
		LL d=tmp.dis;
		if(book[p])continue;
		book[p]=1;
		int k=head[p];
		while(k!=-1)
		{
			int t=e[k].to;
			if(dis[t]>dis[p]+e[k].val)
			{
				dis[t]=dis[p]+e[k].val;
				if(!book[t])
					q.push((node){t,dis[t]});
			}
			k=e[k].next;
		}
	}
	rep(i,1,n)printf("%lld ",dis[i]);
	return 0;
}
2020/12/3 09:41
加载中...