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 ;
}

POJ 3662 (二分+SPFA的更多相关文章

  1. poj 3621 二分+spfa判负环

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

  2. poj 3621 二分+spfa

    题意:给出一个有向图,问求一个回路,使得回路上的点权之和/边权之和最大. 这题主要是分析出如何确定ans值.我们将(a1*x1+a2*x2+..+an*xn)/(b1*x1+b2*x2+..+bn*x ...

  3. POJ 3662 二分+Dijkstra

    题意: 思路: 二分+Disjktra 二分一个值 如果某条边的边权比它小,则连上边权为0的边,否则连上边权为1的边 最后的d[n]就是最小要免费连接多少电话线. //By SiriusRen #in ...

  4. poj 3662 Telephone Lines spfa算法灵活运用

    意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...

  5. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  6. UVALive 4223 Trucking 二分+spfa

    Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  7. POJ - 2018 二分+单调子段和

    依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...

  8. 洛谷P1462 通往奥格瑞玛的道路(二分+spfa,二分+Dijkstra)

    洛谷P1462 通往奥格瑞玛的道路 二分费用. 用血量花费建图,用单源最短路判断 \(1\) 到 \(n\) 的最短路花费是否小于 \(b\) .二分时需要不断记录合法的 \(mid\) 值. 这里建 ...

  9. POJ 3662 Telephone Lines(二分答案+SPFA)

    [题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...

随机推荐

  1. UITableView分隔线

    问题1: 在ios中使用UITableView时,当行数较少是,可能一屏幕能显示完全所有行,这时候会出现下面的问题,显示多余的分隔线 图如下: 解决方案: //解决方案1 //添加如下代码 -(CGF ...

  2. Mac Postman app使用方法

    Postman是一种网页调试与发送网页http请求的chrome插件.我们可以用来很方便的模拟get或者post或者其他方式的请求来调试接口.本文是使用的Mac端的app.利用第三方平台LeanClo ...

  3. ArcGIS数据存储的方式

    工作空间文件夹和地理数据库为存储和管理ArcGIS地理信息提供了主要容器.工作空间文件夹只是磁盘上的普通文件夹,它可保存大量的文件地理数据库.个人地理数据库.基于文件的数据集以及一系列ArcGIS文档 ...

  4. ArcGisJS的layers-add-result事件总结

    map.on("layers-add-result", initEditing);当地图控件中的所有图层加载完毕之后触发. 注意图层加载完成后返回的的结果:event. funct ...

  5. Annotation版本的HelloWorld

    hiberante 的 annotation历史: 在hibernate3以后,开始支持Annotation; 先有hiberante再有JPA,有了JPA标准之后,hibernate写了Annota ...

  6. Java —类和对象

    类和对象 类是对象的类型,是具有相同的属性和方法的一组对象的集合. 定义类 访问修饰符 class 类名 { 数据类型 属性名: 访问修饰符 返回值类型 方法名 {} } 创建对象:类名 对象名 = ...

  7. TeamViewer 软件完全卸载

    TeamViewer 软件似乎用于商业环境中 - 彻底卸载 Windows 1. 检测为商业用途该软件似乎用于商业环境中.请注意:免费版仅供个人使用.您的会话将在 5 分钟后终止. 2.1 Close ...

  8. Python基础学习之集合

    集合是一个无序.不重复的数据集合,它的主要作用如下: 去重:把一个列表变成集合,就可以去除重复的内容 关系测试:测试两组数据之间的交集.并集.差集等关系 集合常用的操作: #创建一个数值集合 s=se ...

  9. 深入了解MongoDB

    一.介绍: 数据库分为关系型数据库和非关系型数据库 关系型数据库是建立在关系模型上的数据库,主要的有Oracle MySQL Microsoft SQL Server NoSQL是非关系型数据存储的广 ...

  10. 【BZOJ4487】[JSOI2015] 染色问题(高维容斥)

    点此看题面 大致题意: 有一个\(n*m\)的矩形,先让你用\(C\)种颜色给它染色.每个格子可染色可不染色,但要求每行每列至少有一个小方格被染色,且每种颜色至少出现一次.求方案数. 高维容斥 显然题 ...