poj1251 Jungle Roads
来源:互联网 发布:哪些网站可以注册域名 编辑:程序博客网 时间:2024/05/01 07:42
poj1251 Jungle Roads
标签:最小生成树
题目链接
/* 题意:输出最小生成树的权值和。每组数据给出节点个数n,以下n-1行, 开头为起始节点,及与它相连的节点数目m,随后m组数据表示和它相连的节点以及边权值(用c++处理空格和换行符)。 思路:Kruskal。*/#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;#define maxn 30typedef struct{ int x, y; int w;}edge;edge e[maxn * maxn / 2];int rank_[maxn], father[maxn], sum;int cmp(edge a, edge b){ // return a.w < b.w;}void make_set(int x){ father[x] = x; rank_[x] = 0;}int find_set(int x){ return x != father[x] ? find_set(father[x]) : father[x];}void union_set(int x, int y, int w){ if(x == y) return ; if(rank_[x] > rank_[y]) father[y] = x; else{ if(rank_[x] == rank_[y]) rank_[y]++; father[x] = y; } sum += w; //}int main(){ int n, m, k, weight, i, j; char a, b; while(scanf("%d", &n) && n){ k = 0, sum = 0; for(i = 0; i < n - 1; i++){ cin >> a >> m, make_set(a - 'A'); for(j = 0; j < m; j++){ cin >> b >> weight, make_set(b - 'A'); e[k].x = a - 'A', e[k].y = b - 'A'; e[k++].w = weight; } } sort(e, e + k, cmp); for(i = 0; i < k; i++) union_set(find_set(e[i].x), find_set(e[i].y), e[i].w); printf("%d\n", sum); } return 0;}
阅读全文
0 0
- poj1251 Jungle Roads
- poj1251 - Jungle Roads
- POJ1251-Jungle Roads
- poj1251 Jungle Roads
- poj1251 Jungle Roads
- POJ1251 Jungle Roads(kru)
- poj1251 Jungle Roads
- Jungle Roads(POJ1251)(primer)
- POJ1251 Jungle Roads
- POJ1251 Jungle Roads
- HDU1301&&POJ1251 Jungle Roads
- POJ1251 Jungle Roads
- poj1251 Jungle Roads
- poj1251 Jungle Roads
- poj1251 Jungle Roads
- POJ1251 Jungle Roads Prim算法
- poj1251 hdu 1301 Jungle Roads
- POJ1251- Jungle Roads(Kruskal)
- 点击标题就可以选中输入框input
- JDBC基本实现原理
- 论SQL语句中的Left join-多表关系
- 基于OpenCV的视频图像组态 (13):VLC Player解码帧数据
- oracle数据库中主要文件
- poj1251 Jungle Roads
- 在CSS中,编写“新闻列表”2.0版
- NOIP2013 神经网络(拓扑)
- Yii2.0实现语言包切换功能
- poj1258 Agri-Net
- poj 1873
- C语言趣味一百道_第十二题
- MySQL数据库下载与安装详细教程
- 728. Self Dividing Numbers