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. WPF socket通讯 UDP接收消息

    客户端: private Socket socket; private IPEndPoint ipEndPoint; private void sendMessageHandler() { //服务端 ...

  2. EMVTag系列13《脱机PIN》

    DGI8010用于个人化借记贷记交易中使用的脱机PIN.数据强制要求加密.制卡数据传输过程中,此DGI采用DEK加密保护. 数据分组标识 '8010'的数据内容       要求            ...

  3. Python学习——struct模块的pack、unpack示例

    he struct module includes functions for converting between strings of bytes and native Python data t ...

  4. 微价值:专访《甜心爱消除》的个人开发者Lee,日入千元

    [导语] 我们希望能够对一些个人开发者进行专访,这样大家更能显得接地气,看看人家做什么,怎么坚持.<甜心爱消除>作者Lee是三群的兄弟,也关注微价值.微价 值的文章还是可以的,得到一些业内 ...

  5. pb datawindow 判断是否是最后一列最后一行

    li_column1 = GetColumn() ls_columnname = GetColumnName() Send(Handle(This),,,Long(,)) ll_row2 = GetR ...

  6. 双系统下利用MbrFix.exe卸载LINUX系统

    前言:  不少同学笔记本都装的有双系统,一般都是LIUNX和WINDOWS的两个系统(由于以前对电脑各种无知)装了双系统,再次,小编就不在阐述双系统地各种不便,再次就强调一下,假若要卸载LINUX的话 ...

  7. 状压DP

    今天稍微看了下状压DP,大概就是这样子的,最主要的就是位运算, i and (1<<k)=0 意味着i状态下没有 k : i and (1<<k)>0 意味着i状态下有 ...

  8. RSA算法详解

    1.RSA加密算法是最常用的非对称加密算法 2.RSARSA以它的三个发明者Ron Rivest, Adi Shamir, Leonard Adleman的名字首字母命名, 3.目前学术界无法证明RS ...

  9. 20145103 《Java程序设计》第2周学习总结

    20145103 <Java程序设计>第2周学习总结 教材学习内容总结 在第三章主要学习了Java语言中的类型及其变量主要类型为:整数(1字节的byte,2字节的short,4字节的int ...

  10. Travis-CI的进一步使用

    今天主要对.travis.yml文件和makefile进行进一步的了解: 1.在.travis.yml文件中添加了给linux系统中安装了cppunit库的语句,使能够持续集成写过的单元测试的代码.主 ...