各位神犇, 帮帮蒟蒻吧, 样例#2,#3,#6过不了。【QWQ】
查看原帖
各位神犇, 帮帮蒟蒻吧, 样例#2,#3,#6过不了。【QWQ】
657502
Wangfeiyang楼主2023/8/25 13:28
#include<bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
typedef pair<int, int> p;

struct node { 
    int id, w;
    friend bool operator < (node a, node b){
        return a.w > b.w;
    }
};

int n, m, s;
vector<p> v[N];
int dis[N], vis[N];

void dijkstra(int s){
	dis[s] = 0;
	priority_queue<node> q;
	q.push(node{s, 0});
	
	while( !q.empty() ){
		node tmp = q.top(); q.pop();
		if(vis[tmp.id]) continue;
		vis[tmp.id] = true;
		
		for(int i = 0; i < v[tmp.id].size(); i ++){
			int j = v[tmp.id][i].first;
			int k = v[tmp.id][i].second;
			if(tmp.w + k < dis[j] && !vis[j]){
				dis[j] = tmp.w + k;
				q.push(node{j, dis[j]});
			}
		}
	}
}

int main() {
	cin >> n >> m >> s;
	for(int i = 1; i <= m; i ++){
		int x, y, z;
		cin >> x >> y >> z;
		v[x].push_back(make_pair(y, z));
	}
	
	memset(dis, 0x3f, sizeof dis);
	memset(vis, 0, sizeof vis);
	dijkstra(s);
	
	int a = INT_MAX;
	for(int i = 1; i <= n; i ++){
		if(dis[i] == 0x3f) cout << a << " ";
		else cout << dis[i] << " ";
	}
	return 0;
}
2023/8/25 13:28
加载中...