Telephone Lines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4591   Accepted: 1693

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

 
 
二分第K大长度L,走dij判断,边权w > L 是路径的权为1,否则为0.
 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; #define maxnode 10005
#define edge 100005
#define INF (1 << 30) struct node {
int d,u;
bool operator < (const node& rhs) const {
return d > rhs.d;
}
};
int n,p,k,l,r,_max;
int v[edge * ],w[edge * ],next[edge * ],first[maxnode];
int d[maxnode];
bool done[maxnode]; int dijkstra(int s,int L) {
priority_queue<node> q;
for(int i = ; i <= n; ++i) d[i] = INF;
d[s] = ; node t;
t.d = ,t.u = s;
q.push(t);
memset(done,,sizeof(done)); while(!q.empty()) {
node x = q.top(); q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = ;
for(int e = first[u]; e != -; e = next[e]) {
if(d[ v[e] ] > d[u] + (w[e] > L) ) {
d[ v[e] ] = d[u] + (w[e] > L);
t.d = d[ v[e] ];
t.u = v[e];
q.push(t);
}
}
} return d[n] <= k;
} void addedge(int a,int b,int id) {
int e = first[a];
next[id] = e;
first[a] = id;
} void solve() { l = ;
while(l < r) {
int mid = (l + r) >> ;
if(dijkstra(,mid)) r = mid;
else l = mid + ;
} if(l == _max + ) printf("-1\n");
else
printf("%d\n",r); } int main()
{
//freopen("sw.in","r",stdin); scanf("%d%d%d",&n,&p,&k);
for(int i = ; i <= n; ++i) first[i] = -;
l = INF,_max = ;
for(int i = ; i < * p; i += ) {
int a;
scanf("%d%d%d",&a,&v[i],&w[i]);
_max = max(_max,w[i]);
v[i + ] = a;
w[i + ] = w[i];
addedge(a,v[i],i);
addedge(v[i],a,i + );
} r = _max + ;
solve(); return ;
}

POJ 3662的更多相关文章

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

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

  2. poj 3662(经典最短路)

    题目链接:http://poj.org/problem?id=3662 思路:这题较多的有两种做法: 方法1:二分枚举最大边长limit,如果图中的边大于limit,则将图中的边当作1,表示免费使用一 ...

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

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

  4. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  5. (poj 3662) Telephone Lines 最短路+二分

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

  6. Divide and conquer:Telephone Lines(POJ 3662)

    电话线 题目大意:一堆电话线要你接,现在有N个接口,总线已经在1端,要你想办法接到N端去,电话公司发好心免费送你几段不用拉网线,剩下的费用等于剩余最长电话线的长度,要你求出最小的费用. 这一看又是一个 ...

  7. POJ 3662 Telephone Lines(二分+最短路)

    查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? #include <cstdio> #include <queue> #include <cstring& ...

  8. poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

    Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...

  9. poj 3662 Telephone Lines

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7115   Accepted: 2603 D ...

随机推荐

  1. 算法系列2《RSA》

    1. RSA介绍 RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响 ...

  2. mysql索引合并:一条sql可以使用多个索引

    前言 mysql的索引合并并不是什么新特性.早在mysql5.0版本就已经实现.之所以还写这篇博文,是因为好多人还一直保留着一条sql语句只能使用一个索引的错误观念.本文会通过一些示例来说明如何使用索 ...

  3. SQL开发 循序渐进

    吉庆师傅整理: http://www.itpub.net/thread-1418150-1-1.html

  4. bzoj 1862/1056 [HAOI2008]排名系统

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1862 很恶心的 一道题,我也不晓得自己是第几次写这题了%>_<%. 写了两种方 ...

  5. JavaScript高级程序设计之数值数组排序

    如果数组中全是Nunber类型,则可以按照数值大小排序 , , , , ]; // asc升序函数 function compareAsc(value1, value2) { if (value1 & ...

  6. 前端开发规范之html编码规范

    原则1.规范 .保证您的代码规范,趋html5,远xhtml,保证结构表现行为相互分离.2.简洁.保证代码的最简化,避免多余的空格.空行,保持代码的语义化,尽量使用具有语义的元素,避免使用样式属性和行 ...

  7. JAVA内部类(转)

    源出处:JAVA内部类 在java语言中,有一种类叫做内部类(inner class),也称为嵌入类(nested class),它是定义在其他类的内部.内部类作为其外部类的一个成员,与其他成员一样, ...

  8. java笔试题(3)

    short a = 1; a = a + 1; 有错吗? short a = 1; a += 1; 有错吗? 对于short a = 1; a = a + 1;由于a + 1 运算时会自动提升表达式的 ...

  9. 玩耍Hibernate之缓存

    2.在持久化层,对象分为哪些状态?分别列出来. 答:瞬时态(Transient).持久态(Persistent).脱管态(Detached). 瞬时态(Transient) 是对象是创建时,瞬时对象在 ...

  10. 一、javaSE总结

    1.变量与常量区别: 常量:是在程序中的不会变化的数据. 变量:其实就是内存中的一个存储空间,用于储存常量数据 ,方便运算,因为有些数据不确定,变量空间可以重复使用. 2.变量空间的开辟条件:数据类型 ...