csu2097—There is No Alternative (最小生成树)

题目链接:传送门

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 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 N. M 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 Si, Di 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

Hint

Source

Asia Regional Contest, Tokyo, 2014


解题思路:

先求出一颗MST,然后枚举每一条不在树上的边,这样会构成一个环,然后看环上哪些边权与枚举的边权相等(类似于次小生成树),时间复杂度度是0(nm)。


代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

typedef long long llt;

const int N = 510;
const int M = 50010;
const int INF = 0x3fffffff;

struct Edge{
    int node,len;
    Edge*next;
}edge[N*2];
int Ecnt;
Edge*head[N];

int vis[N],lowc[N],pre[N],Max[N][N],used[N][N],graph[N][N];

int n,m;

void init()
{
    Ecnt = 0;
    fill(head,head+N,(Edge*)0);

    for(int i = 0; i < N; ++i){
        for(int j = 0; j < N; ++j){
            graph[i][j] = INF;
            if(i == j)
                graph[i][j] = 0;
        }
    }
}

void mkEdge(int a,int b,int c)
{
    edge[Ecnt].node = b;
    edge[Ecnt].len = c;
    edge[Ecnt].next = head[a];
    head[a] = edge+Ecnt++;
}

int Prime(int cost[][N])
{
//    int ans = 0;
    memset(vis,0,sizeof(vis));
    memset(Max,0,sizeof(Max));
    memset(used,0,sizeof(used));

    vis[0] = 1;
    pre[0] = -1;

    for(int i = 1; i < n; ++i){
        lowc[i] = cost[0][i];
        pre[i] = 0;
    }
    lowc[0] = 0;
    for(int i = 1; i < n; ++i){
        int minc = INF;
        int p = -1;
        for(int j = 0; j < n; ++j){
            if(!vis[j] && minc > lowc[j]){
                minc = lowc[j];
                p = j;
            }
        }

        if(minc == INF) return -1;

        vis[p] = 1;
        used[p][pre[p]] = used[pre[p]][p] = 1;
        mkEdge(p,pre[p],graph[p][pre[p]]);
        mkEdge(pre[p],p,graph[p][pre[p]]);

        for(int j = 0; j < n; ++j){
            if(vis[j]){
                Max[j][p] = Max[p][j] = max(Max[j][pre[p]],lowc[p]);
            }
            if(!vis[j] && lowc[j] > cost[p][j]){
                lowc[j] = cost[p][j];
                pre[j] = p;
            }
        }
    }

//    return ans;
}

int dep[N],father[N],tick[N][N];  //tick标记树上的边能否被替换,father表示父亲结点

//无根树转有根树
void Build(int u)
{
    for(Edge*p = head[u]; p; p = p->next){
        int v = p->node;
        if(!dep[v]){
            dep[v] = dep[u]+1;
            father[v] = u;
            Build(v);
        }
    }
}

void solve(int u,int v,int val)
{
    while(u != v){
        if(dep[u] < dep[v]){
            if(graph[v][father[v]] == val)
                tick[v][father[v]] = tick[father[v]][v] = 1;
            v = father[v];
        }else{
            if(graph[u][father[u]] == val)
                tick[u][father[u]] = tick[father[u]][u] = 1;
            u = father[u];
        }

//        cout << u << " " << v << endl;
    }
}

int main()
{
//    freopen("D:\\06.in","r",stdin);
//    freopen("D:\\data.txt","w",stdout);
    while(~scanf("%d%d",&n,&m)){
        init();
        int a,b,c;
        for(int i = 0; i < m; ++i){
            scanf("%d%d%d",&a,&b,&c);
            a--,b--;
            graph[a][b] = graph[b][a] = c;
        }

        Prime(graph);
//        printf("%d %d\n",cnt,ans);
        memset(dep,0,sizeof(dep));
        memset(father,0,sizeof(father));
        memset(tick,0,sizeof(tick));
        dep[0] = 1;
        father[0] = 0;
        Build(0);

        for(int i = 0; i < n; ++i){
            for(int j = i+1; j < n; ++j){
                if(!used[i][j] && graph[i][j] != INF)
                    solve(i,j,graph[i][j]);
            }
        }

//        cout << "aaa" << endl;

        int ans = 0,cnt = 0;
        for(int i = 0; i < n; ++i){
            for(int j = i+1; j < n; ++j){
                if(used[i][j] && !tick[i][j])
                    cnt++,ans += graph[i][j];
            }
        }

        printf("%d %d\n",cnt,ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jiangzhiyuan123/article/details/80223516