当前位置:网站首页>USACO2008通信线路
USACO2008通信线路
2022-07-29 23:17:00 【Vegetable newbie】
USACO2008通信线路
题意:
有 n n n个点, p p p条无向边,从顶点 1 1 1出发到顶点 n n n,每经过一条道路会有一定开销 L i L_i Li,你可以让 k k k条路径免费,问你路程中最贵的那条路径的价格最小为多少。
数据范围
0 ⩽ k ⩽ n 0 \leqslant k \leqslant \ n 0⩽k⩽ n
1 ⩽ p ⩽ 10000 1 \leqslant p \leqslant 10000 1⩽p⩽10000
1 ⩽ L i ⩽ 1000000 1 \leqslant L_i \leqslant 1000000 1⩽Li⩽1000000
思路
我们可以二分出第 k + 1 k + 1 k+1大的那条路线长度,就是答案。每次二分的时候用 双端 B F S 双端BFS 双端BFS求大于 u u u(二分的值)的路线个数 d i s t [ n ] dist[n] dist[n],如果 d i s t [ n ] ⩽ k dist[n] \leqslant k dist[n]⩽k就满足。
时间复杂度
O ( n ∗ log L i ) O(n * \log{Li}) O(n∗logLi)
代码
#include <bits/stdc++.h>
#define PII pair<int,int>
#define LL long long
#define fi first
#define se second
#define debug(a) cout<<#a<<"="<<a<<endl;
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define Yes puts("Yes");
#define No puts("No");
#define sz(x) (int)x.size()
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1; ch=getchar(); }
do {
(t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
template <typename T> void write(T t) {
if (t<0) {
putchar('-'); write(-t); return; }
if (t>9) write(t/10);
putchar('0'+t%10);
}
template <typename T> void writeln(T t) {
write(t); puts(""); }
const int N = 1010, M = 10010;
int h[N], w[M], ne[M], e[M], idx;
bool st[N];
int n, p, k, m;
int dist[N];
void add(int a, int b, int c){
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}
bool check(int u){
memset(dist, 127, sizeof dist);
memset(st, 0, sizeof st);
deque<int>q;
dist[1] = 0;
q.push_back(1);
while(q.size()){
int ver = q.front();
q.pop_front();
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver]; ~i; i = ne[i]){
int j = e[i], d = w[i] > u;
if(dist[j] > dist[ver] + d){
dist[j] = dist[ver] + d;
if(d == 1) q.push_back(j);
else q.push_front(j);
}
}
}
return dist[n] < k;//找到第k大就是小于k
}
int main()
{
read(n);read(m);read(k);
memset(h, -1, sizeof h);
for(int i = 1; i <= m; i ++ ){
int a, b, c;
read(a), read(b), read(c);
add(a, b, c), add(b, a, c);
}
int l = 0, r = 1e6 + 1;
while(l < r){
int mid = l + r >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
if(l == 1e6 + 1) puts("-1");
else writeln(l);
return 0;
}
边栏推荐
- JetsonNano学习(五)JetsonNano 安装 PyTorch 及 Torchvision
- DNA脱氧核糖核酸修饰石墨粉末|DNA修饰还原石墨烯功能材料|保存温度
- The first round of the real offer harvester~ How does the big factory inspect the candidates?(with detailed answer)
- The latest Gansu construction welder (construction special operation) simulation question bank and answer analysis in 2022
- High Numbers|Calculation of Triple Integral 3|Uncle High Numbers|Handwritten Notes
- Mysql8.0新特性之详细版本
- JetsonNano learning (5) JetsonNano installs PyTorch and Torchvision
- Override and customize dependent native Bean methods
- BGP Federal Comprehensive Experiment
- BGP联邦综合实验
猜你喜欢
随机推荐
地狱挖掘者系列#1
J9数字论:为什么我们需要Web3?
通过 FileUploader 的初始化,了解 SAP UI5 应用的 StaticArea 初始化逻辑
Codeforces Round #245 (Div. 1) A (dfs)
Implementation and implementation of Any to Any real-time voice change丨RTC Dev Meetup
JVM初探- 内存分配、GC原理与垃圾收集器
DNA脱氧核糖核酸修饰石墨粉末|DNA修饰还原石墨烯功能材料|保存温度
暴力递归到动态规划 04 (数字字符串转化)
We launched a "developer lab"
The first round of the real offer harvester~ How does the big factory inspect the candidates?(with detailed answer)
r‘w‘r‘w‘r‘w‘r
能源企业数字化转型背景下的数据安全治理实践路径
high-level-rest-client 判断索引是否存在
Huawei 14 Days - (3) Kernel Development
The sequence table of the linear table (the dry goods are full of sharing ~ contains all the function codes of the sequence table~
运动步数抽奖小程序开发
devops学习(九) Helm工具--持续部署
【openlayers】地图【二】
devops学习(六)Jenkins 持续部署-版本选择
WeChat applet sliding navigation bar (how to set the floating window of the webpage)








