HDU 1879 继续畅通工程(最小生成树)
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 <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
bool cmp(int x,int y)
{
return x>y;
}
const int N=;
const int mod=1e9+;
int f[N];
struct edge
{
int u,v,w,flag;
bool operator <(const edge &other)const
{
return w<other.w;
}
}a[N];
void init()
{
for(int i=;i<=N;i++)
f[i]=i;
}
int find1(int x)
{
if(x!=f[x])
f[x]=find1(f[x]);
return f[x];
}
int main()
{
std::ios::sync_with_stdio(false);
int n,m;
while(scanf("%d",&n)&&n){
init();
m=n*(n-)/;
for(int i=;i<m;i++){
scanf("%d %d %d %d",&a[i].u,&a[i].v,&a[i].w,&a[i].flag);
if(a[i].flag) f[a[i].u]=a[i].v;
}
sort(a,a+m);
int s=;
for(int i=;i<m;i++){
int u=a[i].u,v=a[i].v,w=a[i].w;
if(find1(u)==find1(v)) continue;
f[find1(u)]=find1(v);
s+=w;
}
printf("%d\n",s);
}
return ;
}
HDU 1879 继续畅通工程(最小生成树)的更多相关文章
- hdu 1879 继续畅通工程 (最小生成树)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- (step6.1.1)hdu 1879(继续畅通工程——最小生成树、kruscal)
题目大意:输入一个整数n,表示有n个村庄.在接下来的n(n-1)/2行中,每行有4个整数begin end weight flag.分别表示从begin到end之间可以连通 ,他们之间的费用为w ...
- hdu 1879 继续畅通工程
/************************************************************************/ /* hdu 1879 继续畅通工程 Time L ...
- hdu 1879 继续畅通工程 (并查集+最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1879 继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1879 继续通畅工程(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1879 /************************************************* ...
- hdu 1233 还是畅通工程 最小生成树(prim算法 + kruskal算法)
还是畅通工程 Time Limit: 4000/2 ...
- HDU 1233 还是畅通工程 (最小生成树)
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1879 继续畅通工程(Kruskra)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
随机推荐
- Mac OSX上卸载Anaconda
方案一 anaconda安装程序在~/.bash_profile脚本中新添加了一行,将anaconda bin目录添加到了$PATH环境变量中.所以你只需要删除anaconda目录,但是最好也从安装脚 ...
- MySQL8.0.11 组复制配置
my.cnf [mysql] prompt='node2 [\h] {\u} (\d) > ' # [client] user = sa password = cc.123 port = 22 ...
- linux上pem格式私钥转pfx格式证书的命令
1.root.csr 可改成其它名字,后缀名不改 openssl req -new -key 私钥名称.pem -out root.csr 2.root.crt 可改成其它名字,后缀名不改 opens ...
- 笔记:yum和apt-get的区别
rpm包和deb包是两种Linux系统下最常见的安装包格式,在安装一些软件或服务的时候免不了要和它们打交道. rpm包主要应用在RedHat系列包括 Fedora等发行版的Linux系统上, deb包 ...
- 20165321 学习基础与C语言学习心得
一.技能学习 我其实在小时候学过挺多东西,在我小学的时候,我曾经短时间地学过小提琴.拉丁舞.国画.书法,但是,由于各种原因,都没有继续学习下去.后来,在我小学四年级的时候,我接触到了二胡,于是,我开始 ...
- 将多行按分隔符"|"合成一行
原数据文件s.txt api_test account info 参数错误 合并后数据格式 api_test|account||参数错误||||| 方法介绍: 1.sed + xargs sed 's ...
- python 判断字符串是否包含子字符串
第一种方法:in string = 'helloworld' if 'world' in string: print 'Exist' else: print 'Not exist' 第二种方法:fin ...
- (已解决)Xcode 换电脑提示 Could not attach to pid:“XXXX”错误
在运行项目时出现了如下错误 (基本上重新启动项目即可) 紧接着再次运行又没有问题了. 稍微查询了一下得知,这个问题并不是由我们的操作引起的,有时就会莫名其妙的出现,但是有一些不同的情况 下面列出如何解 ...
- .net core 路由处理请求流程图
- python爬虫-基础入门-爬取整个网站《3》
python爬虫-基础入门-爬取整个网站<3> 描述: 前两章粗略的讲述了python2.python3爬取整个网站,这章节简单的记录一下python2.python3的区别 python ...