当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

为什么要做企业固定资产管理系统,企业如何加强固定资产管理
![[paper reading] Ca net: leveraging contextual features for lung cancer prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[paper reading] Ca net: leveraging contextual features for lung cancer prediction

What is 9D movie like? (+ common sense of dimension space)

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出

Refactoring: improving the design of existing code (Part 1)

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

High frequency interview questions

Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
随机推荐
GMapping代码解析[通俗易懂]
Emmet基础语法
Preprocessing and preprocessing macros
PHP非对称加密方法私钥及公钥加密解密的方法
LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
AcWing 1129. 热浪 题解(最短路—spfa)
二进制操作
横向越权与纵向越权[通俗易懂]
Gmapping code analysis [easy to understand]
Excel finds the same value in a column, deletes the row or replaces it with a blank value
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
IEDA refactor的用法
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
Getting started with typescript
MySQL
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
mysql备份后缀是什么_mysql备份还原
Npoi export Excel2007
Compile oglpg-9th-edition source code with clion
451-memcpy、memmove、memset的实现