性感堆优化Dikjstra 90分 在线球调教
查看原帖
性感堆优化Dikjstra 90分 在线球调教
947854
ztyo_zysclown楼主2024/3/8 18:27
#include<bits/stdc++.h>
using namespace std;
const int N=1145140;
int h[N],e[N],ne[N],w[N];
int dist[N];
bool st[N];
int n,m,o,p;
int idx;
void cha(int a,int b,int c){
	e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void dij(){
	memset(dist,0x3f,sizeof(dist));
	dist[o]=0;
	priority_queue<pair<int,int> ,vector<pair<int,int> >,greater<pair<int,int> > > heap;
	heap.push({0,o});
	while(heap.size()){
		auto t=heap.top();
		heap.pop();
		int v=t.second,d=t.first;
		if(st[v]){
			continue;
		}
		st[v]=1;
		for(int i=h[v];i!=-1;i=ne[i]){
			int j=e[i];
			if(dist[j]>(d+w[i])){
				dist[j]=d+w[i];
				heap.push({dist[j],j});
			}
		}
	}
}
int main(){
	cin>>n>>m>>o;
	memset(h,-1,sizeof(h));
	while(m--){
		int u,v,w;
		cin>>u>>v>>w;
		cha(u,v,w);
		//cha(v,u,w);
	}
	dij();
	for(int i=1;i<=n;i++){
		cout<<dist[i]<<" ";
	}
	return 0;
} 
2024/3/8 18:27
加载中...