UVALive

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.
Figure F.2. No alternative bridges for 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 file contains several test cases, each of them has the following format.
N M S1 D1 C1 . . . SM DM CM
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
For each test case, 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

分析:

题意
给定一个N个点,M条边的简单连通无向图。
对于一个无向图来说,它的最小生成树可能不是唯一的。
问在它的所有的最小生成树中共有的边是哪几条,输出边数和权值之和。
3<=N<=500, N-1<=M<=min{50000, N(N-1)/2}
思路
首先跑一遍Kruskal,得到最小生成树的权值。
之后尝试删去图中的边,如果某一条边被删去后,最小生成树的值发生了变化(一定变大),那么说明这条边是在所有的最小生成树中都不可或缺的,那么就把这条边加入到答案中。
注意到第一次Kruskal得到的边已经包含了所有的答案,因此只要枚举这里的N-1条边即可。
边排序的复杂度被均摊了,并查集的复杂度可以忽略,因此总的复杂度是O(NM)

注意:重载运算符比cmp快一点

还有就是用一个数组存下第一次MST用到的边(开始没有存起来,直接标记,超时。。。。。。。。。)

code:

#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define max_v 50010
#define max_n 510
struct edge
{
    int x,y,w;
    bool operator<(const edge& b) const
    {
        return w<b.w;
    }
} e[max_v];
int possEdge[max_n];//保存第一次MST用到的边 很重要
int pa[max_n];
int cnt,minv;
int n,m;
int c,wsum;
void init()
{
    for(int i=1;i<=n;i++)
        pa[i]=i;
}
int find_set(int x)
{
    if(x!=pa[x])
        pa[x]=find_set(pa[x]);
    return pa[x];
}
int union_set(int x,int y)
{
    x=find_set(x);
    y=find_set(y);
    if(x==y)
        return 0;
    pa[x]=y;
    return 1;
}
void firstkrus()
{
    cnt=0;
    minv=0;
    init();
    for(int i=0; i<m; i++)
    {
        if(union_set(e[i].x,e[i].y))
        {
            minv+=e[i].w;
            possEdge[cnt++]=i;
            if(cnt==n-1)
                break;
        }
    }
}
bool krusWithout(int ce)
{
    init();
    int sum=0,ct=0;
    for(int i=0; i<m; i++)
    {
        if(i==ce)
            continue;
        if(union_set(e[i].x,e[i].y))
        {
            ct++;
            sum+=e[i].w;
            if(sum>minv)
                return false;
            if(ct==cnt)
                return true;
        }
    }
    return false;
}
void trycut()
{
    c=0;
    wsum=0;
    for(int i=0; i<cnt; i++)
    {
        if(!krusWithout(possEdge[i]))
        {
            c++;
            wsum+=e[possEdge[i]].w;
        }
    }
    printf("%d %d
",c,wsum);
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=0; i<m; i++)
            scanf("%d %d %d",&e[i].x,&e[i].y,&e[i].w);
        sort(e,e+m);
        firstkrus();
        trycut();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yinbiao/p/9432843.html