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.

Input

* 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

Output

* 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.

Sample Input

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

Sample Output

4

题意:n个基站,p条双向边,免费k条路,剩余的路的花费为剩余最大的那条路的权值,求最小花费, 即是求从1到n的路径,使得k+1大的边权尽量小的路径
思路:二分枚举第k+1条路的权值,跑最短路,把所有权值大于该值的边权当作1,其余当作0,如果求出最短路值不超过k,那么继续二分,找到临界值。 另外还有dp做法(留坑
 #include<deque>
#include<cstdio>
#include<cstring>
#include<iostream> using namespace std; int n,p,k;
const int maxn = ;
int head[maxn];
int cnt; struct Node
{
int x,y,val;
int next;
Node(int x=,int y=,int val=,int next = ):x(x),y(y),val(val),next(next){}
}node[maxn<<]; void add(int x,int y,int val)
{
node[++cnt].x = x;
node[cnt].y = y;
node[cnt].val = val;
node[cnt].next = head[x];
head[x] = cnt;
} bool vis[maxn];
int dist[maxn];
bool bfs(int mid)
{
deque<int>que;
while(!que.empty())que.pop_back();
que.push_back();
memset(vis,,sizeof(vis));
memset(dist,0x3f,sizeof(dist));
dist[] = ;
vis[] = ;
while(!que.empty())
{
int s = que.front();
que.pop_front();
if(s == n)return dist[n] <= k;
for(int i=head[s];i;i=node[i].next)
{
int to = node[i].y;
int val = node[i].val;
if(val <= mid)
{
dist[to] = min(dist[to],dist[s]);
if(!vis[to])que.push_front(to);
}
else
{
dist[to] = min(dist[to],dist[s]+);
if(!vis[to])que.push_back(to);
}
vis[to] = ;
} }
return ;
}
int cal(int r)
{
int l = ;
int ans = -;
while(l <= r)
{
int mid = (l+r)>>;
if(bfs(mid))r = mid - ,ans = mid;
else l = mid + ;
}
return ans;
} int main()
{
scanf("%d%d%d",&n,&p,&k);
int maxx = cnt = ;
for(int i=;i<=p;i++)
{
int u,v,val;
scanf("%d%d%d",&u,&v,&val);
add(u,v,val);
add(v,u,val);
maxx = max(maxx,val);
}
int ans = cal(maxx);
printf("%d\n",ans);
}

Telephone Lines POJ - 3662 (二分+spfa)的更多相关文章

  1. Divide and conquer:Telephone Lines(POJ 3662)

    电话线 题目大意:一堆电话线要你接,现在有N个接口,总线已经在1端,要你想办法接到N端去,电话公司发好心免费送你几段不用拉网线,剩下的费用等于剩余最长电话线的长度,要你求出最小的费用. 这一看又是一个 ...

  2. 【bzoj1614】[Usaco2007 Jan]Telephone Lines架设电话线 二分+SPFA

    题目描述 Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1 ...

  3. (poj 3662) Telephone Lines 最短路+二分

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

  4. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...

  5. POJ - 3662 Telephone Lines (dijstra+二分)

    题意:有N个独立点,其中有P对可用电缆相连的点,要使点1与点N连通,在K条电缆免费的情况下,问剩下的电缆中,长度最大的电缆可能的最小值为多少. 分析: 1.二分临界线(符合的情况的点在右边),找可能的 ...

  6. BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线:spfa + 二分【路径中最大边长最小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1614 题意: 给你一个无向图,n个点,m条边. 你需要找出一条从1到n的路径,使得这条路径 ...

  7. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  8. [Usaco2007 Jan]Telephone Lines架设电话线[二分答案+最短路思想]

    Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...

  9. bzoj 1614 Telephone Lines架设电话线 - 二分答案 - 最短路

    Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...

随机推荐

  1. IDEA的 mybatis插件报错 - IDE Fatal Errors

    IDE Fatal Errors Exception in plugin Mybatis plugin. A minute ago. Occurred once since the last clea ...

  2. H5下拉刷新和上拉加载实现原理浅析

    前言 在移动端H5网页中,下拉刷新和上拉加载更多数据的交互方式出现频率很高,开源社区也有很多类似的解决方案,如iscroll,pulltorefresh.js库等.下面是对这两种常见交互基本实现原理的 ...

  3. [BJOI2019]勘破神机

    [BJOI2019]勘破神机 推式子好题 m=2,斐波那契数列,$f_{n+1}$项 不妨$++l,++r$,直接求$f_n$ 求$\sum C(f_n,k)$,下降幂转化成阶乘幂,这样都是多项式了, ...

  4. BZOJ 3674 可持久化并查集

    https://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化数组维护并查集的fa数组, 查询时间复杂度为nlognlogn,一个log是并查集 ...

  5. Gaussian Process for Regression

    python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...

  6. Hadoop基础-镜像文件(fsimage)和编辑日志(edits)

    Hadoop基础-镜像文件(fsimage)和编辑日志(edits) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看日志镜像文件(如:fsimage_00000000000 ...

  7. Kubernetes之存储

    存储卷概述 容器磁盘上的文件的生命周期是短暂的,这就使得在容器中运行重要应用时会出现一些问题.首先,当容器崩溃时,kubelet 会重启它,但是容器中的文件将丢失——容器以干净的状态(镜像最初的状态) ...

  8. dubbo本地服务化实现(dubbo三)

    一.dubbo服务化架构包含的内容 对于传统工程而言,分层的依据是按照包来区分.由于在相同的工程中,所以服务的提供和调用可以方便的实现. 但是对于分布式架构而言,服务的提供者负责服务具体的实现和接口规 ...

  9. DirectX11 With Windows SDK--13 动手实现一个简易Effects框架、阴影效果绘制

    前言 到现在为止,所有的教程项目都没有使用Effects11框架类来管理资源.因为在D3DCompile API (#47)版本中,如果你尝试编译fx_5_0的效果文件,会收到这样的警告: X4717 ...

  10. java树形菜单实现

    java树形菜单实现 公司表: 部门表: 实体类: public class Node { private Integer companyId;//公司id private String compan ...