第五场-F-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 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 ≤ 500N − 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


(二)题目大意:

        给定一个n个顶点m条边的连通图,让你求这个图的最小生成树中不能被其它边取代的边。不能被取代的边是指:若该边不取的话,就无法得到最小生成树(具体可以参见题目样例)。输出这样的边的总数目及其权值和。


(三)解题思路:

  1. 先求一边原图的最小生成树,得到最小生成树的权值和val。
  2. 枚举得到的最小生成树的中每一条边,判断这条边被删除后(不取)是否仍然可以得到最小生成树(权值和=val)。若可以得到,说明该边可以被取代,反之则反之。
  3. 复杂度O(NM)。


(四)具体代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=5e4+10;
int p[maxn],vis[maxn],have[maxn];
int FIND(int x){return p[x]==x?x:p[x]=FIND(p[x]);}
int n,m,ans,res;
struct node{
    int f,t,val;
}edge[maxn];
bool cmp(node n1,node n2){
    return n1.val<n2.val;
}
int kruskal(int cancled){
    int mid=0;
    for(int i=1;i<=n;i++)p[i]=i;
    for(int i=0;i<m;i++){
        if(i==cancled)continue;
        int x=FIND(edge[i].f);
        int y=FIND(edge[i].t);
        if(x!=y){
            if(cancled==-1)vis[i]=1;
            p[x]=y,mid+=edge[i].val;
        }
    }
    return mid;
}
int main(){
    freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        ans=0;res=0;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&edge[i].f,&edge[i].t,&edge[i].val);
        }
        sort(edge,edge+m,cmp);
        memset(vis,0,sizeof vis);
        int best=kruskal(-1);
        for(int i=0;i<m;i++)
            if(vis[i]&&best!=kruskal(i))ans++,res+=edge[i].val;
        cout<<ans<<" "<<res<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/xbb224007/article/details/80222143
今日推荐