• HDU 5624 KK's Reconstruction


    题目描述

    Our lovely KK has a difficult Social problem.
    A big earthquake happened in his area.
    N(2≤N≤2000)N(2≤N≤2000) cities have been implicated. All the roads between them are destroyed.
    Now KK was commissioned to rebuild these roads.
    However, after investigation,KK found that some roads are too damaged to rebuild.
    Therefore, there are only M(0≤M≤15000)M(0≤M≤15000) roads can be rebuilt.
    KK needs to make sure that there is a way between any two cities, and KK wants to rebuild roads as few as possible.
    With rebuilding minimal number of roads, he also wants to minimize the difference between the price of the most expensive road been built and the cheapest one.
    Input
    The first line of the input file contains an integer T(1≤T≤10)T(1≤T≤10), which indicates the number of test cases.

    For each test case,The first line includes two integers N(2≤N≤2000)N(2≤N≤2000),M(0≤M≤15000)M(0≤M≤15000).

    The next MM lines include three integers a,b,c(a≠b,1≤c≤2∗109)a,b,c(a≠b,1≤c≤2∗109),indicating there is a undirected edge between aa and bb,the cost is cc.
    Output
    For each test case, output the smallest difference between the price of the most expensive road and the cheapest one.If there is no legal solution, then output -1.
    Sample Input
    2
    5 10
    1 2 9384
    1 3 887
    1 4 2778
    1 5 6916
    2 3 7794
    2 4 8336
    2 5 5387
    3 4 493
    3 5 6650
    4 5 1422
    2 0
    Sample Output
    1686
    -1

    题目分析

    题目要求是修的路最少的情况下,找到最长边减最短边是最小的一种方案。

    路最少,就是顶点数减一的数量。还是类似于Kruskal的算法,把边从小到大排序好,只不过这次不是从最小的边开始找了,而是我们自己规定的第k条边开始找。穷举所有可能的差值,然后找到最小的差值。

    边界条件是还能找到这么一棵树,连结所有的顶点(就是树有V-1条边,V为顶点数),若树的边数小于V-1了,那么就不用在找下去了。

    代码

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define MAX_E 15005
    #define MAX_V 2005
    #define INF 0x7fffffff
    
    struct Edge {
        int u, v, len;
    }edges[MAX_E];
    int E, V; //边数,顶点数
    int fa[MAX_V];
    
    int found(int x) {
    	if(x != fa[x])
    	    fa[x] = found(fa[x]);
    	return fa[x];
    }
    
    int Kruskal(int k) {
        int maxcost = 0;
        for (int i = 0; i <= V; i++)
            fa[i] = i;
        int cnt = 0;
        for (int i = k; i < E; i++) {
            int r1 = found(edges[i].u);
            int r2 = found(edges[i].v);
            if (r1 != r2) {
                fa[r1]= r2;
                cnt++;
                maxcost = max(maxcost, edges[i].len);
            }
        }
        if (cnt == V - 1)
            return maxcost;
        else
            return 0;
    
    }
    
    bool cmp(Edge x, Edge y) {
        return x.len < y.len;
    }
    
    int main() {
        int T;
        scanf("%d", &T);
        while (T--) {
            scanf("%d%d", &V, &E);
            for (int i = 0; i < E; i++)
                scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].len);
    
            sort(edges, edges + E, cmp);
            int ans = INF;
            for (int i = 0; i < E; i++) {
                int maxcost = Kruskal(i);
                if (maxcost == 0) {
                    break;
                }
                ans = min(ans, maxcost - edges[i].len);
            }
            if (ans == INF)
                cout << -1 << endl;
            else
                cout << ans << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    2016-2017(2)数据结构课程小结
    顺序表2种结构体构造方法
    有理数四则运算的实验报告小结
    20165230 结对编程项目-四则运算 阶段总结
    20165230 《Java程序设计》实验二(Java面向对象程序设计)实验报告
    20165230 2017-2018-2 《Java程序设计》第7周学习总结
    20165230 2017-2018-2 《Java程序设计》第6周学习总结
    20165230 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
    20165230 2017-2018-2 《Java程序设计》第5周学习总结
    20165230 2017-2018-2 《Java程序设计》第4周学习总结
  • 原文地址:https://www.cnblogs.com/wudongwei/p/9370585.html
Copyright © 2020-2023  润新知