当前位置:网站首页>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;
}
边栏推荐
- Getting started with typescript
- KT148A语音芯片ic的硬件设计注意事项
- 450-深信服面经1
- PXE installation "recommended collection"
- KT148A语音芯片ic的用户端自己更换语音的方法,上位机
- Correspondence between pytoch version, CUDA version and graphics card driver version
- How to avoid duplicate data in gaobingfa?
- 《重构:改善既有代码的设计》读书笔记(上)
- Automatic reading of simple books
- Detailed tutorial on installing stand-alone redis
猜你喜欢
随机推荐
Function high order curry realization
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
AcWing 1126. Minimum cost solution (shortest path Dijkstra)
Understanding and function of polymorphism
SQLite 3.39.0 发布,支持右外连接和全外连接
解决方案:VS2017 无法打开源文件 stdio.h main.h 等头文件[通俗易懂]
SIFT feature point extraction "suggestions collection"
思考变量引起的巨大变化
KT148A语音芯片ic的用户端自己更换语音的方法,上位机
Shardingsphere jdbc5.1.2 about select last_ INSERT_ ID () I found that there was still a routing problem
Automatically generate VGg image annotation file
Infix expression is converted to suffix expression (C language code + detailed explanation)
开始练习书法
Notes on hardware design of kt148a voice chip IC
Horizontal ultra vires and vertical ultra vires [easy to understand]
Pytorch版本、CUDA版本与显卡驱动版本的对应关系
450 Shenxin Mianjing 1
Yes, that's it!
Reading notes of "the way to clean structure" (Part 2)
SQLite 3.39.0 release supports right external connection and all external connection