uva10600 - ACM Contest and Blackout 次小生成树

Problem A

ACM CONTEST AND BLACKOUT

 

In order to prepare the “The First National ACM School Contest”(in 20??) the major of the city decided to provide all the schools with a reliable source of power. (The major is really afraid of blackoutsJ). So, in order to do that, power station “Future” and one school (doesn’t matter which one) must be connected; in addition, some schools must be connected as well.

 

You may assume that a school has a reliable source of power if it’s connected directly to “Future”, or to any other school that has a reliable source of power. You are given the cost of connection between some schools. The major has decided to pick out two the cheapest connection plans – the cost of the connection is equal to the sum of the connections between the schools. Your task is to help the major – find the cost of the two cheapest connection plans.

 

Input

The Input starts with the number of test cases, T (1£T£15) on a line. Then T test cases follow. The first line of every test case contains two numbers, which are separated by a space, N (3£N£100) the number of schools in the city, and M the number of possible connections among them. Next M lines contain three numbers Ai, Bi, Ci , where Ci  is the cost of the connection (1£Ci£300) between schools Ai  and Bi. The schools are numbered with integers in the range 1 to N.

 

Output

For every test case print only one line of output. This line should contain two numbers separated by a single space - the cost of two the cheapest connection plans. Let S1 be the cheapest cost and S2 the next cheapest cost. It’s important, that S1=S2 if and only if there are two cheapest plans, otherwise S1£S2. You can assume that it is always possible to find the costs S1 and S2..

 

Sample Input

Sample Output

2

5 8

1 3 75

3 4 51

2 4 19

3 2 95

2 5 42

5 4 31

1 2 9

3 5 66

9 14

1 2 4

1 8 8

2 8 11

3 2 8

8 9 7

8 7 1

7 9 6

9 3 2

3 4 7

3 6 4

7 6 2

4 6 14

4 5 9

5 6 10

110 121

37 37


  求最小生成树和次小生成树。

  次小生成树:先求最小生成树,记录最小生成树上的边,然后用DFS(BFS也行)求出任意两点u,v在最小生成树上唯一路径上的最大边maxe[u][v],那么只要枚举增加一条不在最小生成树上的边u,v,并且删除u-v在最小生成树路径上的最大边maxe[u][v](增加边后形成环,删掉一条边后任意两点仍然相互可达),在枚举的里面找到一个最小的生成树就是次小生成树。很明显次小生成树是最小生成树删掉一条边加上一条新边,不可能删掉两条,没有删掉一条划得来。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<vector>
using namespace std;

typedef pair<int,int> pii;

const int MAXN=110;
const int MAXM=5010;
const int MAXNODE=4*MAXN;
const int LOGMAXN=50;
const int INF=0x3f3f3f3f;

int T,N,M;
int mst[MAXN][MAXN],maxe[MAXN][MAXN],pa[MAXN];
vector<int> G[MAXN],C[MAXN],node;

int find(int x){
    return x==pa[x]?x:pa[x]=find(pa[x]);
}

struct Edge{
    int u,v,d;
    bool operator < (const Edge& rhs) const{
        return d<rhs.d;
    }
}e[MAXM];

int MST(){
    sort(e,e+M);
    memset(mst,0,sizeof(mst));
    for(int i=0;i<=N;i++){
        pa[i]=i;
        G[i].clear();
        C[i].clear();
    }
    int cnt=0,ret=0;
    for(int i=0;i<M;i++){
        int x=find(e[i].u),y=find(e[i].v);
        if(x!=y){
            pa[x]=y;
            G[e[i].u].push_back(e[i].v);
            C[e[i].u].push_back(e[i].d);
            G[e[i].v].push_back(e[i].u);
            C[e[i].v].push_back(e[i].d);
            mst[e[i].u][e[i].v]=mst[e[i].v][e[i].u]=1;
            cnt++;
            ret+=e[i].d;
        }
        if(cnt==N-1) return ret;
    }
    return -1;
}

void DFS(int u,int fa,int d){
    int len=node.size();
    for(int i=0;i<len;i++){
        int v=node[i];
        maxe[u][v]=maxe[v][u]=max(maxe[v][fa],d);
    }
    len=G[u].size();
    node.push_back(u);
    for(int i=0;i<len;i++){
        int v=G[u][i];
        if(v!=fa) DFS(v,u,C[u][i]);
    }
}

int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        int u,v,d;
        for(int i=0;i<M;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].d);
        int ans1=MST();
        node.clear();
        memset(maxe,-1,sizeof(maxe));
        DFS(1,-1,0);
        int ans2=INF;
        for(int i=0;i<M;i++) if(!mst[e[i].u][e[i].v]){
            ans2=min(ans2,ans1+e[i].d-maxe[e[i].u][e[i].v]);
        }
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}


你可能感兴趣的:(uva10600 - ACM Contest and Blackout 次小生成树)