当前位置:网站首页>Roads and routes -- dfs+topsort+dijkstra+ mapping
Roads and routes -- dfs+topsort+dijkstra+ mapping
2022-07-05 01:07:00 【Wawa source】


Algorithm flow
1. Enter all two-way roads first , then DFS Out all connected blocks , Calculate two arrays :id[] Store which connected block each point belongs to ;vector<int> block[] Store the points in each connected block :
2. Enter all routes , At the same time, the penetration of each connected block is counted .
3. Process each connected block in topological order , First, set all entries to 0 The number of the connected block of is added to the queue
in .
4. Take out the number of one connected block from the head of the team at a time bid
5. Will be block[bid] All points in are added to the heap , Then run for all points in the heap dijkstra Algorithm .
6. Take out the smallest point in the heap every time ver.
7. Then traverse ver All the neighbors of j. If id[ver]==id[], So if j Can be updated , Will j Insert into the heap : If id[ver]!= id[j], Will id[j] The penetration of this connected block decreases 1, If reduced 0 了 , Then insert it into the queue of topology sorting
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int N = 25010,M=150010,INF=0x3f3f3f3f;
typedef pair<int, int> PII;
#define x first
#define y second
int e[M],h[N],ne[M],w[M],idx=0;
int n,mr,mp,s;
int bcnt;
vector<int>block[N];
int id[N];
int din[N];
int dist[N];
bool st[N];
queue<int>q;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void dfs(int u,int bid)
{
id[u]=bid,block[bid].push_back(u);
for(int i=h[u];i!=-1;i=ne[i])
{
int j=e[i];
if(!id[j])
dfs(j,bid);
}
}
void dij(int bid)
{
priority_queue<PII,vector<PII>,greater<PII> >heap;
for(auto u:block[bid])
heap.push({
dist[u],u});
while(heap.size())
{
auto t=heap.top();
heap.pop();
int ver=t.y,distance=t.x;
if(st[ver])continue;
st[ver]=true;
for(int i=h[ver];i!=-1;i=ne[i])
{
int j=e[i];
if(id[j]!=id[ver]&&--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<=bcnt;i++)
if(!din[i])
q.push(i);
while(q.size())
{
int t=q.front();
q.pop();
dij(t);
}
}
signed main()
{
memset(h, -1, sizeof h);
cin>>n>>mr>>mp>>s;
for(int i=1;i<=mr;i++)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
for(int i=1;i<=n;i++)
if(!id[i])
{
bcnt++;
dfs(i,bcnt);
}
for(int i=1;i<=mp;i++)
{
int a,b,c;
cin>>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;
}
边栏推荐
- Maximum number of "balloons"
- 26.2 billion! These universities in Guangdong Province have received heavy support
- MongoDB系列之学习笔记教程汇总
- 【纯音听力测试】基于MATLAB的纯音听力测试系统
- 各大主流编程语言性能PK,结果出乎意料
- Recursive execution mechanism
- Discrete mathematics: Main Normal Form (main disjunctive normal form, main conjunctive normal form)
- 那些一门心思研究自动化测试的人,最后都怎样了?
- Basic operation of database and table ----- the concept of index
- Safety learning week4
猜你喜欢

Sorting selection sorting

Leetcode70 (Advanced), 322

【纯音听力测试】基于MATLAB的纯音听力测试系统

潘多拉 IOT 开发板学习(RT-Thread)—— 实验4 蜂鸣器+马达实验【按键外部中断】(学习笔记)

Identifiers and keywords

Les phénomènes de « salaire inversé » et de « remplacement des diplômés » indiquent que l'industrie des tests a...

SAP ui5 application development tutorial 107 - trial version of SAP ui5 overflow toolbar container control introduction

全栈开发提效神器——ApiFox(Postman + Swagger + Mock + JMeter)

A simple SSO unified login design

"Upside down salary", "equal replacement of graduates" these phenomena show that the testing industry has
随机推荐
pycharm专业版下载安装教程
Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
大专学历,33岁宝妈又怎样?我照样销售转测试,月入13k+
dotnet-exec 0.6.0 released
Query for Boolean field as "not true" (e.g. either false or non-existent)
Introduction to the gtid mode of MySQL master-slave replication
Arbitrum: two-dimensional cost
无心剑英译席慕容《无怨的青春》
Discrete mathematics: Main Normal Form (main disjunctive normal form, main conjunctive normal form)
Intel sapphire rapids SP Zhiqiang es processor cache memory split exposure
Basic concept and usage of redis
揭露测试外包公司,关于外包,你或许听到过这样的声音
华为百万聘请数据治理专家!背后的千亿市场值得关注
Pycharm professional download and installation tutorial
leetcode494,474
潘多拉 IOT 开发板学习(RT-Thread)—— 实验4 蜂鸣器+马达实验【按键外部中断】(学习笔记)
Compare whether two lists are equal
Global and Chinese market of nutrient analyzer 2022-2028: Research Report on technology, participants, trends, market size and share
程序员SQL数据脚本编码能力弱,BI做不出来怎么办?
Delaying wages to force people to leave, and the layoffs of small Internet companies are a little too much!