当前位置:网站首页>AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
2022-07-02 18:27:00 【乔大先生】
AcWing 340. 通信线路
y总说有noip提高组水平,确实题意转换比较难,想找到解法也难,希望自己再努力努力某一天可以只凭自己写出这种水平的题
解题思路:二分+双端队列BFS求最短路,思维转换比较难,几个思维的拐弯点在:①找合适的小于k的值x,用二分。②在判断x是否合适时,可以用双端队列找最短路
#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){
//对边权值只有0/1的图用双端队列广搜找最短路
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; //返回图中边权值大于bound的边数量是否大于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);
}
//二分找到合适的值mid
//合适的值mid是指有mid条边的权值被赋为0时,可以找到最短路
//这里我一直有一个疑惑是为什么二分出的mid一定是图中存在的权值
//看了别的大佬详细的解释明白了
//①第一种理解,如果mid不是图中存在的权值,那么本次check的结果和上次check返回的结果相同,二分不会停止
//②第二种理解,二分的本质是找到最小值,如果mid不是图中存在的边权值,那么一定存在比他小的是边权值的数,二分会继续,直到找到最小的符合条件的值
//总得来说一句话,二分找的是最值,而不是合适就行
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;
}
边栏推荐
- PHP asymmetric encryption method private key and public key encryption and decryption method
- xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
- What is 9D movie like? (+ common sense of dimension space)
- Use cheat engine to modify money, life and stars in Kingdom rush
- 4274. 后缀表达式-二叉表达式树
- Notes de lecture sur le code propre
- MySQL表历史数据清理总结
- 【ERP软件】ERP体系二次开发有哪些危险?
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- PyTorch函数中的__call__和forward函数
猜你喜欢
Excel finds the same value in a column, deletes the row or replaces it with a blank value
Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Juypter notebook modify the default open folder and default browser
IEDA refactor的用法
juypter notebook 修改默认打开文件夹以及默认浏览器
Introduction to the paper | application of machine learning in database cardinality estimation
Compile oglpg-9th-edition source code with clion
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
随机推荐
虚拟机初始化脚本, 虚拟机相互免秘钥
AcWing 383. 观光 题解(最短路)
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
AcWing 1127. 香甜的黄油 题解(最短路—spfa)
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
全志A33使用主线U-Boot
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
AcWing 1129. 热浪 题解(最短路—spfa)
程序猿入门攻略(十二)——数据的存储
股票证券公司排名,有安全保障吗
安装单机redis详细教程
Thread application instance
PHP非对称加密方法私钥及公钥加密解密的方法
ORA-01455: converting column overflows integer datatype
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
Fastdfs installation
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
《代码整洁之道》读书笔记
Introduction of Ethernet PHY layer chip lan8720a
Educational Codeforces Round 129 (Rated for Div. 2) 补题题解