当前位置:网站首页>AcWing 342. Road and route problem solving (shortest path, topological sorting)
AcWing 342. Road and route problem solving (shortest path, topological sorting)
2022-07-02 19:31:00 【Mr. Qiao Da】
AcWing 342. Roads and routes
y It's always a problem , exactly , It's more useful than writing ten simple questions . A problem with a large amount of code , But I learned a lot , From roads to topological sorting , Using the topological sorting, we can know that each point will only be visited once , This is also a way to find the shortest way , So run in the Unicom block first jk shortest path , Then topological sorting of all connected blocks , You can find the shortest path of the whole diagram
#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]; // Store connecting blocks
queue<int> q; // Store the number of the connected block , Used for topological sorting
int n, mp, mr, S;
int id[N]; // Record which connected block each point is in
int din[N]; // Record the penetration of each connected block
int cnt; // Record the number of connecting blocks
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 Is the connected block number , Run in the connected block jk shortest path
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); // The degree of engagement is 0 Connected blocks of join the team , Topological sort
}
while(q.size()){
int op = q.front();
q.pop();
dijkstra(op); // The degree of 0 Running within the connected block of 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;
}
边栏推荐
- MySQL
- PHP-Parser羽毛球预约小程序开发require线上系统
- AcWing 1135. 新年好 题解(最短路+搜索)
- Virtual machine initialization script, virtual machine mutual secret key free
- IEDA refactor的用法
- Bubble sort array
- Thread application instance
- Introduction to the paper | application of machine learning in database cardinality estimation
- 453-atoi函数的实现
- Chapter 7 - class foundation
猜你喜欢
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
全志A33使用主线U-Boot
Thread application instance
450-深信服面经1
juypter notebook 修改默认打开文件夹以及默认浏览器
Compile oglpg-9th-edition source code with clion
冒泡排序数组
PHP-Parser羽毛球预约小程序开发require线上系统
Excel finds the same value in a column, deletes the row or replaces it with a blank value
mysql函数
随机推荐
MySQL
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
PyTorch函数中的__call__和forward函数
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
4274. Suffix expression - binary expression tree
Golang concurrent programming goroutine, channel, sync
Date tool class (updated from time to time)
zabbix5客户端安装和配置
第七章-类基础
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
高级性能测试系列《24. 通过jdbc执行sql脚本》
2022.7.1-----leetcode. two hundred and forty-one
Reading notes of code neatness
Npoi export Excel2007
虚拟机初始化脚本, 虚拟机相互免秘钥
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
《重构:改善既有代码的设计》读书笔记(上)
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)