HDoj-1879-畅通project-并查集
继续畅通project
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14309 Accepted Submission(s): 6228
当N为0时输入结束。
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
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-并查集的更多相关文章
- HDU1232 畅通project 并查集
这道题跟HDU 1213 How Many Tables 并查集很接近,都是赤裸裸的并查集的题. 思路:如果还须要建n-1条路.每并一次就自减1. 參考代码: #include<stdio.h& ...
- HDU1232 畅通工程 并查集
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- ACM: 继续畅通工程-并查集-最小生成树-解题报告
继续畅通工程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descri ...
- ACM: 畅通工程-并查集-解题报告
畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查城镇交通状况 ...
- B - 畅通工程(并查集)
对并查集理解之后就可以做这种题了,虽说这种题做的不多,这道题做过才这么快搞定,可是还是挺happy滴,加油 Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接 ...
- NSOJ 畅通工程(并查集)
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...
- hdu 1233 还是畅通工程 并查集or最小生成树
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路 ...
- hdu1232 畅通工程 并查集的 应用
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- hdu 1863 畅通工程 (并查集+最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 畅通工程 Time Limit: 1000/1000 MS (Java/Others) M ...
- HDU - 1232 畅通工程-并查集模板
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...
随机推荐
- Activity详细解释(生命周期、以各种方式启动Activity、状态保存,等完全退出)
一.什么是Activity? 简单的说:Activity或者悬浮于其它窗体上的交互界面. 在一个应用程序中通常由多个Activity构成.都会在Manifest.xml中指定一个主的Activity, ...
- 利用Sambaserver在Ubuntu系统和Win7系统间共享目录
1 介绍 如今是网络化的时代,我们每一个人要更好的发展.离不开网络化.信息化的支持.利用网络的支持.在不同的操作系统间共享文件等信息,是计算机专业学生必备的一项技能. 本文所讲的就是怎样建立.设置.链 ...
- HDU 1828 Picture(长方形的周长和)
HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...
- WPF Delegate委托整理
那啥,是从这里整理出来的,感谢Rising_Sun,整理的过于简单,看不明白的戳这里 using System; using System.Collections.Generic; using Sys ...
- java HashMap中出现反复的key, 求解释
上代码: Person p1 = new Person("xiaoer",1); Person p2 = new Person("san",4); Map< ...
- 加入指数(IOS开发)
该指数是用来协助查询. 原则上: - 索引的标题是不完全一样的标题显示: - 指数应该具有一定的代表性,它可表示一组数据: - 假设索引列表视图.在一般情况下不再使用扩展视图. (easy指向) 会又 ...
- 递归遍历XML所有节点
package xml; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.DocumentEx ...
- VBoxGuestAdditions.iso下载
http://download.virtualbox.org/virtualbox/4.1.2/VBoxGuestAdditions_4.1.2.iso 其他版本可能反过来确定..
- 高仿快车100--实战RadioGroup和RadioButton应用
1.RadioButtonCheckBox的差别: a.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后.通过点击能够变为未选中 b.一组RadioButton ...
- Android 高仿微信即时聊天 百度云为基础的推
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/38799363 ,本文出自:[张鸿洋的博客] 一直在仿微信界面,今天最终有幸利用百 ...