题目描述

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.

多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着 1≤N≤1000 根据 1⋯N 顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有 1≤p≤10000 对电话杆可以拉电话线。其他的由于地震使得无法连接。

第i对电线杆的两个端点分别是 ai,bi​,它们的距离为 1≤li≤1000000。数据中每对 (ai,bi) 只出现一次。编号为 1 的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号 N 的电话线杆上。也就是说,笨笨的任务仅仅是找一条将 1 号和 N 号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。

电信公司决定支援灾区免费为此市连接 k 对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过 k 对,那么支出为 0。

请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?

输入格式

输入文件的第一行包含三个数字 n,p,k。

第二行到第 p+1 行,每行分别都为三个整数 ai,bi,li。

输出格式

一个整数,表示该项工程的最小支出,如果不可能完成则输出 -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

输出 #1

4

思路:

首先我们可以明确题要的是: 最大值的最小值

这恰是二分的标志!!!

如何二分 or 二分什么:

不难发现是要二分答案

如何写check函数(难点):

注:mid是二分中用于check的值

我们可以这样想:如果价格小于等于mid,那我们就把边权设置为0;

如果价格大于mid,那我们就把边权设置为1;

边权0 or 1代表什么:0 --> 不需要免费(不消耗k),1 --> 需要免费(消耗k)

跑最短路,如果结果 小于等于 k 则check 为 true;

如果结果 大于 k 则check 为 false;

AC代码如下:

#include <bits/stdc++.h>
#define buff                     \ios::sync_with_stdio(false); \cin.tie(0);                  \cout.tie(0)
#define endl "\n"
using namespace std;
const int N = 20009;
int dist[N];
int ne[N], h[N], e[N], w[N], idx;
bool st[N];
int n, m, k;
void add(int a, int b, int c)
{idx++;e[idx] = b;ne[idx] = h[a];w[idx] = c;h[a] = idx;
}
bool spfa(int x)
{int val;for (int i = 1; i <= n; i++){st[i] = 0;dist[i] = 0x3f3f3f3f;}queue<int> q;dist[1] = 0;st[1] = 1;q.push(1);while (q.size()){int t = q.front();q.pop();st[t] = 0;for (int i = h[t]; i; i = ne[i]){int j = e[i];if (w[i] - x > 0)val = 1;elseval = 0;if (dist[j] > dist[t] + val){dist[j] = dist[t] + val;if (!st[j]){st[j] = 1;q.push(j);}}}}if (dist[n] <= k)return 1;elsereturn 0;
}
int main()
{buff;cin >> n >> m >> k;for (int i = 1; i <= m; i++){int a, b, c;cin >> a >> b >> c;add(a, b, c);add(b, a, c);}//二分答案int l = 0, r = 1000000;bool is_ok = 0;while (l < r){int mid = (l + r) >> 1;if (spfa(mid)){r = mid;is_ok = 1;}elsel = mid + 1;}if (is_ok)cout << l << endl;elsecout << -1 << endl;
}

P1948 [USACO08JAN]Telephone Lines S(二分+spfa)相关推荐

  1. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines

    P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. ...

  2. bzoj 1614: [Usaco2007 Jan]Telephone Lines架设电话线(二分+SPFA)

    1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1761  Solve ...

  3. [BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

    1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1806  Solve ...

  4. [BZOJ1614][Usaco2007 Jan]Telephone Lines架设电话线

    [Usaco2007 Jan]Telephone Lines架设电话线 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Farmer John打算将电话线引到自己的农场,但电信公司并不打算 ...

  5. UVALive 4223 Trucking 二分+spfa

    Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  6. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  7. 牛客假日团队赛8:F.Telephone Lines(二分+spfa)

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

  8. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines【NOIP模拟笨笨的电话线】

    [二分,spfa chenck] 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the p ...

  9. poj 3662 Telephone Lines spfa算法灵活运用

    意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...

最新文章

  1. 矩阵论习题:设A,B为投影矩阵,证明A+B仍为投影矩阵当且仅当AB=BA=0。
  2. 英伟达3080Ti、3070Ti来了:继续封锁挖矿性能,网友:不信,空气卡+1
  3. 交换机出现err-disable的原因及解决方法
  4. 四张图揭秘中国AI人才现状
  5. 接口 Swagger 部分Web API的隐藏
  6. 2017职称英语和计算机考试,2017年职称英语考试取消了吗
  7. oracle函数总结
  8. async spring 默认线程池_Spring定时任务高级使用篇
  9. python生活中的小问题_python日常注意小知识集锦
  10. [编程语言]C陷阱与缺陷
  11. mysql data masking_Percona8.0.17的数据屏蔽插件的使用
  12. 8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码。
  13. 时域信号的频谱、功率谱和功率谱密度计算
  14. thinkphp3.2.3 支付宝授权登录php
  15. 28岁自学编程会不会太晚了?靠谱吗?
  16. Mentor Graphics LP Wizard 软件使用----创建零件BGA封装库
  17. Excel 宏录制与VBA编程 —— 3、第一个VBA弹窗代码(附视频)
  18. DAMA学习笔记1-3
  19. JAVA基础之while 循环(小白必看!!!)
  20. 面对初学者的CAN总线入门教程(三)_CAN通信中帧、优先级、位填充、错误、位时序以及同步的介绍

热门文章

  1. 一台服务器搭建部署两个或多个Redis实例
  2. JavaScript性能优化【中】-- Performance 工具
  3. windows git密码 删除
  4. mac chrome 打包扩展程序
  5. 斐讯N1刷Armbian_5.62,无法自动获取到ip怎么办?
  6. 用js实现鼠标点击爱心特效
  7. C#LeetCode刷题之#13-罗马数字转整数(Roman to Integer)
  8. 了解如何使用ASP.NET Core 3.1构建Web应用程序
  9. node js 技术架构_[视频] Node JS中的干净架构
  10. 软件设计师 一年考几次_一年写106篇文章如何帮助我成长为设计师