当前位置:网站首页>AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
2022-07-02 19:43:00 【Mr. Qiao Da】
AcWing 340. Communication lines
y Always say yes noip Improve the level of the Group , It's really difficult to change the meaning of the question , It's hard to find a solution , I hope I can write this level of questions by myself one day
Their thinking : Two points + deque BFS Find the shortest path , It is difficult to change thinking , Several turning points of thinking are :① Find a suitable less than k Value x, Two points .② In judging x When appropriate , You can use a double ended queue to find the shortest path
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10, M = 2e4 + 10, INF = 0x3f3f3f3f;
int h[N], ne[M], e[M], w[M], idx;
bool st[N];
deque<int>q;
int n, m, p, k;
int dist[N];
void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++ ;
}
bool check(int bound){
// The weight of the opposite side is only 0/1 The graph uses a double ended queue to search widely to find the shortest path
memset(st, 0, sizeof st);
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
q.push_front(1);
while(q.size()){
int op = q.front();
q.pop_front();
if(st[op]) continue;
st[op] = true;
for(int i = h[op]; ~i; i = ne[i]){
int j = e[i], v = w[i] > bound;
if(dist[j] > dist[op] + v){
dist[j] = dist[op] + v;
if(!v) q.push_front(j);
else q.push_back(j);
}
}
}
return dist[n] <= k; // The edge weight in the return graph is greater than bound Whether the number of edges of is greater than k
}
int main()
{
cin>>n>>m>>k;
memset(h, -1, sizeof h);
while(m -- ){
int a, b, c;
cin>>a>>b>>c;
add(a, b, c);
add(b, a, c);
}
// Two points to find the right value mid
// The right value mid It means having mid The weight of the edge is assigned 0 when , The shortest path can be found
// Here I have always wondered why the dichotomy mid It must be the weight in the graph
// After reading the detailed explanation of other bosses, I understand
//① The first understanding , If mid It is not the weight existing in the graph , So this time check The result is the same as last check The results returned are the same , Two points won't stop
//② The second understanding , The essence of dichotomy is to find the minimum , If mid Not the edge weights that exist in the graph , Then there must be a smaller number of edge weights , Chapter II continued , Until the smallest qualified value is found
// In a word , Two points is the best value , Instead of being appropriate
int l = 0, r = 1e6 + 1;
while(l < r){
int mid = l + r >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
if(r == 1e6 + 1) r = -1;
cout<<r<<endl;
return 0;
}
边栏推荐
- 高并发下如何避免产生重复数据?
- What is the MySQL backup suffix_ MySQL backup restore
- MySQL表历史数据清理总结
- Automatic reading of simple books
- 多端小程序开发有什么好处?覆盖百度小程序抖音小程序微信小程序开发,抢占多平台流量红利
- MySQL table historical data cleaning summary
- pxe装机「建议收藏」
- Horizontal ultra vires and vertical ultra vires [easy to understand]
- Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
- AcWing 1126. 最小花费 题解(最短路—dijkstra)
猜你喜欢
Build a master-slave mode cluster redis
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
基于SSM实现网上购物商城系统
Detailed tutorial on installing stand-alone redis
Kt148a voice chip IC user end self replacement voice method, upper computer
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
Notes on hardware design of kt148a voice chip IC
Génération automatique de fichiers d'annotation d'images vgg
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
随机推荐
基于SSM实现网上购物商城系统
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
RPD product: super power squad nanny strategy
AcWing 181. 回转游戏 题解(搜索—IDA*搜索)
AcWing 1125. Cattle travel problem solution (shortest path, diameter)
450-深信服面经1
AcWing 341. 最优贸易 题解 (最短路、dp)
AcWing 181. Turnaround game solution (search ida* search)
Bubble sort array
开始练习书法
Implementation of online shopping mall system based on SSM
Understanding and function of polymorphism
从20s优化到500ms,我用了这三招
Reading notes of "the way to clean structure" (Part 2)
AcWing 342. Road and route problem solving (shortest path, topological sorting)
Golang:[]byte to string
AcWing 1128. 信使 题解(最短路—Floyd)
Gmapping code analysis [easy to understand]
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
Function high order curry realization