蒟蒻求助,TLE,明明开了小根堆啊
查看原帖
蒟蒻求助,TLE,明明开了小根堆啊
251998
百毒不侵√楼主2020/11/5 23:58
#include<bits/stdc++.h>
using namespace std;
#define ll long long  
const int inf=0x3f3f3f3f;
const int maxn=500010,maxm=100010;

int dis[maxm],n,m,s,cnt=0,head[maxm];
bool book[maxm];

struct ok{
	int next,to,w;
}e[maxn];

void add(int u,int v,int dis){
	e[++cnt].next=head[u];
	e[cnt].to=v;
	e[cnt].w=dis;
	head[u]=cnt;
}

struct wjqm{//小根堆 
	int id,id_dis;
	bool operator <(const wjqm &a) const {return id_dis>a.id_dis ;}
};

priority_queue<wjqm> q;

void da(){
	for(int i=1;i<=n;i++){
		book[i]=0;dis[i]=inf;
	}
	dis[s]=0;	
	q.push( ( wjqm ) { s , 0 } );
	while(!q.empty()){
		wjqm a=q.top();q.pop();
		int x = a.id , d = a.id_dis;
		if(book[x]) continue;
		for(int i = head[x] ; i ; i = e[i].next){
			int y=e[i].to;
			if(dis[y]>dis[x]+e[i].w){
				dis[y]=dis[x]+e[i].w;
				if(book[y]==0){
					q.push((wjqm){y,dis[y]});
				}
			}
		}
	}
}

int read(){
	int sum=0,fg=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')fg=-1;ch=getchar();}
	while(isdigit(ch)){sum=sum*10+ch-'0';ch=getchar();}
	return sum*fg;
}


int main(){
	n=read();m=read();s=read();
	for(int i=1;i<=m;i++){
		int u,v,w;
		u=read();v=read();w=read();
		add(u,v,w);
	}
	da();
	for(int i=1;i<=n;i++){
		if(s==i)cout<<0<<" ";
		else cout<<dis[i]<<" ";
	}
	return 0;
}

2020/11/5 23:58
加载中...