(beginer) 最小生成树 UVA 11747 Heavy Cycle Edges

本文深入探讨了深度学习在人工智能领域的应用,包括卷积神经网络、循环神经网络、自动推理系统等关键技术,并展示了它们在图像处理、语音识别、自然语言处理等领域的实际应用案例。

Problem F: Heavy Cycle Edges

(beginer) 最小生成树 UVA 11747 Heavy Cycle Edges - 恶魔仁 - 恶魔仁

Given an undirected graph with edge weights, a minimum spanning tree is a subset of edges of minimum total weight such that any two nodes are connected by some path containing only these edges. A popular algorithm for finding the minimum spanning tree T in a graph proceeds as follows:

  • let T be initially empty
  • consider the edges e1, ..., em in increasing order of weight
    • add ei to T if the endpoints of ei are not connected by a path in T

An alternative algorithm is the following:

  • let T be initially the set of all edges
  • while there is some cycle C in T
    • remove edge e from T where e has the heaviest weight in C

Your task is to implement a function related to this algorithm. Given an undirected graph G with edge weights, your task is to output all edges that are the heaviest edge in some cycle of G.

Input Format

The first input of each case begins with integers n and m with 1 ≤ n ≤ 1,000 and 0 ≤ m ≤ 25,000 where n is the number of nodes and m is the number of edges in the graph. Following this are m lines containing three integers u, v, and w describing a weight w edge connecting nodes u and v where 0 ≤ u, v < n and 0 ≤ w < 231. Input is terminated with a line containing n = m = 0; this case should not be processed. You may assume no two edges have the same weight and no two nodes are directly connected by more than one edge.

Output Format

Output for an input case consists of a single line containing the weights of all edges that are the heaviest edge in some cycle of the input graph. These weights should appear in increasing order and consecutive weights should be separated by a space. If there are no cycles in the graph then output the text forest instead of numbers.

Sample Input

3 3
0 1 1
1 2 2
2 0 3
4 5
0 1 1
1 2 2
2 3 3
3 1 4
0 2 0
3 1
0 1 1
0 0

Sample Output

3
2 4
forest

题意:把找最小生成树的步骤改成是删除环中最大的边。输出边的长度。

思路:其实和一开始是空的然后加边是一样的,因为我们在加边的时候就要判断会不会形成环,那么我们照样用kruskal算法,然后如果加边会形成环就把他输出就行了。

代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1010;
const int maxm = 25010;
int n , m;
int p[maxn];

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

struct Edge
{
	Edge() { }
	Edge(int uu,int vv,int ww)
		:u(uu) , v(vv) , w(ww) { }
	int u , v , w;
}edge[maxm];

inline bool operator<(const Edge&e1,const Edge&e2)
{
	return e1.w < e2.w;
}

void input()
{
	for (int i = 0 ; i < m ; ++i)
	{
		int u , v , w;
		scanf("%d%d%d",&u,&v,&w);
		edge[i] = Edge(u,v,w);
	}
	sort(edge,edge+m);
}

void solve()
{
	bool flag = true;
	for (int i = 0 ; i < n ; ++i) p[i] = i;
	for (int i = 0 ; i < m ;  ++i)
	{
		int u = find(edge[i].u);
		int v = find(edge[i].v);
		if (u==v) {
			if (flag) printf("%d",edge[i].w);
			else printf(" %d",edge[i].w);
			flag = false;
			continue;
		}
		p[u] = v;
	}
	if (flag) printf("forest");
	cout << endl;
}

int main()
{
	while (scanf("%d%d",&n,&m)==2,n+m)
	{
		input();
		solve();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值