poj3662 二分+spfa
来源:互联网 发布:乐高编程机器人 编辑:程序博客网 时间:2024/05/16 03:10
如题:http://poj.org/problem?id=3662
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 ofP (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 andBi, 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 andN 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, andLi
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 11 2 53 1 42 4 83 2 35 2 93 4 74 5 6
Sample Output
4
Source
思路:二分可能的最大花费,如果x是最大花费,那么在图中满足
1.图要从1-n至少有一条路径。
2.在所有路径中,>x的边最少的那条路径的边数必须<=k。
因此,将小于等于x的边权看做0,>x的边看做1.求最短路。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 1005
typedef pair<int,int>P;
#define inf 0x0fffffff
#define min(a,b)(a<b?a:b)
struct edge
{
int to,cost;
edge(){}
edge(int a,int b):to(a),cost(b){}
};
vector<edge>G[MAXN];
bool C(int x,int n,int k)
{
int dist[MAXN]={0};
fill(dist,dist+n+1,inf);
dist[1]=0;
priority_queue<P,vector<P>,greater<P> >que;
que.push(P(0,1));
while(!que.empty())
{
P p=que.top();
que.pop();
int v=p.second;
if(dist[v]<p.first)
continue;
int i;
for(i=0;i<G[v].size();i++)
{
edge e=G[v][i];
if(dist[e.to]>dist[v]+bool(e.cost>x))
{
dist[e.to]=dist[v]+bool(e.cost>x);
que.push(P(dist[e.to],e.to));
}
}
}
return dist[n]<=k;
}
int main()
{
// freopen("C:\\1.txt","r",stdin);
int n,m,k;
cin>>n>>m>>k;
int i;
for(i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
G[u].push_back(edge(v,w));
G[v].push_back(edge(u,w));
}
int l=-1,r=inf;
int ans=inf;
while(r-l>1)
{
int mid=(l+r)/2;
if(C(mid,n,k))
{
ans=min(ans,mid);
r=mid;
}
else
l=mid;
}
if(ans==inf)
cout<<-1<<endl;
else
cout<<ans<<endl;
return 0;
}
- 二分+SPFA--poj3662
- poj3662 二分+spfa
- POJ3662 二分+最短路
- poj3662,二分 + 最短路,好题啊
- poj3662
- poj3662
- POJ3662
- POJ3662 Telephone Lines 二分+最短路
- POJ3662 Telephone Lines [二分 最短路]
- POJ3662 Telephone Lines 二分+最短路
- 二分+SPFA
- poj 3621 二分+spfa
- HDU 2962(SPFA+二分)
- hdu2962 二分 + spfa
- uva11090(二分spfa)
- poj3621 二分+spfa
- hdu1839 spfa+二分查找
- poj3662 dij
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
- HDU 2473-Junk-Mail Filter-并查集删除
- HDU 1061 Rightmost Digit
- sscanf用法详解
- Tcp通信中服务器处理客户端意外断开 / 10054
- poj3662 二分+spfa
- Codeforces 380C. Sereja and Brackets(模拟啊)
- jquery 元素控制(追加元素/追加内容)介绍及应用
- 新浪微博新兵训练营系列课程——平台RPC框架介绍
- 导航栏和状态栏的颜色设置
- 在界面条件检索时设置默认时间
- linux cp命令批量复制不提示方法
- iOS中设置view的边界
- Java IO完全总结