hdu 1879 继续畅通工程
/************************************************************************/
/* hdu 1879
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6017 Accepted Submission(s): 2493 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年
*/
/************************************************************************/
//最小生成树
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = + ;
int u[maxn], v[maxn], w[maxn], b[maxn], p[maxn], r[maxn], n, m;
void init()
{
memset(u, , sizeof(u)); memset(v, , sizeof(v)); memset(w, , sizeof(w));
memset(b, , sizeof(b)); memset(p, , sizeof(p)); memset(r, , sizeof(r));
} int cmp(const int i, const int j)
{
return w[i] < w[j];
} int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
} int Kruscal()
{
int ans = ;
for(int i = ; i < n; i++) p[i] = i;
for(int i = ; i < m; i++) r[i] = i;
sort(r, r+m, cmp);
for(int i = ; i < m; i++){
int e = r[i];
int x = find(u[e]), y = find(v[e]);
if(x != y) {
ans += w[e];
p[x] = y; //合并
}
}
return ans;
} int main()
{
while(~scanf("%d", &n) && n){
init();
m = n*(n-)/;
for(int i = ; i < m; i++){
scanf("%d%d%d%d", &u[i], &v[i], &w[i], &b[i]);
if(b[i] == ) w[i] = ;
}
int cnt = Kruscal();
printf("%d\n", cnt);
}
return ;
}
hdu 1879 继续畅通工程的更多相关文章
- hdu 1879 继续畅通工程 (并查集+最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1879 继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1879 继续畅通工程(最小生成树)
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经 ...
- hdu 1879 继续畅通工程 (最小生成树)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 1879 继续畅通工程(Kruskra)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- hdu 1879 继续畅通工程 解题报告
题目链接:http://code.hdu.edu.cn/showproblem.php?pid=1879 这条题目我的做法与解决Constructing Roads的解法是相同的. 0 表示没有连通: ...
- hdu 1879 继续畅通工程(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<algo ...
- HDU 1879 继续畅通工程(Prim||Kruscal模板题)
原题链接 Prim(点归并) //异或运算:相同为假,不同为真 #include<cstdio> #include<algorithm> #define maxn 105 us ...
- (step6.1.1)hdu 1879(继续畅通工程——最小生成树、kruscal)
题目大意:输入一个整数n,表示有n个村庄.在接下来的n(n-1)/2行中,每行有4个整数begin end weight flag.分别表示从begin到end之间可以连通 ,他们之间的费用为w ...
随机推荐
- except ShortInputException,x中逗号
class ShortInputException(Exception): def __init__(self, length, atleast): Exception.__init__(self) ...
- IOS格式规范
IOS格式规范 目录 概述 日期格式 NSDateFormatter格式说明 概述 日期格式 声明时间格式:NSDateFormatter *date_formatter = [[NSDateForm ...
- 代码片段 - JavaScript 字符串模板
/* ------------------------------ // 字符串模板1,语法严格,不能混用,效率相对较高 // 使用 {{ }} 作为标记是为了允许在模板中使用 JSON 字符串 // ...
- .Net中的socket编程例子
vb2010: '发送端代码Public Class Form1 Inherits System.Windows.Forms.FormPrivate Sub Button1_Click(ByVa ...
- oc中的block使用心得
typedef void (^ simpleBlock) (void); typedef double (^multiplyTwoValues)(double, double); typedef vo ...
- js兼容性问题
javscript 浏览器兼容性问题: scrollTop = document.documentElement.scrollTop || document.body.scrollTop; scrol ...
- 免费公测:RDS只读实例
免费公测:RDS只读实例 简要介绍 在对数据库有少量写请求,但有大量的读请求的应用场景下,单个实例可能无法抵抗读取压力, 甚至对主流程业务产生影响.为了实现读取能力的弹性扩展,分担数据库压力,阿里 ...
- 使用 xcode 8 构建版本 iTunes Connect 获取不到应用程序的状态
今天在提交 APP 审核版本的时候iTunes Connect 一直获取不到应用程序的状态,原因是,14号 xcode 8一出 直接升级使用 xcode 8 打包发布包,然后直接用 xcode8 构建 ...
- 【Linux】gdb调试core文件
编写服务器端程序,很容易遇到Crash问题,比较幸运的是Linux提供了core file,保留了Crash的现场.有时候,根据当前的调用栈,并且打印出当前栈的变量就可以分析出crash的原因,但是, ...
- [转]Displaying standard DataTables in MVC
本文转自:http://stackoverflow.com/questions/2243898/displaying-standard-datatables-in-mvc Controller act ...