POJ - 2421 Constructing Roads【prim】

Constructing Roads

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 28912 Accepted: 12823
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

链接:http://poj.org/problem?id=2421

简述:有n个村庄,要使所有村庄连通,就有n-1条路。假如A跟B连通,B跟C连通,则A、B、C都连通。

分析:转化为最小生成树的问题,从第一个村庄开始选择其他到此村庄距离最短的一个(已经连通的村庄间需要成本为0),每一次都选择最短的那个。

说明:一开始数组开太小,以为100就可以了emmmm。prim()函数里面两层循环来实现,外循环循环到n-1。

AC代码如下:

#include 
#include 
#include 
using namespace std;
#define MAX 9999999;	//不用加=号
int n;
int map[110][110];
int d[110], vis[110];
void prim(int p) {
	for (int i = 1; i <= n; i++)
		d[i] = i == p ? 0 : map[p][i];
	vis[p] = 1;
	int sum = 0;
	for (int i = 1; i < n; i++) {	//第一层循环不用更新最后一个值
		int min = MAX;
		int k;
		for (int j = 1; j <= n; j++)
			if (!vis[j] && min > d[j]) {
				k = j;
				min = d[j];
			}
		vis[k] = 1;
		sum += min;
		for (int j = 1; j <= n; j++) {
			if (!vis[j] && d[j] > map[k][j])
				d[j] = map[k][j];
		}
	}
	cout << sum << endl;

}

int main()
{
	int q, a, b;
	while (cin >> n) {
		memset(vis, 0, sizeof(vis));
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> map[i][j];
		cin >> q;
		while (q--) {
			cin >> a >> b;
			map[a][b] = 0;
			map[b][a] = 0;
		}
		prim(1);
	}
}
#include 
#include 
#include 
using namespace std;
#define INF 1e6;
int map[110][110], n;
int dis[110];
int vis[110];
int emmmmm(int q) 
{
	int i, j, k, min, sum = 0;
	for (i = 1; i <= n; i++) dis[i] = map[q][i];
	vis[q] = 1;
	for (i = 1; i < n; i++) {
		min = INF;
		for (j = 1; j <= n; j++) {
			if (!vis[j] && dis[j] < min) {
				k = j;
				min = dis[j];
			}
		}
		vis[k] = 1;
		sum += min;
		for (j = 1; j <= n; j++) {
			if (!vis[j] && dis[j] > map[k][j])
				dis[j] = map[k][j];
		}
	}
	return sum;
}
int main()
{
	int i, j, Q, a, b;
	memset(map, 0, sizeof(map));
	memset(vis, 0, sizeof(vis));
	cin >> n;
	for (i = 1; i <= n; i++)
		for (j = 1; j <= n; j++)
			cin >> map[i][j];
	cin >> Q;
	while (Q--) {
		cin >> a >> b;
		map[a][b] = map[b][a] = 0;
	}
	cout << emmmmm(1) << endl;
}

你可能感兴趣的:(POJ - 2421 Constructing Roads【prim】)