优先队列WA 求调
查看原帖
优先队列WA 求调
910992
hupan8845074楼主2024/10/19 00:54

刚学dijkstra,不知道有什么问题。。

#include<bits/stdc++.h>
const int N=2e6;
const int inf=INT_MAX;
using namespace std;
struct jntm{
	int to,w;
};
struct que{
	int u,w;
	bool operator <(const que &x) const{
		return x.w < w ;
	}
};
int n,m,s;
int d[N],vis[N];
vector <jntm> edge[N];
priority_queue<que> q ;
void dj(int ss){
	for(int i=0;i<=n;i++){
		d[i]=inf;
	} 
	d[ss]=0;
	q.push((que){ss,0});
	for(int i=1;i<=n;i++){
		int u=q.top().u;
		while(!q.empty()) q.pop();
		
		for(auto ed:edge[u]){
			int to=ed.to , w=ed.w;
			if(d[to]>d[u]+w){
				d[to]=d[u]+w;
				q.push((que){to,d[to]});
			}
		}
	}
}
int main(){
	int a,b,x;
	cin>>n>>m>>s;
	for(int i=1;i<=m;i++){
		cin>>a>>b>>x;
		edge[a].push_back({b,x});
	}
	dj(s);
	for(int i=1;i<=n;i++){
		cout<<d[i]<<" ";
	}
	return 0;
}
2024/10/19 00:54
加载中...