当前位置:网站首页>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;
}
边栏推荐
- IEDA refactor的用法
- Compile oglpg-9th-edition source code with clion
- In pytorch function__ call__ And forward functions
- Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
- Use cheat engine to modify money, life and stars in Kingdom rush
- Typescript 之 快速入门
- Watchful pioneer world outlook Architecture - (how does a good game come from)
- metric_ Logger urination
- zabbix5客户端安装和配置
- codeforces每日5题(均1700)-第四天
猜你喜欢
![[0701] [paper reading] allowing data imbalance issue with perforated input during influence](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [paper reading] allowing data imbalance issue with perforated input during influence

AcWing 342. 道路与航线 题解 (最短路、拓扑排序)

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

Introduction to the paper | application of machine learning in database cardinality estimation

End-to-End Object Detection with Transformers(DETR)论文阅读与理解
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept

《重构:改善既有代码的设计》读书笔记(下)

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

Compile oglpg-9th-edition source code with clion

Novice must see, click two buttons to switch to different content
随机推荐
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
C的内存管理
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
juypter notebook 修改默认打开文件夹以及默认浏览器
程序猿入门攻略(十二)——数据的存储
Yunna | why use the fixed asset management system and how to enable it
MySQL表历史数据清理总结
虚拟机初始化脚本, 虚拟机相互免秘钥
Notes de lecture sur le code propre
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
Typescript 之 快速入门
MySQL advanced (Advanced) SQL statement
多态的理解以及作用
ORA-01455: converting column overflows integer datatype
ICDE 2023|TKDE Poster Session(CFP)
450-深信服面经1
C文件输入操作
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下