强化版过了,弱化版不过?
查看原帖
强化版过了,弱化版不过?
655195
Yannik楼主2024/10/22 21:03
#include<bits/stdc++.h>
#define int long long
#define PII pair<int,int> 
using namespace std;
const int N = 2e5 + 10;
int n,m,s;
int dist[N],vis[N];
vector<PII> g[N];
void init(){
    for(int i=1;i<=n;i++){
        dist[i]=INT_MAX;
        vis[i]=0;
    }
}
void dij(int s){
    init();
	priority_queue<PII,vector<PII>,greater<PII>> q;
	q.push({dist[s]=0,s});
	while(q.size()){
		int var=q.top().second;
		q.pop();
		if(vis[var]) continue;
		vis[var]=1;
		for(auto x:g[var]){
			int e=x.first,w=x.second;
			if(dist[e]>dist[var]+w){
				q.push({dist[e]=dist[var]+w,e});
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m>>s;
	for(int i=1;i<=m;i++){
		int a,b,c;
		cin>>a>>b>>c;
		g[a].push_back({b,c});
	}
	dij(s);
	for(int i=1;i<=n;i++){
	    if(dist[i]==INT_MAX) cout<<INT_MAX-1<<" ";
		else cout<<dist[i]<<" ";
	} 
	return 0;
}
2024/10/22 21:03
加载中...