链接:https://ac.nowcoder.com/acm/contest/1069/F
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.
There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1…N that are scattered around Farmer John’s property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.
The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.
As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.
Determine the minimum amount that Farmer John must pay.
输入描述:

  • Line 1: Three space-separated integers: N, P, and K
  • Lines 2…P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li
    输出描述:
  • Line 1: A single integer, the minimum amount Farmer John can pay. If it is impossible to connect the farm to the phone company, print -1.
    示例1
    输入
    复制
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

输出
复制

4

说明
There are 5 poles. Pole 1 cannot be connected directly to poles 4 or 5. Pole 5 cannot be connected directly to poles 1 or 3. All other pairs can be connected. The phone company will provide one free cable.
If pole 1 is connected to pole 3, pole 3 to pole 2, and pole 2 to pole 5 then Farmer John requires cables of length 4, 3, and 9. The phone company will provide the cable of length 9, so the longest cable needed has length 4.
题意:
大概题意就是,给一些点之间建边的花费,要使1与n连通,你建立的1–〉n这条路上,有k条边的花费(任意k条)可以报销(原题就是电力公司会免费提供),要你求解要自己出钱最大一条边的权值最小是多少。

很明显就是求解第k+1大最小。
转换一下就是最多有k条边比ans大
为什么是最多k条呢?
因为1–〉n这条路可能不足k条边就可以使它相连(此时自己不用出钱)
ans一定是唯一的一个值
所以本题:用二分ans+spfa来做
spfa中dis数组中记录的是1到各点有多少个点比二分的ans大
当dis[n]<=k说明此时二分的ans大了,二分ans要向前移动
AC_code:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int MAXN = 1e4+5;
struct Edge
{int to;int val;Edge(int t,int v):to(t),val(v){}
};
vector<Edge>vec[MAXN];
bool isIn[MAXN];
int dis[MAXN];
int n,p,k;
bool spfa(int mid)
{for(int i = 1; i<= n; i++){isIn[i] = false;dis[i] = INF;}dis[1] = 0;queue<int>q;isIn[1] = true;q.push(1);while(!q.empty()){int k = q.front();q.pop();isIn[k] = false;int iSize = vec[k].size();for(int i = 0; i < iSize; i++){int t = vec[k][i].to;int v = vec[k][i].val > mid;if(dis[t] > dis[k] + v){dis[t] = dis[k] + v;if(!isIn[t]){isIn[t] = true;q.push(t);}}}}return dis[n] <= k;
}
int main()
{scanf("%d%d%d",&n,&p,&k);int x,y,v;for(int i = 0; i < p; i++){scanf("%d%d%d",&x,&y,&v);vec[x].push_back(Edge(y,v));vec[y].push_back(Edge(x,v));}int l = 0,r = 1e6;int res = -1;while(l <= r){int mid = (l+r)>>1;if(spfa(mid)){res = mid;r = mid - 1;}else{l = mid + 1;}}printf("%d\n",res);return 0;
}

牛客假日团队赛8:F.Telephone Lines(二分+spfa)相关推荐

  1. 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)...

    链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  2. 牛客假日团队赛10 L 乘积最大 (dp,大数)

    链接:https://ac.nowcoder.com/acm/contest/1072/L?&headNav=acm&headNav=acm 来源:牛客网 乘积最大 时间限制:C/C+ ...

  3. 牛客假日团队赛12【为了抽奖闲来做题2】

    A题 链接:https://ac.nowcoder.com/acm/contest/1081/A 来源:牛客网 Bessie is trapped in a triangular maze with ...

  4. 牛客假日团队赛8:H.Cell Phone Network(最小支配集)

    链接:https://ac.nowcoder.com/acm/contest/1069/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...

  5. 牛客假日团队赛8:K.Cow Contest(最短路(floyd)变形)

    链接:https://ac.nowcoder.com/acm/contest/1069/K 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...

  6. P5200 [USACO19JAN]Sleepy Cow Sorting 牛客假日团队赛6 D 迷路的牛 (贪心)

    链接:https://ac.nowcoder.com/acm/contest/993/E 来源:牛客网 对牛排序 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  7. 牛客假日团队赛6 D 迷路的牛 (思维)

    链接:https://ac.nowcoder.com/acm/contest/993/D 来源:牛客网 迷路的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  8. 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)...

    链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  9. 牛客假日团队赛5 K 金币馅饼 (DP 基础题)

    链接:https://ac.nowcoder.com/acm/contest/984/K 来源:牛客网 金币馅饼 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

最新文章

  1. 美国爱因斯坦计划技术分析
  2. c语言链表交换,求单链表的数据交换解决思路
  3. Intel® Media Server Studio Support
  4. 数据结构——第一章线性表:01线性表的逻辑结构
  5. 中国SaaS死或生之四:卧榻之侧,是谁在捅刀 SaaS?
  6. 7、ReadWriteLock
  7. 解析JVM内存区域组成
  8. 通过OracleDataReader来读取BLOB类型的数据
  9. pinfinder开源下载_BayesianNetworktool
  10. 蓝桥杯基础练习十六进制转十进制
  11. vue ---- vue 的入门程序
  12. Arcgis Javascript那些事儿(一)--Arcgis server发布feature access服务
  13. python是什么专业学的-Python开发专业学校排名是什么样的
  14. StringBuilder类的作用,以及与String类的相互转换
  15. 【cs231】损失函数与优化
  16. ssh 报 You don't exist, go away
  17. Nodejs使用ffi调用so库
  18. 网页设计语言html做思维导图,纯css3实现思维导图样式示例
  19. ensp 防火墙 pat 映射
  20. 《只是为了好玩:Linux之父林纳斯自传》

热门文章

  1. mac catalina删除系统多余文件 内存不足_macOS Catalina Patcher(如何在旧mac上安装Catalina系统)...
  2. Flink异步io应用场景之流表join维表
  3. mkswap,swapon, swapoff命令:创建交换分区
  4. Java后台管理系统,开箱即用
  5. 国产数据库建模工具,看到界面第一眼,良心了! ​
  6. supervisor nginx_Supervisor 的使用和进阶 (3)
  7. android的构成和工作流程,分析Android中View的工作流程
  8. git-ssh-keygen
  9. PHP的 preg_match_all
  10. 如何保证消息不被重复消费啊(如何保证消息消费时的幂等性)?