继续畅通project

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 14309    Accepted Submission(s): 6228

Problem Description
省政府“畅通project”的目标是使全省不论什么两个村庄间都能够实现公路交通(但不一定有直接的公路相连,仅仅要能间接通过公路可达就可以)。现得到城镇道路统计表,表中列出了随意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编敲代码,计算出全省畅通须要的最低成本。
 
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
#include <stdio.h>
#include<string.h>
#include <algorithm>
using namespace std;
typedef struct Road
{
int c1, c2, cost, state;
}Road; bool myCompare(const Road &a, const Road &b)
{
if(a.cost < b.cost)
return 1;
return 0;
} Road road[5051];
int city[101]; int Find(int n)
{
if(city[n] == -1)
return n;
return city[n] = Find(city[n]);
} bool Merge(int s1, int s2)
{
int r1 = Find(s1);
int r2 = Find(s2);
if(r1 == r2)
return 0;
if(r1 < r2)
city[r2] = r1;
else
city[r1] = r2;
return 1;
} int main()
{
//freopen("input.txt", "r", stdin);
int n;
while(scanf("%d", &n) && n)
{
int m = n*(n-1)/2;
memset(city, -1, sizeof(city));
int count = 0;
for(int i=0; i<m; ++i)
{
scanf("%d %d %d %d", &road[i].c1, &road[i].c2, &road[i].cost, &road[i].state);
if(road[i].state == 1)
{
count ++;
Merge(road[i].c1, road[i].c2);
}
}
sort(road, road+m, myCompare);
int sum = 0;
for(int i=0; i<m; ++i)
{
if(Merge(road[i].c1, road[i].c2) && road[i].state == 0)
{
count ++;
sum += road[i].cost;
}
if(count == n-1)
break;
}
printf("%d\n", sum);
}
return 0;
}

#include <stdio.h>
#include<string.h>
#include <algorithm>
int city[5000];
using namespace std;
struct Road
{
int c1;
int c2;
int cost;
int state;
};
Road road[110]; bool cmp(Road a,Road b)
{
if(a.cost<b.cost)
return 1;
return 0;
}
int find(int a)
{
if(city[a]==-1)
return a;
return city[a]=find(city[a]);
}
bool merge(int x, int y)
{
int fx,fy;
fx = find(x);
fy = find(y);
if(fx == fy) return 0;
else if(fx < fy) //不能省去
city[fy] = fx;
else
city[fx] = fy;
return 1;
} int main()
{
int n, m;
while(scanf("%d",&n),n)
{
m=n*(n-1)/2;
int i;
int count = 0;
int sum = 0;
memset(city,-1,sizeof(city)); for(i=0; i<m; i++)
{
scanf("%d %d %d %d",&road[i].c1, &road[i].c2, &road[i].cost, &road[i].state);
if(road[i].state == 1)
{
count ++;
merge(road[i].c1, road[i].c2);
}
}
sort(road, road+m,cmp); for(i=0; i<m; i++)
{
if(merge(road[i].c1, road[i].c2) && road[i].state==0)
{
count ++;
sum += road[i].cost;
}
if(count == n-1)
break;
}
printf("%d\n",sum);
}
return 0;
}

HDoj-1879-畅通project-并查集的更多相关文章

  1. HDU1232 畅通project 并查集

    这道题跟HDU 1213 How Many Tables 并查集很接近,都是赤裸裸的并查集的题. 思路:如果还须要建n-1条路.每并一次就自减1. 參考代码: #include<stdio.h& ...

  2. HDU1232 畅通工程 并查集

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. ACM: 继续畅通工程-并查集-最小生成树-解题报告

    继续畅通工程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descri ...

  4. ACM: 畅通工程-并查集-解题报告

    畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查城镇交通状况 ...

  5. B - 畅通工程(并查集)

    对并查集理解之后就可以做这种题了,虽说这种题做的不多,这道题做过才这么快搞定,可是还是挺happy滴,加油 Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接 ...

  6. NSOJ 畅通工程(并查集)

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...

  7. hdu 1233 还是畅通工程 并查集or最小生成树

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

  8. hdu1232 畅通工程 并查集的 应用

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. hdu 1863 畅通工程 (并查集+最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 畅通工程 Time Limit: 1000/1000 MS (Java/Others)    M ...

  10. HDU - 1232 畅通工程-并查集模板

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...

随机推荐

  1. windows phone (22) 隐藏元素

    原文:windows phone (22) 隐藏元素 在wp中我们也会用到隐藏某个元素,已达到某种效果,刚刚从文章看到了,分享一下[作者:神舟龙] Visibility 此属性能非常方便的实现隐藏某个 ...

  2. 工作经常使用的SQL整理,实战篇(三)

    原文:工作经常使用的SQL整理,实战篇(三) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...

  3. Android架构分析之LOG模块

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android版本:2.3.7_r1 Linux内核版本:android-goldfish-2.6.29 Andro ...

  4. 请注意CSDN社区微通道,许多其他的精彩等着你

    CSDN社区微信公众号"程序人生"(微信ID:coder_life)来了,每天我们会将CSDN社区中大量的优质内容浓缩成1~3篇文章.推送到您的手机中,让您不管何时何地都能感受到知 ...

  5. 使用SKIP-GRANT-TABLES 解决 MYSQL ROOT密码丢失(转)

    B.5.3.2 How to Reset the Root Password If you have never assigned a root password for MySQL, the ser ...

  6. 小巧的UML工具-UMLet

    画简单的UML图时非常方便 比如我画blog中的流程图就是用的UMLet

  7. freemarker错误七

    1.错误叙述性说明 五月 30, 2014 11:33:57 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template p ...

  8. DataGridView显示数据的两种方法

    1.简介 DataGridView空间是我们经常使用的显示数据的控件,它有极高的可配置性和可扩展性. 2.显示数据 DataGridView显示数据一般我们经常使用的有两种方法,一种是直接设置Data ...

  9. Android利用网络编程HttpClient批量上传(一个)

    请尊重他人的劳动成果.转载请注明出处:Android网络编程之使用HttpClient批量上传文件 我曾在<Android网络编程之使用HTTP訪问网络资源>一文中介绍过HttpCient ...

  10. Apple Watch 1.0 开发介绍 2.1 WatchKit Apps UI要点

    实现app的开始是定义storyboard场景.每个场景定义了app的一部分界面.可以为不同的尺寸自定义场景. 组装storyboard界面 WatchKit app和iOS app的布局模式不同.组 ...