求助!
查看原帖
求助!
359430
江户川コナン楼主2021/9/26 13:16

为什么堆优化dij会TLE? 求助大佬! 代码:

#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,u;
	bool operator<(const node &a)const{
		return u<a.u;
	}
	node(int xx,int uu){
		x=xx;
		u=uu;
	}
};
const int maxn=1e5+5;
vector<node> G[maxn];
int a[maxn];
int n,m,s;
multiset<node> q;
int main(){
	memset(a,0x3f,sizeof a);
	cin>>n>>m>>s;
	for(int i=1;i<=m;i++){
		int u,x,y;
		scanf("%d%d%d",&x,&y,&u);
		G[x].push_back(node(y,u));
	}
	a[s]=0;
	//cout<<a[s]<<endl;
	q.insert(node(s,0));
	while(!q.empty()){
		/*cout<<a[1]<<endl;
		for(int i=1;i<=n;i++){
			if(a[i]==0x3f3f3f3f) printf("2147483647 ");
			else printf("%d ",a[i]);
		}
		cout<<endl<<endl;*/
		node now=*(q.begin());
		q.erase(q.begin());
		for(auto i:G[now.x]){
			if(now.u+i.u<a[i.x]){
				a[i.x]=now.u+i.u;
				q.insert(node(i.x,a[i.x]));
			}
		}
		
	}
	for(int i=1;i<=n;i++){
		if(a[i]==0x3f3f3f3f) printf("2147483647 ");
		else printf("%d ",a[i]);
	}
	return 0;
}
2021/9/26 13:16
加载中...