求调,玄关
查看原帖
求调,玄关
1097936
nineup楼主2024/7/26 09:15
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=2e5+5,N=1e5+5;
int n,m,s;
int h[M],cnt=1;
struct edge{
	int to,next,w;
}e[M];
struct node{
	int x,d;
	bool operator <(const node &a)const{
		return a.d<d;
	}
};
priority_queue <node> q;
bool vis[N];
int dis[N],pre[N];
int u,v,w;
void add(int u,int v,int w){
	e[cnt].to=v;
	e[cnt].next=h[u];
	e[cnt].w=w;
	h[u]=cnt++;
}
void way(int s,int t){
	if(t==s) printf("%d ",s);
	else{
		way(s,pre[t]);
		printf("%d ",t);
	}
}
int main(){
	cin>>n>>m>>s;
	memset(h,-1,sizeof(h));
	for(int i=1; i<=m; i++){
		scanf("%d%d%d",&u,&v,&w);
		add(u,v,w);
	}
	for(int i=1; i<=n; i++) dis[i]=0x3f3f3f3f;
	dis[s]=0;
	q.push((node){s,0});
	while(!q.empty()){
		node k=q.top();
		q.pop();
		if(vis[k.x]) continue;
		vis[k.x]=1;
		for(int i=h[k.x]; i; i=e[i].next){
			int t=e[i].to;
			if(dis[t]>dis[k.x]+e[i].w){
				dis[t]=dis[k.x]+e[i].w;
				pre[t]=k.x;
				
			}
			if(!vis[t]) q.push((node){t,dis[t]});
		}
	}
	for(int i=1; i<=n; i++) printf("%d ",dis[i]);
	return 0;
}
2024/7/26 09:15
加载中...