32分AC#2#5求助!

P4779 【模板】单源最短路径(标准版)

Nuclear_Fish_cyq @ 2022-08-21 16:50:18

#include <bits/stdc++.h>
using namespace std;
int n, m, s, en[100005], dis[100005], p, minn, mini;
bool f[100005];
struct edge{
    int to, weight;
};
struct node{
    int id, dist;
    bool operator <(const node x)const{
        return dist < x.dist;
    }
};
priority_queue<node>q;
vector<edge> a[100005];
void dij(){
    dis[s] = 0;
    q.push({s, 0});
    for(int i = 0; i < n && !q.empty(); i++){
        mini = q.top().id;
        minn = dis[mini];
        q.pop();
        f[mini] = true;
        if(minn != q.top().dist){
            continue;
        }
        for(int j = 0; j < en[mini]; j++){
            if(!f[a[mini][j].to] && dis[a[mini][j].to] > a[mini][j].weight + minn){
                dis[a[mini][j].to] = a[mini][j].weight + minn;
                q.push({a[mini][j].to, dis[a[mini][j].to]});
            }
        }
    }
}
int main(){
    cin >> n >> m >> s;
    s--;
    edge t;
    int tf;
    for(int i = 0; i < n; i++){
        dis[i] = 2147483647;
    }
    for(int i = 0; i < m; i++){
        cin >> tf >> t.to >> t.weight;
        tf--;
        t.to--;
        a[tf].push_back(t);
        en[tf]++;
    }
    dij();
    cout << dis[0];
    for(int i = 1; i < n; i++){
        cout << " " << dis[i];
    }
    cout << endl;
    return 0;
}

|