当前位置:网站首页>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;
}
边栏推荐
- PXE installation "recommended collection"
- 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
- Reading notes of code neatness
- ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题
- Istio1.12:安装和快速入门
- 定了,就是它!
- Understanding and function of polymorphism
- 数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
- AcWing 181. Turnaround game solution (search ida* search)
- Introduction to mongodb chapter 03 basic concepts of mongodb
猜你喜欢
随机推荐
ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题
Start practicing calligraphy
冒泡排序数组
451-memcpy、memmove、memset的实现
MySQL table historical data cleaning summary
AcWing 1126. Minimum cost solution (shortest path Dijkstra)
PXE installation "recommended collection"
解决方案:VS2017 无法打开源文件 stdio.h main.h 等头文件[通俗易懂]
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
Getting started with typescript
NMF-matlab
Golang:[]byte to string
From 20s to 500ms, I used these three methods
Yes, that's it!
Codeforces Round #802 (Div. 2) 纯补题
AcWing 383. 观光 题解(最短路)
What is the MySQL backup suffix_ MySQL backup restore
浏览器缓存机制概述
Implementation of 453 ATOI function









