POJ 3662
| 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 {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 <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的更多相关文章
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...
- poj 3662(经典最短路)
题目链接:http://poj.org/problem?id=3662 思路:这题较多的有两种做法: 方法1:二分枚举最大边长limit,如果图中的边大于limit,则将图中的边当作1,表示免费使用一 ...
- POJ 3662 Telephone Lines(二分答案+SPFA)
[题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- Divide and conquer:Telephone Lines(POJ 3662)
电话线 题目大意:一堆电话线要你接,现在有N个接口,总线已经在1端,要你想办法接到N端去,电话公司发好心免费送你几段不用拉网线,剩下的费用等于剩余最长电话线的长度,要你求出最小的费用. 这一看又是一个 ...
- POJ 3662 Telephone Lines(二分+最短路)
查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? #include <cstdio> #include <queue> #include <cstring& ...
- poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)
Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...
- poj 3662 Telephone Lines
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7115 Accepted: 2603 D ...
随机推荐
- ORA-14099 错误解决
DB: 11.2.0.3.0 在测试把普通表修改为交换分区的时候,出现ORA-14099: all rows in table do not qualify for specified partiti ...
- hdu 1250 Hat's Fibonacci
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Description A Fibonacci sequence ...
- GoAhead 嵌入式web
https://embedthis.com/goahead/ 入手了一个360 4g wifi :使用jquery .goahead Goahead webserver编辑 GoAhead We ...
- C#类型的转换:Converter<TInput, TOutput> 委托的使用
Converter<TInput, TOutput> 委托 表示将对象从一种类型转换为另一种类型的方法. 此委托由 Array 类的 ConvertAll<TInput, TOutp ...
- (转)android Fragments详解一:概述
Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment.你 ...
- verilog实现16位五级流水线的CPU带Hazard冲突处理
verilog实现16位五级流水线的CPU带Hazard冲突处理 该文是基于博主之前一篇博客http://www.cnblogs.com/wsine/p/4292869.html所增加的Hazard处 ...
- MVC Controller 链接到 API Controller 以及反向链接
MVC Controller 链接到 API Controller 以及反向链接 问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controll ...
- 深入理解CSS3 animation的steps
在应用 CSS3 渐变/动画时,有个控制时间的属性 <timing-function> .它的取值中除了常用到的三次贝塞尔曲线以外,还有个让人比较困惑的 steps() 函数. steps ...
- ListView单击单元格 产生其他控件
以combobox为例. 假如一行里面只有一个combobox. //在类中声明一个控件数组 private ComboBox[] cmds = null; //initview中调用dao组件获得显 ...
- 【Exception】 java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.setOnDismissListener
f(Build.VERSION.SDK_INT >10) builder =newAlertDialog.Builder(getActivity(), R.style.Theme.Sherloc ...