zoukankan      html  css  js  c++  java
  • There is No Alternative CSU

    Description

    ICPC (Isles of Coral Park City) consist of several beautiful islands.

    The citizens requested construction of bridges between islands to resolve inconveniences of using boats between islands, and they demand that all the islands should be reachable from any other islands via one or more bridges.

    The city mayor selected a number of pairs of islands, and ordered a building company to estimate the costs to build bridges between the pairs. With this estimate, the mayor has to decide the set of bridges to build, minimizing the total construction cost.

    However, it is difficult for him to select the most cost-efficient set of bridges among those connecting all the islands. For example, three sets of bridges connect all the islands for the Sample Input 1. The bridges in each set are expressed by bold edges in Figure F.1.

    Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1

    Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1

    As the first step, he decided to build only those bridges which are contained in all the sets of bridges to connect all the islands and minimize the cost. We refer to such bridges as no alternative bridges. In Figure F.2, no alternative bridges are drawn as thick edges for the Sample Input 1, 2 and 3.

    Write a program that advises the mayor which bridges are no alternative bridges for the given input.

    Input

    The input consists of several tests case.

    Figure F.2. No alternative bridges for Sample Input 1, 2 and 3

    Figure F.2. No alternative bridges for Sample Input 1, 2 and 3

    N MS1 D1 C1SM DM CMN MS1 D1 C1⋮SM DM CM

    For each test, the first line contains two positive integers N and M . N represents the number of islands and each island is identified by an integer 1 through NM represents the number of the pairs of islands between which a bridge may be built.

    Each line of the next M lines contains three integers SiDi and Ci (1 ≤ i ≤ M) which represent that it will cost Ci to build the bridge between islands Si and Di. You may assume 3 ≤ N ≤ 500, N − 1 ≤ M ≤ min(50000, N(N − 1)/2), 1 ≤ Si < Di ≤ N, and 1 ≤ Ci ≤ 10000. No two bridges connect the same pair of two islands, that is, if i ≠ j and Si = Sj , then Di ≠ Dj. If all the candidate bridges are built, all the islands are reachable from any other islands via one or more bridges.

    Output

    Output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.

    Sample Input

    4 4
    1 2 3
    1 3 3
    2 3 3
    2 4 3
    
    4 4
    1 2 3
    1 3 5
    2 3 3
    2 4 3
    
    4 4
    1 2 3
    1 3 1
    2 3 3
    2 4 3
    
    3 3
    1 2 1
    2 3 1
    1 3 1

    Sample Output

    1 3
    3 9
    2 4
    0 0

    题意是建桥,然后求最小建桥方案中哪些桥是必须要留着的,求这些桥的个数和总花费
    先求出最小生成树,然后再去掉一条条边,看哪些边去掉后结果和最小生成树的结果不一样,那么这些边就是要留着的

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<cmath>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    typedef long long ll;
    const ll mod = 1e9 + 7;
    const ll maxn = 1e5 + 10;
    int n, m, num, cnt, result, pre[maxn], b[maxn], vis[maxn];
    struct node {
        int x, y, z;
    };
    node edge[maxn];
    bool cmp( node p, node q ) {
        return p.z < q.z;
    }
    void init() {
        for( int i = 1; i <= n; i ++ ) {
            pre[i] = i;
        }
    }
    int find( int x ) {
        int r = x;
        while( r != pre[r] ) {
            r = pre[r];
        }
        int i = x, j;
        while( pre[i] != r ) {
            j = pre[i];
            pre[i] = r;
            i = j;
        }
        return r;
    }
    void join( int x, int y ) {
        int fx = find(x), fy = find(y);
        if( fx != fy ) {
            pre[fx] = fy;
        }
    }
    int kruskal( int flag ) {
        int sum = 0;
        for( int i = 0; i < m; i ++ ) {
            if( vis[i] ) {
                continue;
            }
            int fx = find( edge[i].x );
            int fy = find( edge[i].y );
            if( fx != fy ) {
                sum += edge[i].z;
                pre[fx] = fy;
                if( !flag ) {
                    b[cnt++] = i;
                }
            }
        }
        return sum;
    }
    int main() {
        std::ios::sync_with_stdio(false);
        while( cin >> n >> m ) {
            memset( vis, 0, sizeof(vis) );
            for( int i = 0; i < m; i ++ ) {
                cin >> edge[i].x >> edge[i].y >> edge[i].z;
            }
            sort( edge, edge + m, cmp );
            cnt = 0, num = 0, result = 0;
            init();
            int ans = kruskal(0);
            for( int i = 0; i < cnt; i ++ ) {
                init();
                vis[b[i]] = 1;
                if( kruskal(1) != ans ) {
                    result += edge[b[i]].z;
                    num ++;
                }
                vis[b[i]] = 0;
            }
            cout << num << " " << result << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    Your branch and 'origin/master' have diverged, and have # and # different commits each, respectively
    testng dataprovider 的几种用法以及Java中的二维数组
    python 类属性 实例属性 类方法 实例方法 静态方法(转载)
    Webdriver中PageFactory的正确用法
    Selenium webdriver在最开始打开的时候浏览器的地址栏会出现data的解决方法
    Selenium webdriver如何处理confirm对话框的问题
    SoapUI 引用第三方jar包和引用Groovy脚本
    git rebase -i 合并commit
    Git 撤销commit的注意事项
    单进程执行
  • 原文地址:https://www.cnblogs.com/l609929321/p/9021474.html
Copyright © 2011-2022 走看看