二分+SPFA--poj3662

来源:互联网 发布:淘宝运营助理工作职责 编辑:程序博客网 时间:2024/06/05 05:27
Language:
Telephone Lines
Time Limit: 1000MS Memory Limit: 65536KTotal 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 11 2 53 1 42 4 83 2 35 2 93 4 74 5 6

Sample Output

4思路:二分答案然后构图,比mid小的边权值为0,否则为,1,然后求最短路,如果比K大,则返回false,否则韩慧true。下面是代码:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>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;i<P;i++)    {        if(f[i]>mid)            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<int> 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;i<P;i++)        {            scanf("%d%d%d",&a[i],&b[i],&f[i]);            if(f[i]>max1)                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<<l<<endl;    }    return 0;}


原创粉丝点击