畅通工程

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27097 Accepted Submission(s): 11840

Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。

Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。

Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。

Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100

Sample Output
3
?

Source
浙大计算机研究生复试上机考试-2007年

#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
class Union_Find_Set {
#define MAX_UNION_FIND_SET_SIZE 105
public:
int setSize;
int father[MAX_UNION_FIND_SET_SIZE];
Union_Find_Set() {
setSize = ;
}
Union_Find_Set(int x) {
setSize = x;
clear(x);
}
void clear(int x) {
for (int i = ; i < x; i++) {
father[i] = i;
}
}
int getFather(int x) {
if (x != father[x]) {
father[x] = getFather(father[x]);
}
return father[x];
}
bool merge(int a, int b) {
a = getFather(a);
b = getFather(b);
if (a != b) {
father[a] = b;
return true;
} else {
return false;
}
}
int countRoot() {
int ret = ;
for (int i = ; i < setSize; i++) {
if (father[i] = i) {
ret++;
}
}
return ret;
}
};
class Kruskal {
#define Kruskal_MAXN 100
#define Kruskal_MAXM 10005
public:
Union_Find_Set ufs;
int x[Kruskal_MAXM], y[Kruskal_MAXM], w[Kruskal_MAXM], N, M;
Kruskal() {
N = ;
M = ;
}
void clear() {
N = ;
M = ;
}
void addEdge(int a, int b, int c) {
x[M] = a;
y[M] = b;
w[M] = c;
M++;
x[M] = b;
y[M] = a;
w[M] = c;
M++;
}
void sort(int l, int r) {
int i = l, j = r, m = w[(l + r) >> ], t;
do {
while (w[i] < m) {
i++;
}
while (m < w[j]) {
j--;
}
if (i <= j) {
t = x[i];
x[i] = x[j];
x[j] = t;
t = y[i];
y[i] = y[j];
y[j] = t;
t = w[i];
w[i] = w[j];
w[j] = t;
i++;
j--;
}
} while (i <= j);
if (l < j) {
sort(l, j);
}
if (i < r) {
sort(i, r);
}
}
int MST() {
sort(, M - );
ufs.clear(N + );
int cnt = , ret = ;
for (int i = ; i < M; i++) {
if (cnt == N - ) {
return ret;
}
if (ufs.getFather(x[i]) != ufs.getFather(y[i])) {
ufs.merge(x[i], y[i]);
ret += w[i];
cnt++;
}
}
if (cnt == N - ) {
return ret;
} else {
return -;
}
}
};
Kruskal Kr;
int main() {
int n, m;
while (scanf("%d%d", &m, &n) != EOF) {
if (m == ) {
break;
}
Kr.clear();
Kr.N = n;
for (int i = ; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
Kr.addEdge(a, b, c);
}
int ans = Kr.MST();
if (ans == -) {
printf("?\n");
} else {
printf("%d\n", ans);
}
}
return ;
}

畅通工程[HDU1863]的更多相关文章

  1. 最小生成树算法 prim kruskal两种算法实现 HDU-1863 畅通工程

    最小生成树 通俗解释:一个连通图,可将这个连通图删减任意条边,仍然保持连通图的状态并且所有边权值加起来的总和使其达到最小.这就是最小生成树 可以参考下图,便于理解 原来的图: 最小生成树(蓝色线): ...

  2. hdu1863 畅通工程(最小生成树之prim)

    Problem Description 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可 ...

  3. Kruskal算法-HDU1863畅通工程

    链接 [http://acm.hdu.edu.cn/showproblem.php?pid=1863] 题意 Problem Description 省政府"畅通工程"的目标是使全 ...

  4. 畅通工程(hdu1863)并查集

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

  5. HDU1863 畅通工程 2017-04-12 19:25 59人阅读 评论(0) 收藏

    畅通工程 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  6. HDU1863 畅通工程---(最小生成树)

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

  7. hdu1863畅通工程

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

  8. HDU 1233 还是畅通工程(最小生成树)

    传送门 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. 所有的畅通工程[HDU1232][HDU1874][HDU1875][HDU1879]

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

随机推荐

  1. 如何同时打开两个excel

    1. 打开一个excel1 2. 不要双击想要打开的excel2.右键excel应用的图标,选择excel2007. 3. 将excel2拖动到2所打开的新建excel中. 4. over.

  2. Dom4j把xml转换成Map(固定格式)

    /** * 可解析list * * @param fileName * @return * @throws Exception */ @SuppressWarnings("unchecked ...

  3. linux下apache各种跳转(包括伪静态)的配置

      1.404跳转: vi /etc/httpd/conf/httpd.conf 在虚拟主机配置里添加一行:ErrorDocument 404 /404.html 2.301跳转: 1)将不带www的 ...

  4. [MapReduce] Google三驾马车:GFS、MapReduce和Bigtable

    声明:此文转载自博客开发团队的博客,尊重原创工作.该文适合学分布式系统之前,作为背景介绍来读. 谈到分布式系统,就不得不提Google的三驾马车:Google FS[1],MapReduce[2],B ...

  5. iframe的自适应高度

    <iframe src="index.html" id="iframepage" name="iframepage" frameBor ...

  6. 如何挂载阿里云Linux服务器的“数据盘”(新购买)

    详细操作参考: http://jingyan.baidu.com/article/90808022d2e9a3fd91c80fe9.html 用到的命令行汇总: 1.查看磁盘: fdisk -l 2. ...

  7. ssh框架整合-NoClassDefFoundError-NoSuchMethodError-遁地龙卷风

    (-1)写在前面 spring2.0.struts1.2.hibernate3.0.myeclipse8.5.tomcat6.0,整合之中出现了很多问题,前几天忙着整理毕业论文的资料,时间腾出来了,总 ...

  8. python之路六

    面向对象 引言 提到面向对象,总是离不开几个重要的术语:多态(Polymorphism),继承(Inheritance)和封装(Encapsulation).Python也是一种支持OOP的动态语言, ...

  9. Opera Browser -- Access Restricted Sites using Free VPN /Free VPN Services List

    Opera Browser  -- Access Restricted Sites using Free VPN: currently the feature is available in Oper ...

  10. python DBUtils.PooledDB 中 maxcached 和 maxconnections

    PooledDB 有这么几个参数 mincached : the initial number of idle connections in the pool (the default of 0 me ...