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 ...
随机推荐
- centos 安装 pcre
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luozhonghua2014/article/details/37054235 #rpm -qa | ...
- SegmentedControlIOS使用
代码: import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, SegmentedContr ...
- Visual Studio使用Web Deploy远程发布网站及其配置
https://blog.csdn.net/yzj_xiaoyue/article/details/60574378 废话不多说,直接进入正题(各个步骤请看图片的序号): IIS配置 1.打开服务器 ...
- clone git 项目到 非空目录
如果我们往一个非空的目录下 clone git 项目,就会提示错误信息: fatal: destination path '.' already exists and is not an empty ...
- bootbox.js官方文档中文版
bootbox.js官方文档中文版简介:Bootbox.js是一个小型的JavaScript库,基于Bootstrap模态框开发,用于创建可编程的对话框. 不像原生的alert等对话框,所有的Boot ...
- Linux更改IP地址
1.进入到root用户 2.执行命令:ifconfig 查看本机的名称 3.执行命令:ifconfig eth0 192.168.25.128 netmask 255.255.255.0 //eth ...
- SSLServerSocket代码实现
理解一个设计思想,结合代码是最好的途径.安全套接字服务端的实现代码如下: X509TrustManager MyX509TrustManager = new X509TrustManager() { ...
- c#中取整和取余
"%"为取余. "/"号整型运算是取整,浮点运算时为除法运算.如54/10结果为5,54.0/10.0结果为5.4.而且取整时不进行四舍五入只取整数部分,如54 ...
- python list seq
//test.py list1 = [1, 2, 3]list2 = [4, 5, 6] print cmp(list1, list2)print len(list1), len(list2)prin ...
- opencv安装终结版
---恢复内容开始--- 1.安装Qt5.9.1 Qt官网址:https://download.qt.io/official_releases/qt/ 根据自己的需要选择版本,这里我选择的是qt-op ...