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. xadmin后台 导入 excel 功能拓展

    新建 excel 文件 在 xadmin 的 plugins 下添加一个 excel.py # _*_ coding:utf-8 _*_ __author__ = "yangtuo" ...

  2. MYSQL 企业常用架构与调优经验分享

    一.选择Percona Server.MariaDB还是MYSQL  mysql应用源码:http://www.jinhusns.com/Products/Download/?type=xcj 1.M ...

  3. 不得不用的提高效率小技巧让你用Mac更顺手| Mac小技巧(三)

    文章内容及图片来源于:知乎,如果涉及版权问题,请联系作者删除 文章收录于:风云社区(提供上千款各类mac软件的下载) 1. 用预览给GIF删帧 我们在给文章配图或者做表情包的过程中,常需要截取 GIF ...

  4. 金融量化分析【day112】:均值回归策略

    一.均值回归策略 1.什么是回归策略 二.归一标准化 import numpy as np a = np.random.uniform(100,5000,1000) b = np.random.uni ...

  5. 静态IP设置

    先查看自动网络的ip地址,然后设置 cmd进入DOS输入命令:ipconfig /all 设置固定IP

  6. 液晶流在齐次 Besov 空间中的正则性准则

    在 [Zhang, Zujin. Regularity criteria for the three dimensional Ericksen–Leslie system in homogeneous ...

  7. 同步Name到Comment 及 同步 Comment 到Name

    在 PowerDesigner执行命令  Tools->Execute Commands->Edit/Run Scripts 代码一:将Name中的字符COPY至Comment中 Opti ...

  8. java8 按条件过滤集合

    //黄色部分为过滤条件list.stream().filter(user-> user.getId() > 5 && "1组".equals(user. ...

  9. 设计模式六: 模板方法(Template Method)

    简介 模板方法属于行为型模式的一种. 实现层面上, 在抽象类中定义了算法或流程的骨架, 将其中易变的部分延迟到子类实现, 也就是允许它的子类实现其中的某些步骤. 模板方法适用于算法不变, 但算法中某些 ...

  10. 不用写代码的框架 - RobotFramework+Eclispe环境安装篇

    环境安装是学习任何一个新东西的第一步,这一步没走舒坦,那后面就没有心情走下去了. 引用名句:工欲善其事必先利其器!! Robotframework:一款 自动化测试框架. Eclipse:一款编辑工具 ...