继续畅通工程

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18218    Accepted Submission(s): 7891

Problem Description

政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表
中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
 
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
 
Author
ZJU

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500
#define maxm 260000 struct Edge
{
int u,v,w,s;
}edge[maxm];
int n,cnt1;
int ans;
int root[maxn];
int cmp(Edge a,Edge b)
{
if(a.s != b.s)
return a.s > b.s;
return a.w < b.w;
} void init()
{
for(int i = ; i < ; i++)
root[i] = i;
}
int find_root(int x)
{
if(x != root[x])
root[x] = find_root(root[x]);
return root[x];
}
void uni(int a, int b, int c, int d)
{
int x = find_root(a);
int y = find_root(b);
if(x != y)
{
root[y] = x;
if(!d)
ans += c;
cnt1++;
}
}
int main()
{
while(~scanf("%d", &n) && n)
{
int m = n*(n-)/;
for(int i = ; i < m; i++)
scanf("%d%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w, &edge[i].s);
init();
sort(edge, edge+m, cmp);
ans = ;
cnt1 = ;
for(int i = ; i < m; i++)
{
uni(edge[i].u, edge[i].v, edge[i].w, edge[i].s);
if(cnt1 == n-)
break;
}
printf("%d\n", ans);
}
return ;
}

HDU1879 继续畅通工程 (并查集)的更多相关文章

  1. HDU1232 畅通工程 并查集

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. SQL Server索引 - 索引(物化)视图 <第九篇>

    一.索引视图基本概念 索引视图实际上是一种将一组唯一值“物化”为群集索引形式的视图,所为物化就是几乎和表一样,其数据也是会存储一份的(会占用硬盘空间,但是查询速度快,例如可以将count(),sum( ...

  2. 数据类型MSVC和gcc/g++的不同

    前言: 在16位环境下,int/unsigned int 占16位,long/unsigned long占32位 在32位环境下,int占32位,unsigned int占16位,long/unsig ...

  3. GetCurrentDirectory、SetCurrentDirectory和GetModuleFileName

    DWORD GetCurrentDirectory( DWORD nBufferLength, // size of directory buffer LPTSTR lpBuffer      // ...

  4. ubuntu 12.04(Precise Pangolin)启用休眠(Hibernate)功能的方案

    官方支持文档:https://help.ubuntu.com/12.04/ubuntu-help/power-hibernate.html 按照这篇官方支持,因为休眠功能存在bug,12.04没有默认 ...

  5. C++对象模型浅析

    本文仅代表博主自己对C++内存对象模型的一点理解,如果文中有 理解偏差和不准确的地方,希望各位大大提出,我好及时改正. 本博文只对博主自己负责,不对任何人负责. 就如<深度探索C++对象模型&g ...

  6. LeeCode-Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. int mySqrt(int x) { ) ; /* for(i ...

  7. tangible T4 Editor 2.2.3 plus modeling tools for VS 2012 扩展名

    tangible T4 Editor 2.2.3 plus modeling tools for VS 2012 扩展名 tangible T4 Editor 2.2.3 plus modeling ...

  8. CDH 2、Cloudera Manager的安装

    1.Cloudera Manager • Cloudera Manager是一个管理CDH的端到端的应用. • 作用: – 管理 – 监控 – 诊断 – 集成 • 架构 • Server – 管理控制 ...

  9. cocos2d-x 3.0游戏实例学习笔记 《跑酷》 完结篇--源代码放送

    说明:这里是借鉴:晓风残月前辈的博客,他是将泰然网的跑酷教程,用cocos2d-x 2.X 版本号重写的,眼下我正在学习cocos2d-X3.0 于是就用cocos2d-X 3.0重写,并做相关笔记 ...

  10. 【数学.前左上计数法】【HDU1220】Cube

    Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...