Is There A Second Way Left? UVA - 10462 带重边的次小生成树(kruskal算法)

  • Is There A Second Way Left? 

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1403

Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently all his neighbors have decided to connect themselves over a network (actually all of them want to share a broadband internet connection :-)). But he wants to minimize the total cost of cable required as he is a bit fastidious about the expenditure of the project. For some unknown reasons, he also wants a second way left. I mean, he wants to know the second best cost (if there is any which may be same as the best cost) for the project. I am sure, he is capable of solving the problem. But he is very busy with his private affairs(?) and he will remain so. So, it is your turn to prove yourself a good programmer. Take the challenge (if you are brave enough)...

Input

Input starts with an integer t ≤ 1000 which denotes the number of test cases to handle. Then follows t datasets where every dataset starts with a pair of integers v (1 ≤ v ≤ 100) and e (0 ≤ e ≤ 200). v denotes the number of neighbors and e denotes the number of allowed direct connections among them. The following e lines contain the description of the allowed direct connections where each line is of the form ‘start end cost’, where start and end are the two ends of the connection and cost is the cost for the connection. All connections are bi-directional and there may be multiple connections between two ends.

Output

There may be three cases in the output

1. No way to complete the task,

2. There is only one way to complete the task,

3. There are more than one way. Output ‘No way’ for the first case, ‘No second way’ for the second case and an integer c for the third case where c is the second best cost. Output for a case should start in a new line.

Sample Input

4

5 4

1 2 5

3 2 5

4 2 5

5 4 5

5 3

1 2 5

3 2 5

5 4 5

5 5

1 2 5

3 2 5

4 2 5

5 4 5

4 5 6

1 0

Sample Output

Case #1 : No second way

Case #2 : No way

Case #3 : 21

Case #4 : No second way

题意:判断一个图能否求出次小生成树,若能输出次小生成树的值,若无法求出最小生成树则输出“No way”,若没有次小生成树则输出“No second way”

思路:次小生成树模板题。先求出最小生成树,之后依次去掉最小生成树的边,再在剩下的边中求最小生成树,之后取的答案中的最小值,即为次小生成树的权值。

(这种利用反复求最小生成树的方法求次小生成树,时间复杂度较高,在题目数据量不大的情况下可以使用。但是当遇到稠密图的时候,由于边数过多,这种方法会超时,需要用一个二维数组maxd来记录i点到j点路径上的最大边来进行优化)

AC代码:

20ms

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
const int INF = 0x7fffffff;
const int MAXN = 1e6 + 7;
typedef long long ll;
using namespace std;
int n, m;
struct edge {
	int u, v;
	int cost,used,del;
}e[MAXN];
int pre[MAXN];
int ans;
bool cmp(edge a, edge b);
int find(int x);
void Union(int a,int b);
void kruskal();
int main() {
	int t;
	int k = 1;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= m; ++i) {
			scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].cost);
			e[i].used = 0;
			e[i].del = 0;
		}
		printf("Case #%d : ",k);
		kruskal();
		k++;
	}
	return 0;
}

bool cmp(edge a, edge b) {
	return a.cost < b.cost;
}

int find(int x) {
	if (pre[x] == x)
		return x;
	return pre[x] = find(pre[x]);
}

void Union(int a,int b){
	int x = find(a);
	int y = find(b);
	if (x == y)
		return;
	else
		pre[x] = y;
}

void kruskal() {
	for (int i = 1; i <= n; ++i) {
		pre[i] = i;
	}
	ans = 0;
	int z = 0;
	sort(e + 1, e + m + 1, cmp);
	for (int i = 1; i <= m; ++i) {
		if (z == n - 1) {
			break;
		}
		if (find(e[i].u) != find(e[i].v)) {
			z++;
			Union(e[i].u, e[i].v);
			ans += e[i].cost;
			e[i].used = 1;
		}
	}
	if (z != n - 1) {
		printf("No way\n");
		return;
	}
	int minn = INF;
	for (int i = 1; i <= m; ++i) {
		int cur = 0;
		if (e[i].used) {
			e[i].del = 1;
			for (int j = 1; j <= n; ++j) {
				pre[j] = j;
			}
			z = 0;
			for (int j = 1; j <= m; ++j) {
				if (z == n - 1)
					break;
				if (!e[j].del&&find(e[j].u) != find(e[j].v)) {
					Union(e[j].u, e[j].v);
					z++;
					cur += e[j].cost;
				}
			}
			if(z==n-1)
				minn = min(cur, minn);
			e[i].del = 0;
		}
	}
	if (minn != INF)
		printf("%d\n", minn);
	else
		printf("No second way\n");
}

猜你喜欢

转载自blog.csdn.net/weixin_43821265/article/details/87455120
way