【题解】道路航线

#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]); } }

__EOF__

本文作者仰望星空的蚂蚁
本文链接https://www.cnblogs.com/cqbzly/p/17530403.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   仰望星空的蚂蚁  阅读(6)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示