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. MySQL安装-windows安装

    windows下安装MySQL 在windows下面安装MySQL 本文以5.7.17为示例 MySQL下载 官网:https://dev.mysql.com/downloads/mysql/ 本次安 ...

  2. 如何重写Java中的equals方法

    Java中,只有8种基本类型不是对象,例如:4种整形类型(byte, short, int,long),2种浮点类型(flout, double),boolean, char不是对象,其他的所有类型, ...

  3. webserver Etcd Cluster / CoreOS etcd / macOS etcd

    s https://coreos.com/etcd/ https://coreos.com/etcd/docs/latest/ macOS mojave etcd 003deMac-mini:~ ma ...

  4. JVM调优命令-jhat

    jhat JVM Heap Analysis Tool命令是与jmap搭配使用,用来分析jmap生成的dump,jhat内置了一个微型的HTTP/HTML服务器,生成dump的分析结果后,可以在浏览器 ...

  5. Coroutine的原理以及实现

    最近在写WinForm,在UI界面需要用到异步的操作,比如加载数据的同时刷系进度条,WinForm提供了不少多线程的操作, 但是多线程里,无法直接修改主线程里添加的UI的get/set属性访问器(可以 ...

  6. h3c mstp的举例

    h3c交换机的图如下: 分别对于SWA,SWB,SWC,SWD,SWE 配置如下: SWA: vlan 10 vlan 20 vlan 30 region-name h3c instance 0 vl ...

  7. nnet3的并行化训练

    num_epochs=1 num_archives=64 args.num_jobs_initial=3 args.num_jobs_final=8 num_iters=2*num_epochs*nu ...

  8. xls表格 ctrl+D 和ctrl+Enter区别 --快速填充相同数据,同时填充多个不同数据

    一.ctrl+Enter应用 如何快速实现下图两个图的填充值效果? ==>效果 1. 选择A列,或者所需要填充的范围 (下面利用 ctrl+G定位应用) 2.Ctrl+G 定位 选择 空值  在 ...

  9. 两个同级div重叠的情况

    一个div使用了position,自身脱离了文本流,另一个顶上去.

  10. (二)Java工程化--Maven实践

    Maven项目版本号 默认版本号: 1.0-SNAPSHOT 最佳实践是约定该版本为不稳定版本,如果发布一定要删除; 建议的版本规则: 主版本号.次版本号.增量版本号- 如:1.0.0-RELEASE ...