zoukankan      html  css  js  c++  java
  • 【POJ 3662】Telephone Lines

    Telephone Lines
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5413   Accepted: 1976

    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

     
    这道题的解法挺巧妙的,开始并没有想到是这样的,算是长长见识,题解是USACO的官方思路。
    二分答案+检查;
    对于一条边,如果这条边是答案,那么对于经过这条边的1~N的某一条路径上大于这条边的权值的边数一定不大于K。
    我们可以用类似spfa的思想来检查答案。
    设dis[i]是1~i最少大于w(当前边的权值)的边数。那么就看dis[N]符不符合。
    当然,这个时候可能会有人想不明白,为什么求出dis[N]的路径一定经过e(当前边)。答案是否定的。
    但这并不影响我们求解。因为如果dis[N] > k,那么就说明了无论怎么走,这条边都不会是答案。(想一想为什么?)
    如果dis[N] <= k 但求出这个解的路径并没有经过e,那么说明了这个一定不是最优的答案。(想一想为什么?)
    解决了以上两个问题之后,你就可以开始敲代码啦。啦啦啦~~~~~~
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    const int MAXP = 10005;
    const int MAXN = 1005;
    
    struct Edge
    {
        int from;
        int to;
        int weight;
        int next;
        
        Edge() {}
        
        Edge(int u, int v, int w) : from(u), to(v), weight(w) {}
    
    }edges[MAXP << 1];
    
    int cost[MAXP];
    int dis[MAXN];
    int first[MAXN];
    int Q[MAXN];
    int h;
    int t;
    int tots;
    int n;
    int p;
    int k;
    int level;
    bool v[MAXN];
    
    void add(int u, int v, int w)
    {
        edges[tots] = Edge(u, v, w);
        edges[tots].next = first[u];
        first[u] = tots++;
    }
    
    void dfs(int x, int standard)
    {
        v[x] = false;
        dis[x] = level;
        Q[t++] = x;
        for (int i = first[x]; i != -1; i = edges[i].next)
        {
            Edge &e = edges[i];
            if (v[e.to] && e.weight <= standard) dfs(e.to, standard);
        }
    }
    
    void bfs(int standard)
    {
        memset(v, true, sizeof(v));
        memset(dis, 0, sizeof(dis));
        h = t = 0;
        level = 0;
        dfs(1, standard);
        while (h < t)
        {
            int x = Q[h++];
            for (int i = first[x]; i != -1; i = edges[i].next)
            {
                Edge &e = edges[i];
                if (v[e.to])
                {
                    level = dis[x] + 1;
                    dfs(e.to, standard);
                }
            }
        }
    }
    
    void binarySearch(int l, int r)
    {
        if (l == r)
        {
                printf("%d
    ", cost[l]);
                return;
        }
        int mid = (l + r) >> 1;
        bfs(cost[mid]);
        if (dis[n] <= k) binarySearch(l, mid);
        else binarySearch(mid + 1, r);
    }
    
    int main()
    {
        scanf("%d%d%d", &n, &p, &k);
        cost[0] = 0;
        memset(first, -1, sizeof(first));
        tots = 0;
        for (int i = 1; i <= p; ++i)
        {
            int a;
            int b;
            int c;
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, c);
            add(b, a, c);
            cost[i] = c;
        }
        bfs(0);
        if (dis[n] == 0) printf("-1
    ");
        else if (dis[n] <= k) printf("0
    ");
            else
            {
                sort(cost + 1, cost + p + 1);    
                binarySearch(1, p);
            }
        return 0;
    }
  • 相关阅读:
    CloudFoundry 中的GoRouter性能測试
    Android-SharedPreferences
    Oracle 闪回表实验
    使用Jfree实现吧条形图,java代码
    ZOJ Monthly, October 2010 ABEFI
    熟知CDN
    看,2015阿里巴巴视觉设计人员面临的问题招收学校
    于win7使用虚拟磁盘隐藏文件
    2015广东工业大学ACM学校巡回赛 I 游戏高手 (如压力dp)
    spring mvc 错误摘要--。位。
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4755609.html
Copyright © 2011-2022 走看看