求助,就过了一个点
查看原帖
求助,就过了一个点
433642
m泛函625楼主2022/4/30 08:03
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N = 100005, M = 500005;
int n, m, s;
typedef pair<int,int> PII;
int h[N], e[N], ne[N], w[N], idx;
int dist[N], st[N];
void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void Dijkstra() {
    for (int i = 1; i <= n; ++i)dist[i] = 0x7fffffff;
    priority_queue<PII, vector<PII>, greater<PII> >heap;
    dist[s] = 0;
    heap.push({0,s});
    while (!heap.empty()) {
        PII head= heap.top();
        heap.pop();
        int ver = head.second,d=head.first;
        if (st[ver])continue;
        st[ver] = 1;
        for (int i = h[ver];i!=-1; i = ne[i]) {
            int j = e[i];
            if (dist[j] > d + w[i]) {
                dist[j] = d + w[i];
                heap.push({ dist[j],j });
            }
        }
        
        
    }
}
int main() {
    memset(h, -1, sizeof h);
    cin >> n >> m >> s;
    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }
    Dijkstra();
    for (int i = 1; i <= n; ++i)cout << dist[i]<<" ";
}

2022/4/30 08:03
加载中...