HDU1863-畅通工程
题目链接:点击打开链接
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-畅通工程的更多相关文章
- 最小生成树算法 prim kruskal两种算法实现 HDU-1863 畅通工程
最小生成树 通俗解释:一个连通图,可将这个连通图删减任意条边,仍然保持连通图的状态并且所有边权值加起来的总和使其达到最小.这就是最小生成树 可以参考下图,便于理解 原来的图: 最小生成树(蓝色线): ...
- hdu1863 畅通工程(最小生成树之prim)
Problem Description 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可 ...
- Kruskal算法-HDU1863畅通工程
链接 [http://acm.hdu.edu.cn/showproblem.php?pid=1863] 题意 Problem Description 省政府"畅通工程"的目标是使全 ...
- HDU1863 畅通工程 2017-04-12 19:25 59人阅读 评论(0) 收藏
畅通工程 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- HDU1863 畅通工程---(最小生成树)
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu1863畅通工程
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDU1863畅通工程---并查集+最小生成树
#include<cstdio> #include<algorithm> #define MAX 105 struct edge { int from,to; long lon ...
- hdu1863 畅通工程---MST&连通
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1863 题目大意: 中文题,求MST权值,如果不连通,输出? 解题思路: 这道题帮我找出了之前模板中的 ...
- hdu1863 畅通工程 基础最小生成树
#include <iostream> #include <cstdio> #include <algorithm> #define N 110 #define M ...
- 畅通工程[HDU1863]
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...
随机推荐
- 关于ATML信号定义的理解-1
1.XML中的类型标签: <xs:complexType>复合类型和<xs:simpleTyle>简单类型是数据结构类型,包含了各种类型的属性.可以被子类型继承,继承方式为&l ...
- codeforces 553B B. Kyoya and Permutation(找规律)
题目链接: B. Kyoya and Permutation time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- leetcode 226. Invert Binary Tree(递归)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- NOIP 2011 DAY 2
第一题:计算系数 题目 给定一个多项式 (ax+by)k,请求出多项式展开后xnym 项的系数. 输入 共一行,包含 5 个整数,分别为 a,b,k,n,m,每两个整数之间用一个空格隔开 ...
- BZOJ2563阿狸和桃子的游戏
2563: 阿狸和桃子的游戏 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 952 Solved: 682[Submit][Status][Discu ...
- 洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers
题目描述 Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. ...
- DNSmasq
是一款轻便的主要用于个人电脑的DNS:占用的端口是53(和DNS服务的bind的端口一致):我之所以关注它,就是因为在安装DCOS的时候是不允许占用53端口:但是其实默认安装的CentOS几乎都有这个 ...
- Sentry的要点
1.Apache的Build 在研究Sentry的时候,发现没有bin.jar,只能手工编辑工程,但是编辑发现很多jar包有问题:在访问官网的时候发现左侧菜单中有一项是builds,点开后(https ...
- POJ1442:Black Box
浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=1442 用对顶堆维护第\(k\)小 ...
- android开发中 解决服务器端解析MySql数据时中文显示乱码的情况
首先,还是确认自己MySql账户和密码 1.示例 账户:root 密码:123456 有三个字段 分别是_id .username(插入有中文数据).password 1)首先我们知道 ...