当前位置:网站首页>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;
}
边栏推荐
- 冒泡排序数组
- Golang:[]byte to string
- Getting started with typescript
- Data dimensionality reduction factor analysis
- PHP parser badminton reservation applet development requires online system
- Processing strategy of message queue message loss and repeated message sending
- ORA-01455: converting column overflows integer datatype
- 函数高阶-柯里化实现
- SIFT feature point extraction "suggestions collection"
- A4988 drive stepper motor "recommended collection"
猜你喜欢

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5

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

Markdown basic grammar

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5

机器学习笔记 - 时间序列预测研究:法国香槟的月销量

守望先锋世界观架构 ——(一款好的游戏是怎么来的)

Codeworks 5 questions per day (1700 average) - day 4
![[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

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
冒泡排序数组
随机推荐
第七章-类基础
Reading notes of "the way to clean structure" (Part 2)
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
SIFT feature point extraction "suggestions collection"
Advanced performance test series "24. Execute SQL script through JDBC"
Refactoring: improving the design of existing code (Part 1)
股票证券公司排名,有安全保障吗
Imitation Jingdong magnifying glass effect (pink teacher version)
横向越权与纵向越权[通俗易懂]
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
SIFT特征点提取「建议收藏」
Golang concurrent programming goroutine, channel, sync
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
Emmet basic syntax
思考变量引起的巨大变化
PHP-Parser羽毛球预约小程序开发require线上系统
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
High frequency interview questions