Telephone Lines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8856   Accepted: 3211

Description

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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 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: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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

Source

题意:求一条路径从1到n使第k+1大的边最小。
思路:二分第k+1大的路径长度mid,大于mid的边标记为1,否则标记为0,跑一遍SPFA,当d[n]<=k就为满足条件,取最小的mid。
代码:
 #include"bits/stdc++.h"
#define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
int R()
{
int f=,x=;
char e=getchar();
while(e<''||e>''){if(e=='-')f=-;e=getchar();}
while(e>=''&&e<=''){x=x*+e-'';e=getchar();}
return f*x;
} const int inf=;
int n,m,k;
struct P{int v,dis,nxt;}E[];
int head[N],tot;
int d[],vis[];//数组开太大会T
int l=,r=1e6+,mid; void add(int u,int v,int dis)
{
E[++tot].nxt=head[u];
E[tot].v=v; E[tot].dis=dis;
head[u]=tot;
} void SPFA()
{
memset(d,,sizeof(d)); d[]=;
queue<int> q; q.push();
memset(vis,,sizeof(vis));
while(!q.empty())
{
int u=q.front();
q.pop(); vis[u]=;
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v,dis=(E[i].dis>mid);
if(d[v]>d[u]+dis)
{
d[v]=d[u]+dis;
if(!vis[v])q.push(v),vis[v]=;
}
}
}
} int main()
{
n=R();m=R();k=R();
for(int i=;i<=m;++i)
{
int u=R(),v=R(),dis=R();
add(u,v,dis);add(v,u,dis);
}
int ans=-;
while(l<=r)
{
mid=(l+r)>>;
SPFA();
if(d[n]==inf){ printf("-1"); return ;}
if(d[n]<=k) ans=mid,r=mid-;
else l=mid+;
}
printf("%d",ans);
return ;
}

最新文章

  1. JavaScript随笔6
  2. highcharts相关属性
  3. IOS开发之功能模块--自定义导航控制器类常用自定义的代码
  4. java带图片的邮件发送方法实现
  5. zImage和uImage的区别
  6. Swift构造函数(Initializer)和析构函数(Deinitializer)
  7. ID3算法(Java实现)
  8. 载入DLL中的图片资源生成Skia中的SkBitmap对象
  9. 神奇的Python
  10. fs检测文件夹状态
  11. python/数据类型和变量
  12. Java锁机制了解一下
  13. Git - git branch - 查看远端所有分支
  14. 【SS &amp; KCP centos7配置】
  15. 【linux】linux查找功能
  16. jfinal定时任务插件jfinal-quartz
  17. NYOJ 圈水池
  18. 【MySQL】死锁问题分析
  19. Linux每日小技巧---ss命令
  20. POJ 1860&amp;&amp;3259&amp;&amp;1062&amp;&amp;2253&amp;&amp;1125&amp;&amp;2240

热门文章

  1. vueHistory 模式下,布置到服务器上路由刷新会报nginx404错误
  2. 1064. 计算斐波那契第n项 通项公式
  3. AngularJS 指令解析(二)
  4. HTTP杂记
  5. pcp分布式监控工具
  6. java最大最小堆
  7. python3基础13(format的使用)
  8. SSL Labs: Increased Penalty When TLS 1.2 Is Not Supported
  9. SQL:获取语句执行时间2
  10. 如何实现SQL Server临时表的创建?