poj3662 二分+spfa

27 篇文章 0 订阅
21 篇文章 0 订阅

 

 

如题:http://poj.org/problem?id=3662

Telephone Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5390 Accepted: 1968

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

 

 

思路:二分可能的最大花费,如果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;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值