UVa 908 - Re-connecting Computer Sites

题目:有一些电脑,电脑间由一些线路连接。现在给你电脑间的连接状态,以及原来选取的网络线路。

在此基础上加入了K条新线路,要从里面选取新的网络,使得电脑都连通并且线路长度和最小。

分析:图论、最小生成树。

说明:每组数据间有空行。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>

using namespace std;

typedef struct d_node
{
	int point1;
	int point2;
	int weight;
}enode;
enode edge[1000010];

//union_set
int sets[1000010];
int rank[1000010];

void set_inital( int a, int b )
{
	for ( int i = a ; i <= b ; ++ i ) {
		rank[i] = 0;
		sets[i] = i;
	}
}

int  set_find( int a )
{
	if ( a != sets[a] )
		sets[a] = set_find( sets[a] );
	return sets[a];
}

void set_union( int a, int b )
{
	if ( rank[a] < rank[b] )
		sets[a] = b;
	else {
		if ( rank[a] == rank[b] )
			rank[a] ++;
		sets[b] = a;
	}
}
//end_union_set

int cmp_e( enode a, enode b )
{
	return a.weight < b.weight;
}

int kruskal( int n, int m )
{
	sort( edge, edge+m, cmp_e );

	set_inital( 0, n );
	int sum = 0;
	for ( int i = 0 ; i < m ; ++ i ) {
		int A = set_find( edge[i].point1 );
		int B = set_find( edge[i].point2 );
		if ( A != B ) {
			set_union( A, B );
			sum += edge[i].weight;
		}
	}
	return sum;
}

int main()
{
	int N,K,M,u,v,w,c = 0;
	while ( ~scanf("%d",&N) ) {
		int old_cost = 0;
		for ( int i = 1 ; i < N ; ++ i ) {
			scanf("%d%d%d",&u,&v,&w);
			old_cost += w;
		}

		int e_count = 0;
		scanf("%d",&K);
		for ( int i = 0 ; i < K ; ++ i ) {
			scanf("%d%d%d",&u,&v,&w);
			edge[e_count].point1 = u;
			edge[e_count].point2 = v;
			edge[e_count].weight = w;
			e_count ++;
		}
		scanf("%d",&M);
		for ( int i = 0 ; i < M ; ++ i ) {
			scanf("%d%d%d",&u,&v,&w);
			edge[e_count].point1 = u;
			edge[e_count].point2 = v;
			edge[e_count].weight = w;
			e_count ++;
		}

		if ( c ++ ) printf("\n");
		printf("%d\n%d\n",old_cost,kruskal( N, e_count ));
	}

	return 0;
}

UVa 908 - Re-connecting Computer Sites,布布扣,bubuko.com

时间: 2024-10-09 12:17:01

UVa 908 - Re-connecting Computer Sites的相关文章

uva 12096 - The SetStack Computer(STL)

UVA 12096 - The SetStack Computer 题目链接 题意:几个操作,push是在栈顶加入一个空集,dup是复制栈顶集合,在放入栈顶,union是把头两个取并集放回,int是头两个取交集放回,add是取头两个,把第一个当成一个集合加入第二个,每次操作输出栈顶集合的里面的个数 思路:用set,stack模拟,然后利用map去hash一个集合,模拟即可 代码: #include <cstdio> #include <cstring> #include <s

UVA 11766 Racing Car Computer --DP

题意:电脑记录了某一时刻每个赛车的前面和后面个有多少辆车(多个车并排时在别的车那只算一辆),问最少有多少个不合理的数据. 分析:看到n<=1000时,就尽量往DP上想吧. 每输入一组数据a,b,如果a+b>=n肯定不行,加上自己就超过n了.否则这个车肯定在(a+1,n-b)这段区间内,所以这段区间内的车子数(cnt[][]记录)++,如果车子数大于区间长度,就不再加了.搞完输入数据后,再来DP: 定义:dp[i] :前 i 辆车最多有多少车位置合理 则有方程: dp[i] = min(dp[j

UVa 12096 The SetStack Computer

Description Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational

UVA - 12096 The SetStack Computer(编码,STL)

12096 The SetStack Computer Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century.Initially controversial, set theory has come to play the role o

UVa 1647 (递推) Computer Transformation

题意: 有一个01串,每一步都会将所有的0变为10,将所有的1变为01,串最开始为1. 求第n步之后,00的个数 分析: 刚开始想的时候还是比较乱的,我还纠结了一下000中算是有1个00还是2个00 最终想明白后,并不会出现这样的子串. 总结了几个要点: 第n步之后,串的长度为2n,且0和1的个数相等,分别为2n-1 1经过两步变化为1001,所以每个1经过两步变化就会得到一个00,而且这个00分别被左右两边一个1包围着,不会与其他数字凑出额外的00 0经过两步变化为0110,所以00就会变成0

uva 12096 The SetStack Computer(STL set)

题意: 有5种操作: PUSH:加入"{}"空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出栈的加入到后出栈的集合中. 输入不超过2000, 保证操作顺利进行. 分析: 用set<int>(本身又可以映射成一个int)去模拟集合,所以可以将不同的集合映射成int型. 用一个Map<set<int>,int> 去映射成不同的int. 以后需要se

uva 12096 - The SetStack Computer(STL 的运用)

这道题目貌似就是在不停地用STL中的内容,对STL熟练运用的大神估计坐起来会比较easy.. 不过对于我这种看着代码还是需要上网查函数运用的菜鸟来说,若让我自己做这道题,肯定不会使用STL.. 就当对STL的学习了. #include<cstdio> #include<iostream> #include<cstring> #include<string> #include<set> #include<stack> #include&

UVa - 12096 The SetStack Computer(STL容器综合,强推!)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42064 #include <iostream> #include <algorithm> #include <string> #include <map> #include <set> #include <vector> #include <stack> #define ALL(x) x.b

1-Computer Networks and the Internet

Please indicate the source: http://blog.csdn.net/gaoxiangnumber1 Welcome to my github: https://github.com/gaoxiangnumber1 1.1 What Is the Internet? 1.1.1 A Nuts-and-Bolts Description When one end system has data to send to another end system, the sen