hdoj_1102Constructing Roads(最小生成树)&& poj_2485Highways

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10143    Accepted Submission(s): 3778


Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. 

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
 

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
 

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum. 
 

Sample Input

   
3 0 990 692 990 0 179 692 179 0 1 1 2
 

Sample Output

   
179
多组测试数据=。=

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
#pragma warning(disable : 4996)
#define MAX 10000
typedef struct edge
{
	int x, y;
	int w;
}edge;

edge e[MAX];
int father[MAX], ranks[MAX];

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

void Make_Set(int n)
{
	for(int i = 1; i <= n; i++)
	{
		father[i] = i;
		ranks[i] = 0;
	}
}

int Find_Set(int x)
{
	if(x != father[x])
		father[x] = Find_Set(father[x]);
	return father[x];
}

void Merge_Set(int x, int y)
{
	x = Find_Set(x);
	y = Find_Set(y);
	if(x == y) return;
	if(ranks[x] > ranks[y])
	{
		father[y] = x;
	}
	else if(ranks[x] < ranks[y])
	{
		father[x] = y;
	}
	else 
	{
		ranks[y]++;
		father[x] = y;
	}
}

int main()
{
	freopen("in.txt", "r", stdin);
	int n, x, y, q, count = 0;
	while (scanf("%d", &n) != EOF)
	{
		Make_Set(n);
		count = 0;
		for (int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				scanf("%d", &x);
				if(i != j)
				{
					e[count].x = i;
					e[count].y = j;
					e[count++].w = x;
				}
			}
		}
		sort(e, e + count, cmp);
		scanf("%d", &q);
		while (q--)
		{
			scanf("%d %d", &x, &y);
			Merge_Set(x, y);
		}
		int sum = 0;
		for (int i = 0; i < count; i++)
		{
			x = Find_Set(e[i].x);
			y = Find_Set(e[i].y);
			if(x != y)
			{
				sum += e[i].w;
				Merge_Set(e[i].x, e[i].y);
			}
		}
		printf("%d\n", sum);
	}
	return 0;
}

Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18016 Accepted: 8372

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

Input

The first line of input is an integer T, which tells how many test cases followed.
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
#pragma warning(disable : 4996)
#define MAX 100000
typedef struct edge
{
	int x, y;
	int w;
}edge;

edge e[MAX];
int father[MAX], ranks[MAX];

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

void Make_Set(int n)
{
	for(int i = 1; i <= n; i++)
	{
		father[i] = i;
		ranks[i] = 0;
	}
}

int Find_Set(int x)
{
	if(x != father[x])
		father[x] = Find_Set(father[x]);
	return father[x];
}

void Merge_Set(int x, int y)
{
	x = Find_Set(x);
	y = Find_Set(y);
	if(x == y) return;
	if(ranks[x] > ranks[y])
	{
		father[y] = x;
	}
	else if(ranks[x] < ranks[y])
	{
		father[x] = y;
	}
	else 
	{
		ranks[y]++;
		father[x] = y;
	}
}

int main()
{
	freopen("in.txt", "r", stdin);
	int t, n, x, y, count = 0;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d", &n);
		Make_Set(n);
		count = 0;
		for (int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				scanf("%d", &x);
				if(i != j)
				{
					e[count].x = i;
					e[count].y = j;
					e[count++].w = x;
				}
			}
		}
		sort(e, e + count, cmp);
		int ans;
		for (int i = 0; i < count; i++)
		{
			x = Find_Set(e[i].x);
			y = Find_Set(e[i].y);
			if(x != y)
			{
				ans = e[i].w;
				Merge_Set(e[i].x, e[i].y);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}



转载于:https://my.oschina.net/N3verL4nd/blog/866885

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值