kruskal专题 poj2485 + poj 3723

poj 2485 Highways

Highways

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 28837 Accepted: 13161

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

Hint

Huge input,scanf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int tot = 0;
int fa[253333];
struct edge
{
	int from, to, cost;
}es[253333];
void build(int f, int t, int d)
{
	es[++tot] = (edge){f, t, d};
}
bool cmp(edge a, edge b)
{
	return a.cost < b.cost;
}
int find(int x)
{
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		tot = 0;
		memset(es, 0, sizeof(es));
		int n, la, sum = 0;
		cin>>n;
		for(int i = 1;i <= n;i ++)
		for(int j = 1;j <= n;j ++)
		{
			scanf("%d", &la);
			build(i, j, la);
		}
		for(int i = 0;i <= n;i ++)
			fa[i] = i;
		sort(es+1, es+tot+1, cmp);
		for(int i = 1;i <= tot;i ++)
		{
			int x = es[i].from, y = es[i].to;
			if(find(x) != find(y))
			{
				fa[find(x)] = find(y);
				sum = max(sum, es[i].cost);
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

 

POJ 3723 Conscription

Conscription

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11233 Accepted: 3960

Description

Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have a relationship d and one of them has been collected, Windy can collect the other one with 10000-d RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.

Input

The first line of input is the number of test case.
The first line of each test case contains three integers, N, M and R.
Then R lines followed, each contains three integers xi, yi and di.
There is a blank line before each test case.
1 ≤ N, M ≤ 10000
0 ≤ R ≤ 50,000
0 ≤ xi < N
0 ≤ yi < M
0 < di < 10000

Output

For each test case output the answer in a single line.

Sample Input

2
5 5 8
4 3 6831
1 3 4583
0 0 6592
0 1 3063
3 3 4975
1 3 2049
4 2 2104
2 2 781
5 5 10
2 4 9820
3 2 6236
3 1 8864
2 4 8326
2 0 5156
2 0 1463
4 1 2439
0 4 4373
3 4 8889
2 4 3133

Sample Output

71071
54223

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int fa[23333];
struct qer
{
	int from, to, cost;
}es[100005];
int find(int x)
{
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}
bool cmp(qer a, qer b)
{
	return a.cost > b.cost;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		memset(es, 0, sizeof(es));
		int n, m, r;
		scanf("%d%d%d", &n, &m, &r);
		for(int i = 1;i <= r;i ++)
		{
			scanf("%d%d%d", &es[i].from, &es[i].to, &es[i].cost);
			es[i].to += n;
		}
		sort(es+1, es+r+1, cmp);
		for(int i = 0;i <= m+n;i ++)
		{
			fa[i] = i;
		}
		int ans = 0;
		for(int i = 1;i <= r;i ++)
		{
			int x = es[i].from, y = es[i].to;
			if(find(x) != find(y))
			{
				fa[find(x)] = find(y);
				ans += es[i].cost;
			}
		}
		int anss = (n+m)*10000;
		printf("%d\n", anss-ans);
	}
	return 0;
}

 

上一篇
下一篇