• poj 3662 Telephone Lines dijkstra+二分搜索


    Telephone Lines
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5696   Accepted: 2071

    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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 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: NP, and K
    * Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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

    题意:给你n个节点,p条路(不一定每个节点之间都有路),每条路都对应一定的花费,现在修路公司可以免费为你修k条路,求不是免费的路中花费最大的一条的花费;
     
    错因:分析出了这道问题其实就是最小化第k+1个数,然后一直在往这个方向是上想,又知道求数组中第k大数只要>x的数量>=k的最大x+1,就好,
     
    分析:这道题很典型啊,
     http://blog.163.com/boatswain%40126/blog/static/169396412201071784521862/  分析的很到位
    想像一个单调的函数,就不难写二分了,不过最后要注意区间的开闭性会影响到左右端点的初始化

     总结二分搜索的两句话:1:球数组中的第k小数,就是求<x的数量>=k的最小x-1;

          2.求数组中的第k大数,就是求>=x的数量>=k的最大x

    最后非常重要的是:这两个求出来的数在数组中一定是存在的。

    这个总结几乎可以概括来这段时间做的所有题了

    #include<cstdio>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include<map>
    #include <algorithm>
    #include <set>
    using namespace std;
    #define MM(a) memset(a,0,sizeof(a))
    typedef long long LL;
    typedef unsigned long long ULL;
    const int mod = 1000000007;
    const double eps = 1e-10;
    const int inf = 0x3f3f3f3f;
    const double g = 10;
    int tu[1005][1005], tu2[1005][1005], d[1005], used[1005];
    int n, p, k, f, t, c;
    void init2(int mid)
    {
        memset(used, 0, sizeof(used));
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (tu[i][j] == inf)
                    tu2[i][j] = inf;
                else if (tu[i][j] >mid)
                    tu2[i][j] = 1;
                else
                    tu2[i][j] = 0;
                for (int i = 1; i <= n; i++)
                    d[i] = tu2[1][i];
                d[1] = 0; used[1] = 1;
    }
    int ok(int mid)
    {
        init2(mid);
        while (1)
        {
            int u = 0, minn = inf;
            for (int i = 1; i <= n; i++)
                if (d[i]<minn&&!used[i])
                {
                    minn = d[i];
                    u = i;
                }
            if (!u)  break;
            used[u] = 1;
            for (int i = 1; i <= n; i++)
                if (d[i]>d[u] + tu2[u][i] && !used[i])
                    d[i] = d[u] + tu2[u][i];
        }
        if (d[n] >= inf)
            return -2;
        else return d[n] <= k;
    }
    void init1()
    {
        memset(tu, inf, sizeof(tu));
        for (int i = 1; i <= n; i++)
            tu[i][i] = 0;
    }
    int main()
    {
        while (~scanf("%d %d %d", &n, &p, &k))
        {
            int l=-1, r = 0;
            init1();
            for (int i = 1; i <= p; i++)
            {
                scanf("%d %d %d", &f, &t, &c);
                tu[f][t] = tu[t][f] = c;
                if (c>r) r = c;
            }
             int flag = 1;
            while (r - l>1 && flag)
            {
                int mid = (l + r) >> 1;
                int w = ok(mid);
                if (w == 1)
                    r = mid;
                else if (w == 0)
                    l = mid;
                else if (w == -2)
                {
                    printf("-1
    ");
                    flag = 0;
                }
            }
            if (flag)
                printf("%d
    ", r);
        }
        return 0;
    } 

      下面是第一次wa的代码:

    #include<cstdio>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include<map>
    #include <algorithm>
    #include <set>
    using namespace std;
    #define MM(a) memset(a,0,sizeof(a))
    typedef long long LL;
    typedef unsigned long long ULL;
    const int mod = 1000000007;
    const double eps = 1e-10;
    const int inf = 0x3f3f3f3f;
    const double g=10;
    int tu[1005][1005],tu2[1005][1005],d[1005],used[1005];
    int n,p,k,f,t,c;
    int ok(int mid)
    {
        memset(tu2,inf,sizeof(tu2));
        memset(d,inf,sizeof(d));
        memset(used,0,sizeof(used));
        for(int i=1;i<=n;i++)
          for(int j=1;j<=n;j++)
           if(tu[i][j]!=inf)
            if(tu[i][j]>mid)
              tu2[i][j]=1;
            else
              tu2[i][j]=0;
        d[1]=0;used[1]=0;
        while(1)
        {
            int u=0,minn=inf;
            for(int i=1;i<=n;i++)
                if(d[i]<minn&&!used[i])
                {
                    minn=d[i];
                    u=i;
                }
            if(!u)
                break;
            used[u]=1;
            for(int i=1;i<=n;i++)
                if(d[i]>d[u]+tu2[u][i]&&!used[i])
                   d[i]=d[u]+tu2[u][i];
        }
        return d[n]>=k;
    }
    int main()
    {
        while(~scanf("%d %d %d",&n,&p,&k))
        {
            int l=0,r=0;
            memset(tu,inf,sizeof(tu));
            for(int i=1;i<=p;i++)
            {
                scanf("%d %d %d",&f,&t,&c);
                tu[f][t]=tu[t][f]=c;
                if(c>r) r=c;
            }
            r++;
            while(r-l>1)
            {
                int mid=(l+r)>>1;
                if(ok(mid))
                    l=mid;
                else
                    r=mid;
            }
            printf("%d
    ",l+1);
        }
        return 0;
    }
  • 相关阅读:
    自定义滚动条mCustomScrollbar
    怒怼外媒,为中国正名,这个《流浪地球》捧红的犹太小哥太励志了
    Memcached部署和用法
    SpringMVC中向服务器传递时间参数时出现的问题
    spring4 mvc 出错
    【转】ContextLoaderListener 和 DispatcherServlet
    【转】Myeclipse建立Maven项目
    【转】Docker 常用命令
    docker push到本地仓库失败
    转:Docker学习---挂载本地目录
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5136031.html
Copyright © 2020-2023  润新知