poj3662 二分+spfa

来源:互联网 发布:乐高编程机器人 编辑:程序博客网 时间:2024/05/16 03:10

 

 

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

Telephone Lines
Time Limit: 1000MS Memory Limit: 65536KTotal 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 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

USACO 2008 January Silver

 

 

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

 

0 0
原创粉丝点击