阳历没过,求条
查看原帖
阳历没过,求条
947930
xiaoxiongbinggan楼主2024/9/18 09:56
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int n,m,s;
int u,v,W;
int to[1000000],tot,nex[1000000],w[1000000],head[1000000];
void add(int a,int b,int W){
	to[tot++]=b;
	nex[tot]=head[a];
	head[a]=tot;
	w[tot]=W;
}
int dij[1000000];
bool vis[10000000];
void di(){
	priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
	vis[s]=1;
	q.push({0,s});
	while(!q.empty()){
		pair<int,int> t=q.top();
		q.pop();
		if(vis[t.second]){
			continue;
		}
		vis[t.second]=1;
		for(int i=head[t.second];~i;i=nex[i]){
			tot=to[i];
			if(dij[tot]>dij[t.second]+w[i]){
				dij[tot]=dij[t.second]+w[i];
				if(!vis[t.second]){
					q.push({dij[t.second],t.second});
				}
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	memset(head,-1,sizeof head);
	memset(dij,INT_MAX,sizeof dij);
	memset(vis,0,sizeof vis);
	cin>>n>>m>>s;
	for(int i=1;i<=m;i++){
		cin>>u>>v>>W;
		add(u,v,W);
	}
	di();
	for(int i=1;i<=n;i++){
		cout<<dij[i]<<" ";
	}
	return 0;
}
2024/9/18 09:56
加载中...