二分+SPFA--poj3662

Language: Default
Telephone Lines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4397   Accepted: 1607

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

思路:二分答案然后构图,比mid小的边权值为0,否则为,1,然后求最短路,如果比K大,则返回false,否则韩慧true。
下面是代码:
#include
#include
#include
#include
using namespace std;
const int MAX=11000;
const int INF=100000;
struct node
{
    int to,len,next;
}edge[MAX];
int pre[1010],dis[1010];
bool vis[1010];
int N,P,K;
int num;
int a[MAX],b[MAX],f[MAX];
void init()
{
    memset(pre,-1,sizeof(pre));
    num=0;
}
void add_edge(int a,int b,int f)
{
    edge[num].to=b;
    edge[num].next=pre[a];
    edge[num].len=f;
    pre[a]=num++;
    edge[num].to=a;
    edge[num].next=pre[b];
    edge[num].len=f;
    pre[b]=num++;
}
void build(int mid)
{
    init();
    for(int i=0;imid)
            add_edge(a[i],b[i],1);
        else
            add_edge(a[i],b[i],0);
    }
}
bool spfa()
{
    for(int i=1;i<=N;i++)
    {
        vis[i]=false;
        dis[i]=INF;
    }
    queue q;
    q.push(1);
    dis[1]=0;
    vis[1]=true;
    while(!q.empty())
    {
        int t=q.front();
        vis[t]=false;
        q.pop();
        for(int i=pre[t];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dis[v]>dis[t]+edge[i].len)
            {
                dis[v]=dis[t]+edge[i].len;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=true;
                }
            }
        }
    }
    if(dis[N]>K)
        return false;
    return true;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(cin>>N>>P>>K)
    {
        //init();
        int max1=0;
        for(int i=0;imax1)
                max1=f[i];
        }
        int l=0,r=max1,mid;
        while(r>l)
        {
            mid=(l+r)/2;
            build(mid);
            bool flag=spfa();
            if(dis[N]==INF)
            {
                l=-1;
                break;
            }
            if(flag)
                r=mid;
            else
                l=mid+1;
        }
        cout<


你可能感兴趣的:(二分搜索,最佳路径)