There is No Alternative CSU - 2097 最小生成树

Posted 九月旧约

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了There is No Alternative CSU - 2097 最小生成树相关的知识,希望对你有一定的参考价值。

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

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

 

N MS1 D1 C1?SM 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;
}

 







以上是关于There is No Alternative CSU - 2097 最小生成树的主要内容,如果未能解决你的问题,请参考以下文章

CSU 1541 There is No Alternative (最小生成树+枚举)

sharepoint 2007 log error : There is no Web named "/test/test".

There is no known eureka server; cluster server list is empty

Struts2注解错误之There is no Action mapped for namespace /

[ERROR] The goal you specified requires a project to execute but there is no POM in this directory

mybatis There is no getter for property named 'xxxx