(hdu step 6.1.4)还是畅通工程(求让n个点联通的最小费用)

题目:

还是畅通工程

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 165 Accepted Submission(s): 127
 

Problem Description

某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。


Input

测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。


Output

对每个测试用例,在1行里输出最小的公路总长度。


Sample Input

3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0


Sample Output

3
5

Hint

Hint

Huge input, scanf is recommended.

 

Source

浙大计算机研究生复试上机考试-2006年


Recommend

JGShining

题目分析:

使用kruscal来求最小生成树,简单题。

代码如下:

/*
 * d.cpp
 *
 *  Created on: 2015年3月10日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 101;

struct Edge{
	int begin;
	int end;
	int weight;
}edges[maxn*maxn];//注意这里的边数应该是maxn*maxn而不是maxn。因为n个点之间应该有n*(n-1)/2条边

int father[maxn];

int find(int a){
	if(a == father[a]){
		return a;
	}

	return father[a] = find(father[a]);
}

int kruscal(int count){
	int i;

	for(i = 1 ; i < maxn ; ++i){
		father[i] = i;
	}

	int sum = 0;

	for(i = 1 ; i <= count ; ++i){
		int fa = find(edges[i].begin);
		int fb = find(edges[i].end);

		if(fa != fb){
			father[fa] = fb;
			sum += edges[i].weight;
		}
	}

	return sum;
}

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

int main(){
	int n;
	while(scanf("%d",&n)!=EOF,n){
		int m = (n-1)*n/2;

		int cnt = 1;
		int i;
		for(i = 1 ; i <= m ; ++i){
			scanf("%d%d%d",&edges[cnt].begin,&edges[cnt].end,&edges[cnt].weight);
			cnt++;
		}

		cnt -= 1;

		sort(edges+1,edges+1+cnt,cmp);

		printf("%d\n",kruscal(cnt));
	}

	return 0;
}
时间: 2024-10-07 16:58:50

(hdu step 6.1.4)还是畅通工程(求让n个点联通的最小费用)的相关文章

(hdu step 6.1.5)继续畅通工程(求让n个点连通的最小费用)

题目: 继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 173 Accepted Submission(s): 132   Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任

(hdu step 6.1.2)Eddy&#39;s picture(在只给出二维坐标点的情况下,求让n个点连通的最小费用)

题目: Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 172 Accepted Submission(s): 126   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be

(hdu step 6.1.3)畅通工程(求需要添加多少条边才能让n个点连通)

题目: 畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 163 Accepted Submission(s): 132   Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道

(hdu step 2.2.8)N!Again(求N!的阶乘%2009以后的结果)

题目: N!Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 941 Accepted Submission(s): 526   Problem Description WhereIsHeroFrom:             Zty, what are you doing ?Zty:                        

(hdu step 4.2.4)A strange lift(求从起点到终点的最小步数,限制条件是:在一维的情况下)

题目: A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 709 Accepted Submission(s): 348   Problem Description There is a strange lift.The lift can stop can at every floor as you want, a

(hdu step 2.1.3)Largest prime factor(求一个数的最大质因子的位置)

题目: Largest prime factor Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4868 Accepted Submission(s): 1452   Problem Description Everybody knows any number can be combined by the prime number.Now,

(hdu step 5.2.6)确定比赛名次(求入度为0的点的个数)

题目: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 338 Accepted Submission(s): 181   Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得

(hdu step 6.1.7)Connect the Cities(在有的路已经修建好的情况下,求让n个点连通的最小费用)

题目: Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 391 Accepted Submission(s): 139   Problem Description In 2100, since the sea level rise, most of the cities disappear. Though

(hdu step 5.2.5)确定比赛名次(求拓扑序列)

题目: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 337 Accepted Submission(s): 180   Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得