HDU1879 kruscal 继续畅通工程
继续畅通工程
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17028 Accepted Submission(s): 7319
政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表
中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2
行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示
未建。
当N为0时输入结束。
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
1
0
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int u,v,w,judge;
}que[50000];
int n,cnt;
bool cmp(struct node a,struct node b){
return a.w<b.w;
}
int father[500];
void init(){
for(int i=1;i<=n;i++)
father[i]=i;
}
int find(int a){
if(father[a]!=a)
father[a]=find(father[a]);
return father[a];///不知道为什么return a;就是WA,按理说应该可以的啊,他俩不是相等的时候才返回嘛
}
bool Union(int x1,int x2){
int t1=find(x1);
int t2=find(x2);
if(t1!=t2){
father[t1]=t2;
return true;
}
return false; }
int kruscal(){
int sum=0;
int edge=0;
sort(que,que+cnt,cmp);
for(int i=0;i<cnt;i++){
if(Union(que[i].u,que[i].v)){
sum+=que[i].w;
edge++;
if(edge==n-1)
return sum;
}
}
}
int main(){ while(scanf("%d",&n)!=EOF){
if(n==0)
break;
// memset(father,0,sizeof(father));
init();
cnt=n*(n-1)/2;
for(int i=0;i<cnt;i++){
scanf("%d%d%d%d",&que[i].u,&que[i].v,&que[i].w,&que[i].judge);
if(que[i].judge==1)
que[i].w=0;
}
printf("%d\n",kruscal()); }
return 0;
}
HDU1879 kruscal 继续畅通工程的更多相关文章
- 【HDU1879】继续畅通工程(MST基础题)
真心大水题...不多说. #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
- 所有的畅通工程[HDU1232][HDU1874][HDU1875][HDU1879]
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissio ...
- hdu1879 继续畅通工程
http://acm.hdu.edu.cn/showproblem.php?pid=1879 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...
- 继续畅通工程--hdu1879
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU1879 继续畅通工程 (并查集)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 继续畅通工程(hdu1879)并查集
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU1879 继续畅通工程 2017-04-12 19:12 50人阅读 评论(0) 收藏
继续畅通工程 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- hdu-1879 继续畅通工程---确定部分边的MST
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1879 题目大意: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的 ...
- hdu 1879 继续畅通工程
/************************************************************************/ /* hdu 1879 继续畅通工程 Time L ...
随机推荐
- Daily Scrum – 1/18
Meeting Minutes 完成了User Course, 与 Tips 的设计; 修复了一系列Bug; 完成了夜间模式: Burndown Progress part 组员 今日工作 Time ...
- log4j:WARN Please initialize the log4j system properly.解决
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlA ...
- ansible 使用方法
免密钥方式登陆: [root@yizhen ~]# ssh-keygen -t rsa[root@yizhen ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168 ...
- ansible-2添加公钥
该文章摘自:http://www.fwqtg.net/%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E5%B7%A5%E5%85%B7ansible%E5 ...
- 【转】getHibernateTemplate出现的所有find方法的总结
一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...
- 【BZOJ-2733】永无乡 Splay+启发式合并
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2048 Solved: 1078[Submit][Statu ...
- 【bzoj1853】 Scoi2010—幸运数字
http://www.lydsy.com/JudgeOnline/problem.php?id=1853 (题目链接) 今天考试考了容斥,结果空知道结论却不会写→_→ 题意 求区间中不含6,8两个数字 ...
- Azure怎么使用ftp登录
1.下载配置文件 2.拷贝FTP的地址 3.查看配置文件里面的用户名和密码 4.登录
- MVC模式介绍
MVC是一种通过3个不同部分构造一个软件或组件的理想办法: 1.模型(Model):用于存储数据的对象. 2.视图(View):为模型提供数据显示的对象. 控制器(Controller):负责具体的业 ...
- HD1712ACboy needs your help(纯裸分组背包)
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...