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