题目链接:点击打开链接

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 ?

思路:刚开始没有看出来是最小生成树。。。看出来之后也没有用kruskal算法。

AC代码:

#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
using namespace std;
const int MAX = 500;
const int INF = 0X3f3f3f; int n, m;
int father[MAX];//以下就是并查集模板 void init() {
for(int i = 1; i <= n; i++) {
father[i] = i;
}
} int findfather(int x) {
int a = x;
while(x != father[x]) {
x = father[x];
}
while(a != father[a]) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
} struct node {
int v;
int u;
int cost;
node() {}
node(int _v, int _u, int _cost) : v(_v), u(_u), cost(_cost) {}//构造函数
}stu[MAX];//存边 bool cmp(node a, node b) {//把所有边 按照从小到大排序
return a.cost < b.cost;
} int main() {
int u, v, w;
while(scanf("%d %d", &m, &n) != EOF) {
if(m == 0)
break;
init();
for(int i = 0; i < m; i++) {//输出所有的边
scanf("%d %d %d", &stu[i].u, &stu[i].v, &stu[i].cost);
}
int ans = 0, num = 0;//边权之和,入树的边数
sort(stu, stu + m, cmp);//排序
for(int i = 0; i < m; i++) {//遍历边
int faA = findfather(stu[i].u);
int faB = findfather(stu[i].v);
if(faA != faB) {//如果不在一个集合内就加入最小树
father[faA] = faB;
num++;//边数加1
ans += stu[i].cost;//累加边权
}
}
if(num == n -1)//树的特征,(判是否连通)
cout << ans << endl;
else
cout << "?" << endl;
}
return 0;
}

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 畅通工程 2017-04-12 19:25 59人阅读 评论(0) 收藏

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

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

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

  6. hdu1863畅通工程

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

  7. HDU1863畅通工程---并查集+最小生成树

    #include<cstdio> #include<algorithm> #define MAX 105 struct edge { int from,to; long lon ...

  8. hdu1863 畅通工程---MST&连通

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1863 题目大意: 中文题,求MST权值,如果不连通,输出? 解题思路: 这道题帮我找出了之前模板中的 ...

  9. hdu1863 畅通工程 基础最小生成树

    #include <iostream> #include <cstdio> #include <algorithm> #define N 110 #define M ...

  10. 畅通工程[HDU1863]

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

随机推荐

  1. stl_multimap.h

    stl_multimap.h // Filename: stl_multimap.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...

  2. poj2420 A Star not a Tree? 模拟退火

    题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...

  3. C/C++面试题总结(2)

    C++部分: 1.static(静态)变量有什么作用? 2.virtual关键字用法 3.const有哪些作用 或<王道程序员求职宝典>P95 4.new/delete与malloc/fr ...

  4. TVYJ1266:费解的开关

    我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:http://www.joyoi.cn/problem/tyvj-1266 这 ...

  5. POJ 1503 Integer Inquiry(大数相加)

    一.Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp ...

  6. Poj 2017 Speed Limit(水题)

    一.Description Bill and Ted are taking a road trip. But the odometer in their car is broken, so they ...

  7. HDUj2612(两个起点找到最近的目的地)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. 对spring、AOP、IOP的理解 (转)

    spring 的优点?1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦2.可以使用容易提供的众多服务,如事务管理,消息服务等3.容器提供单例模式支持4.容器提供了AOP技术,利用它很容易实现如权 ...

  9. Unity堆内存优化

    unity中减少堆内存分配以减少垃圾回收处理:只有局部变量且为值类值的变量是从stack栈中分配内存,其它所有情况都是从heap堆中分配内在.* 缓存获取到的数据.* 频繁被调用的函数中尽量少的分配空 ...

  10. shell script-判断式

    test判断式 看下面: test -e /opt/a.txt && echo "exist" || echo "not exist" 判断 / ...