当前位置:网站首页>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;
}
边栏推荐
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
- Notes de lecture sur le code propre
- 【pytorch学习笔记】Tensor
- C的内存管理
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
- [test development] takes you to know what software testing is
- [error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
- Emmet基础语法
- Markdown basic grammar
- 《代碼整潔之道》讀書筆記
猜你喜欢

线程应用实例

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

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

IEDA refactor的用法

zabbix5客户端安装和配置

juypter notebook 修改默认打开文件夹以及默认浏览器

In pytorch function__ call__ And forward functions

Develop fixed asset management system, what voice is used to develop fixed asset management system

Codeworks 5 questions per day (1700 average) - day 4

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
随机推荐
Microservice technology - distributed global ID in high concurrency
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
安装单机redis详细教程
Advanced performance test series "24. Execute SQL script through JDBC"
数据降维——因子分析
多态的理解以及作用
Compile oglpg-9th-edition source code with clion
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
Npoi export Excel2007
452-strcpy、strcat、strcmp、strstr、strchr的实现
2022.7.1-----leetcode. two hundred and forty-one
C的内存管理
【pytorch学习笔记】Tensor
第七章-类基础
metric_ Logger urination
Data dimensionality reduction factor analysis
NPOI导出Excel2007
End to end object detection with transformers (Detr) paper reading and understanding
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
How to print mybats log plug-in using XML file