HDU 1233 还是畅通工程 (最小生成树)
还是畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30542 Accepted Submission(s): 13670
当N为0时,输入结束,该用例不被处理。
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0
5
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE],N,M,NUM;
int MAP[SIZE][SIZE];
struct Node
{
int from,to,cost;
}G[SIZE * SIZE]; void ini(void);
int find_father(int);
void unite(int,int);
bool same(int,int);
int kruskal(void);
bool comp(const Node &,const Node &);
int main(void)
{
while(scanf("%d",&N) && N)
{
ini();
M = N * (N - ) / ;
for(int i = ;i < M;i ++)
{
scanf("%d%d%d",&G[NUM].from,&G[NUM].to,&G[NUM].cost);
NUM ++;
}
sort(G,G + NUM,comp);
kruskal();
} return ;
} void ini(void)
{
NUM = ;
for(int i = ;i <= N;i ++)
FATHER[i] = i;
} int find_father(int n)
{
if(FATHER[n] == n)
return n;
return FATHER[n] = find_father(FATHER[n]);
} void unite(int x,int y)
{
x = find_father(x);
y = find_father(y); if(x == y)
return ;
FATHER[x] = y;
} bool same(int x,int y)
{
return find_father(x) == find_father(y);
} bool comp(const Node & a,const Node & b)
{
return a.cost < b.cost;
} int kruskal(void)
{
int count = ,ans = ; for(int i = ;i < NUM;i ++)
if(!same(G[i].from,G[i].to))
{
unite(G[i].from,G[i].to);
count ++;
ans += G[i].cost;
if(count == N - )
break;
}
printf("%d\n",ans);
}
HDU 1233 还是畅通工程 (最小生成树)的更多相关文章
- hdu 1233 还是畅通工程 最小生成树(prim算法 + kruskal算法)
还是畅通工程 Time Limit: 4000/2 ...
- HDU 1233.还是畅通工程-最小生成树(Prime)
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- (step6.1.5)hdu 1233(还是畅通工程——最小生成树)
题目大意:输入一个整数n,表示有n个村庄,在接下来的n*(n-1)/2中,每行有3个整数beigin.end.weight,分别表示路的起始村庄,结束村庄和村庄之间的距离. 求索要修的路的最短距离 解 ...
- HDU 1233 还是畅通工程(最小生成树,prim)
题意:中文题目 思路:prim实现,因为有n*(n-1)/2条边,已经是饱和的边了,prim比较合适. (1)将点1置为浏览过,点1可以到达其他每个点,所以用low[i]数组记录下目前到达i点的最小长 ...
- HDU.1233 还是畅通工程(Prim)
HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...
- hdu 1233 还是畅通工程 (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 1233 还是畅通工程(Kruskal算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 1233 还是畅通工程 并查集or最小生成树
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路 ...
- hdu 1233:还是畅通工程(数据结构,图,最小生成树,普里姆(Prim)算法)
还是畅通工程 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
随机推荐
- Castle IOC容器构建配置详解(二)
主要内容 1.基本类型配置 2.Array类型配置 3.List类型配置 4.Dictionary类型配置 5.自定义类型转换 一.基本类型配置 在Castle IOC的配置文件中,大家可能都已经注意 ...
- sql 分组后 组内排名
语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW ...
- UVa11205 The Broken Pedometer
// 题意:有P个LED灯,以及N个字符,要求选出个数最少的LED灯,使得即使只有这些灯正常工作,也能区分出这N个字符 // 题意抽象:输入两个整数P, N以及N行P列的01矩阵,找少的列,能区分所有 ...
- PL/pgSQL学习笔记之七
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 如果一个PL/pgSQL函数声明了输出参数,输出参数被赋予$n名 ...
- UVA 1108 - Mining Your Own Business
刘汝佳书上都给出了完整的代码 在这里理一下思路: 由题意知肯定存在一个或者多个双连通分量: 假设某一个双连通分量有割顶.那太平井一定不能打在割顶上. 而是选择割顶之外的随意一个点: 假设没有割顶,则要 ...
- jQuery Mobile和PhoneGap混合开发
其实二者并不影响,PhoneGap负责调用系统的接口,jQuery Mobile实现一些网页效果.PhoneGap开发请看上一篇文章,jQuery Mobile开发环境搭建如下:[请先阅读上一篇文章, ...
- HDU 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- 今天弱爆了,svn创建项目
今天弱爆了 1.再svnRoot下新建你要建的项目名如:hqdj 文件夹,然后选中它点击右键选中create repository here... ,选择文件系统类型 2.进入conf文件夹进行配置 ...
- cocos2dx动画Animation介绍
一.帧动画 你能够通过一系列图片文件,像例如以下这样,创建一个动画: [cpp] CCAnimation *animation = CCAnimation::create(); //从本 ...
- SAO总结
Application Structure Web App-------------------------------------------------------------->Serve ...