CSU-2097: There is No Alternative

2097: There is No Alternative

Submit Page      Summary      Time Limit: 3 Sec       Memory Limit: 512 Mb       Submitted: 78       Solved: 25    

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

Hint

Source

Asia Regional Contest, Tokyo, 2014

题意:给定一个无向图,图中至少有一个最小生成树,求所有最小生成树的交集(边)并输出有多少条交集边,以及边的权值之和。

题解:先求任意一个最小生成树,则最终的交集边一定属于这颗树边的子集。

现在考虑,什么情况下有多个最小生成树,当我们删除一条边时,会形成两个联通块。如果此时有与它等权值的未加入最小生成树的边,可以使得这两个联通块合并(不可能有更小权值的,更小权值则原树肯定不是最小生成树)则说明这条边是可以被替换的。如果遍历所有等权值边依然不存在解,则这条边是交集!因为题目的n较小,故我们对于每一个边都可以重新生成不包括它在内的生成树。最小生成树有n - 1条边,每次最坏遍历m - n + 2次(权值全相等的情况)故最终复杂度为O(n * m)

AC代码

#include <iostream>
#include <vector>
#include <stack>
#include <climits>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>

using namespace std;

const int maxn = 55555;

int vis[maxn], fa[maxn];

struct node{
	int a, b, c;
}p[maxn];

void init(){
	for(int i = 0; i < maxn; i++)
		fa[i] = i;
}

int find_fa(int x){
	if(x == fa[x])
		return x;
	return fa[x] = find_fa(fa[x]);
}

bool cmp(node a1, node a2){
	return a1.c < a2.c;
}

int main(){
	int n, m;
	while(scanf("%d %d", &n, &m) != EOF){
		init();
		for(int i = 0; i < m; i++)
			scanf("%d %d %d", &p[i].a, &p[i].b, &p[i].c);
		sort(p, p + m, cmp);
		vector<int> s1, s2;
		for(int i = 0; i < m; i++){
			int a = p[i].a;
			int b = p[i].b;
			int x = find_fa(a);
			int y = find_fa(b);
			if(x != y){
				s1.push_back(i);
				fa[x] = y;
			}
			else
				s2.push_back(i);
		}
		int len1 = s1.size();
		int len2 = s2.size();
		int num, sum, now;
		num = sum = now = 0;
		for(int i = 0; i < len1; i++){
			int j = 0;
			for(; j < len2 && p[s2[j]].c < p[s1[i]].c; j++);
			if(j == len2 || p[s1[i]].c != p[s2[j]].c){
				now = j;
				num++;
				sum += p[s1[i]].c;
			}
			if(p[s1[i]].c == p[s2[j]].c){
				now = max(j - 1, 0);
				init();
				for(int ii = 0; ii < len1; ii++){
					if(ii == i)
						continue;
					int a = p[s1[ii]].a;
					int b = p[s1[ii]].b;
					int x = find_fa(a);
					int y = find_fa(b);
					if(x != y)
						fa[x] = y;
				}
				int k = j;
				for(; k < len2 && p[s2[k]].c == p[s1[i]].c; k++);
				int kk = j;
				for(; kk < k; kk++){
					int a = p[s2[kk]].a;
					int b = p[s2[kk]].b;
					int x = find_fa(a);
					int y = find_fa(b);
					if(x != y)
						break;
				}
				if(kk == k){
					num++;
					sum += p[s1[i]].c;
				}
			}
		}
		printf("%d %d\n", num, sum); 
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37064135/article/details/80230892