当前位置:网站首页>AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
2022-07-02 18:27:00 【乔大先生】
AcWing 342. 道路与航线
y总说是个难题,确实,比写十道简单题都有用。代码量很大的一道题,但是学到了很多,由道路想到拓扑排序,利用拓扑排序每个点只会被访问一次的性质可知,这也是一种找最短路的方法,所以先在联通块内跑jk最短路,之后进行所有连通块的拓扑排序,就能找到整个图的最短路
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int, int>PII;
const int N = 25010, M = 150010, INF = 0x3f3f3f3f;
int h[N], ne[M], e[M], w[M], idx;
vector<int> block[N]; //存放连通块
queue<int> q; //存放连通块的编号,用于拓扑排序
int n, mp, mr, S;
int id[N]; //记录每个点在哪个连通块内
int din[N]; //记录每个连通块的入度
int cnt; //记录联通块的数量
int dist[N];
bool 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(int u){
//u是连通块编号,在该连通块内跑jk最短路
priority_queue<PII, vector<PII>, greater<PII>>heap;
for(auto op : block[u]){
heap.push({
dist[op], op});
}
while(heap.size()){
auto op = heap.top();
heap.pop();
int ver = op.y;
int dis = op.x;
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver]; ~i; i = ne[i]){
int j = e[i];
if(id[ver] != id[j] && -- din[id[j]] == 0) q.push(id[j]);
if(dist[j] > dist[ver] + w[i]){
dist[j] = dist[ver] + w[i];
if(id[j] == id[ver]) heap.push({
dist[j], j});
}
}
}
}
void topsort(){
memset(dist, 0x3f, sizeof dist);
dist[S] = 0;
for(int i = 1; i <= cnt; i ++ ){
if(!din[i]) q.push(i); //将入度为0的连通块入队,进行拓扑排序
}
while(q.size()){
int op = q.front();
q.pop();
dijkstra(op); //入度为0的连通块内跑dijkstra
}
}
void dfs(int u, int bid){
id[u] = bid;
block[bid].push_back(u);
for(int i = h[u]; ~i; i = ne[i]){
int j = e[i];
if(!id[j]) dfs(j, bid);
}
}
int main()
{
cin>>n>>mr>>mp>>S;
memset(h, -1, sizeof h);
while(mr -- ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
for(int i = 1; i <= n; i ++ ){
if(!id[i]){
cnt ++ ;
dfs(i, cnt);
}
}
while(mp -- ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
din[id[b]] ++ ;
}
topsort();
for(int i = 1; i <= n; i ++ ){
if(dist[i] > INF / 2) cout<<"NO PATH"<<endl;
else cout<<dist[i]<<endl;
}
return 0;
}
边栏推荐
- Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
- 4274. 后缀表达式-二叉表达式树
- Develop fixed asset management system, what voice is used to develop fixed asset management system
- 450-深信服面经1
- Binary operation
- IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
- 虚拟机初始化脚本, 虚拟机相互免秘钥
- The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
- Refactoring: improving the design of existing code (Part 1)
- Golang并发编程——goroutine、channel、sync
猜你喜欢

Juypter notebook modify the default open folder and default browser

codeforces每日5题(均1700)-第四天

守望先锋世界观架构 ——(一款好的游戏是怎么来的)

zabbix5客户端安装和配置

搭建主从模式集群redis

Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5

Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出

教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5

ICDE 2023|TKDE Poster Session(CFP)

Markdown基础语法
随机推荐
高级性能测试系列《24. 通过jdbc执行sql脚本》
数据降维——主成分分析
metric_logger小解
How performance testing creates business value
Novice must see, click two buttons to switch to different content
What is the MySQL backup suffix_ MySQL backup restore
Use cheat engine to modify money, life and stars in Kingdom rush
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
End to end object detection with transformers (Detr) paper reading and understanding
《重构:改善既有代码的设计》读书笔记(上)
搭建主从模式集群redis
Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
SIFT特征点提取「建议收藏」
C file input operation
《架构整洁之道》读书笔记(下)
MySQL高级(进阶)SQL语句
[test development] takes you to know what software testing is
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星