• POJ3662 Telephone Lines (dijkstra+二分)


    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
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <cmath>
    #include <cstring>
    using namespace std;
    int n,p,k;
    int d[1005];
    int head[1005],ver[20005],edge[20005],Next[20005];//开两倍存储双向边 
    bool v[1005];
    int tot=0;
    priority_queue<pair<int,int> >q;
    void add(int x,int y,int z)
    {
        ver[++tot]=y;
        edge[tot]=z;
        Next[tot]=head[x];
        head[x]=tot;
    }
    int dijkstra(int mid)//花费小于等于mid记为0 否则记为1 //只需要为一条路买单 //dij返回的是大于mid的数 
    {
        int cnt=0;
        memset(d,0x3f,sizeof(d));
        memset(v,0,sizeof(v));
        d[1]=0;
        q.push(make_pair(0,1));
        int i,j;
        while(q.size())
        {
            int x=q.top().second;
            q.pop();
            if(v[x])continue;
            v[x]=1;
            for(i=head[x];i;i=Next[i])
            {
                int y=ver[i],z=edge[i];
                int z1;
                if(z<=mid)z1=0;
                else z1=1;
                if(d[y]>d[x]+z1)// 注意 这里要求的最短路并不是原来费用的最短路 
                {
                    d[y]=d[x]+z1;
                    q.push(make_pair(-d[y],y));
                }
            }
        }
        return d[n];    
    }
    bool check(long long mid)
    {
        if(dijkstra(mid)<=k)
        {
            return true;
        }
        else return false;
    }
    int main()
    {
        int i;
        memset(v,0,sizeof(v));
        scanf("%d%d%d",&n,&p,&k);
        for(i=1;i<=p;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        long long l=0,r=1000002,mid;
        while(l<r)
        {
            mid=(l+r)>>1;
            if(check(mid))//钱数够了缩小 
            {
                r=mid;
            }
            else//钱数不够扩大 
            {
                l=mid+1;
            }
        }
        if(r>1000000)cout<<-1<<endl;
        else if(r<0)cout<<0<<endl; 
        else cout<<l<<endl;
        return 0;
    }
  • 相关阅读:
    UNIT THREE
    UNIT TWO
    UNIT ONE
    实验九 根据材料编程
    实验五 编写、调试具有多个段的程序
    实验 四 [bx]和loop的使用
    实验二 用机器指令和汇编指令编程
    实验三 编程、编译、连接、跟踪
    实验一 查看CPU和内存,用机器指令和汇编指令编程
    汇编语言第5~8章知识总结
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12293686.html
Copyright © 2020-2023  润新知