当前位置:网站首页>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;
}
边栏推荐
- PHP asymmetric encryption method private key and public key encryption and decryption method
- PHP非对称加密方法私钥及公钥加密解密的方法
- 【pytorch学习笔记】Tensor
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
- 【ERP软件】ERP体系二次开发有哪些危险?
- C文件输入操作
- In pytorch function__ call__ And forward functions
- Emmet basic syntax
- Getting started with typescript
- 2022 compilation principle final examination recall Edition
猜你喜欢

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

Codeworks 5 questions per day (1700 average) - day 4

潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖

Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config

《重构:改善既有代码的设计》读书笔记(上)

Machine learning notes - time series prediction research: monthly sales of French champagne

Develop fixed asset management system, what voice is used to develop fixed asset management system

juypter notebook 修改默认打开文件夹以及默认浏览器

Data dimensionality reduction factor analysis
随机推荐
PHP asymmetric encryption method private key and public key encryption and decryption method
线程应用实例
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
Talk about the design of red envelope activities in e-commerce system
《重构:改善既有代码的设计》读书笔记(下)
Bubble sort array
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
【pytorch学习笔记】Tensor
电脑使用哪个录制视频软件比较好
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
AcWing 1137. 选择最佳线路 题解(最短路)
golang:[]byte转string
AcWing 1134. 最短路计数 题解(最短路)
SIFT feature point extraction "suggestions collection"
450-深信服面经1
Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
[test development] takes you to know what software testing is
AcWing 383. 观光 题解(最短路)
AcWing 1135. 新年好 题解(最短路+搜索)