木其网络科技专业程序员代写http://www.xmsydw.com
程序员学历擅长经验网店链接
apenny硕士ASP.NET PHP 电子 通信设计 图像 编程 网络5年进入店铺
zheng_qianqian本科C语言 C++面向对象 Java5年进入店铺
guoguanl本科Java Web项目 JSP Hibernate Struts Mysql5年进入店铺

poj 1258 Agri-Net 水题三连发。。。又是kruscal

题目:http://poj.org/problem?id=1258

完全模板题。。。和大水题poj 2485 一样。。。

目的:增强自信心。。。

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 500;

struct Edge{
	int x, y, w;
};
Edge e[maxn*maxn];
int f[maxn];
int v[maxn][maxn];

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

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

int main() {
	int t, n;
	int i, j, cnt, tmp, shortest, sel;
//	freopen("in", "r", stdin);
	while (scanf("%d", &n) != EOF) {
		cnt = 0;
		for (i = 0; i < n; i++) {
			f[i] = i;
			for (j = 0; j < n; j++)
				if (scanf("%d", &tmp) && i > j) {
					e[cnt].x = i;
					e[cnt].y = j;
					e[cnt].w = tmp;
					cnt++;
				}//if 
		}//for
		shortest = sel = 0;
		sort(e, e + cnt, cmp);
		for (i = 0; i < cnt; i++) {
			int a, b;
			a = find(e[i].x);
			b = find(e[i].y);
			if (a != b) {
				f[a] = b;
				shortest += e[i].w;
				if (++sel == n - 1) break;
			}//if
		}//for
		printf("%d\n", shortest);
	}//while
	return 0;
}




posted @ 2013-06-13 20:08  程序流程图  阅读(125)  评论(0编辑  收藏  举报
木其网络科技专业程序员代写http://www.xmsydw.com
程序员学历擅长经验网店链接
apenny硕士ASP.NET PHP 电子 通信设计 图像 编程 网络5年进入店铺
zheng_qianqian本科C语言 C++面向对象 Java5年进入店铺
guoguanl本科Java Web项目 JSP Hibernate Struts Mysql5年进入店铺