【题解】道路航线

#include <bits/stdc++.h>
using namespace std;
const int N = 2.5 * 1e4 + 5;
const int M = 5 * 1e4 + 5;

struct rec {
    
    
    int x, y, z;
} e[M];

struct edge {
    
    
    int v, w;
};

void read(int &x) {
    
    
    int f = 1;
    x = 0;
    char c = getchar();
    while (c < '0' || c > '9') {
    
    
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
    
    
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    x *= f;
}

int n, R, P, s, con[N], in[N], cnt;
long long dis[N];
bool vis[N], pos[N];
vector<edge> son[N];
vector<int> v[N];
priority_queue<pair<long long, int> > q;

void connect(int x) {
    
    
    for (int i = 0; i < son[x].size(); i++) {
    
    
        int y = son[x][i].v;
        if (!con[y]) {
    
    
            con[y] = cnt;
            connect(y);
        }
    }
}

void dijkstra() {
    
    
    for (int i = 1; i <= n; i++) dis[i] = 1e9;
    dis[s] = 0;
    q.push(make_pair(0, s));
    while (q.size()) {
    
    
        int x = q.top().second;
        q.pop();
        if (vis[x])
            continue;
        vis[x] = 1;
        for (int i = 0; i < son[x].size(); i++) {
    
    
            int y = son[x][i].v, w = son[x][i].w;
            if (dis[x] + w < dis[y]) {
    
    
                dis[y] = dis[x] + w;
                if (con[x] == con[y])
                    q.push(make_pair(-dis[y], y));
            }
            if (con[x] != con[y]) {
    
    
                in[con[y]]--;
                if (!in[con[y]]) {
    
    
                    for (int i = 1; i <= n; i++)
                        if (con[i] == con[y] && pos[i])
                            q.push(make_pair(-dis[i], i));
                }
            }
        }
    }
}

void bfs() {
    
    
    queue<int> que;
    que.push(s);
    pos[s] = 1;
    while (que.size()) {
    
    
        int x = que.front();
        que.pop();
        for (int i = 0; i < v[x].size(); i++) {
    
    
            int y = v[x][i];
            if (!pos[y]) {
    
    
                pos[y] = 1;
                que.push(y);
            }
        }
    }
}

int main() {
    
    
    read(n), read(R), read(P), read(s);
    for (int i = 1; i <= R; i++) {
    
    
        int x, y, z;
        read(x), read(y), read(z);
        son[x].push_back((edge){
    
     y, z });
        son[y].push_back((edge){
    
     x, z });
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for (int i = 1; i <= n; i++)
        if (!con[i]) {
    
    
            cnt++;
            con[i]=cnt;
            connect(i);
        }
    for (int i = 1; i <= P; i++) {
    
    
        int x, y, z;
        read(x), read(y), read(z);
        v[x].push_back(y);
        e[i] = (rec){
    
     x, y, z };
    }
    bfs();
    for (int i = 1; i <= P; i++) {
    
    
        int x = e[i].x, y = e[i].y, z = e[i].z;
        if (pos[x] && pos[y]) {
    
      //能够与s联通
            son[x].push_back((edge){
    
     y, z });
            in[con[y]]++;
        }
    }
    dijkstra();
    for (int i = 1; i <= n; i++) {
    
    
        if (dis[i] == 1e9)
            printf("NO PATH\n");
        else
            printf("%lld\n", dis[i]);
    }
}

猜你喜欢

转载自blog.csdn.net/cqbzlydd/article/details/108088351