(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

省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。


Input

测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2 行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。

当N为0时输入结束。


Output

每个测试用例的输出占一行,输出全省畅通需要的最低成本。


Sample Input

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


Sample Output

3
1
0


Author

ZJU


Source

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

题目分析:

使用kruscal来求最小生成树。这道题的特点如下:

1)对已建边的处理方式:将其权值标记为0即可,如:edges[i].weight = 0;

需要注意的是,本题需要用C++提交。感觉杭电最近是不是有点问题啊。。。之前用G++提交一直好好的。

2)顺便重温了一下快排的写法:qsort(edges+1,cnt,sizeof(edges[1]),cmp1);

代码如下:

/*
 * e.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];

int father[maxn];

int n;

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 <= n ; ++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 cmp1(const void* a,const void* b){
	return ((Edge*)a)->weight > ((Edge*)b)->weight;//这样才是从小到大排序
}

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

		int cnt = 1;
		int i;
		for(i = 1 ; i <= m ; ++i){
			int flag;

			scanf("%d%d%d%d",&edges[cnt].begin , &edges[cnt].end , &edges[cnt].weight , &flag);

			if(flag == 1){
				edges[cnt].weight = 0;
			}

			cnt++;
		}

		cnt -= 1;

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

//		qsort(edges+1,cnt,sizeof(edges[1]),cmp1);

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

	return 0;
}
时间: 2024-10-03 09:08:30

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

(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 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只

(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.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 6.1.6)Jungle Roads(在索引为字符索引的情况下,求让n个点连通的最小费用)

题目: Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 126 Accepted Submission(s): 118   Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of

(heu step 6.1.1)Constructing Roads(最小生成树模板题:求让n个点连通的最小费用)

题目: Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 207 Accepted Submission(s): 135   Problem Description There are N villages, which are numbered from 1 to N, and you should bu

(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,