hdoj1233 还是畅通工程(Prime || Kruskal)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1233
思路
最小生成树问题,使用Prime算法或者Kruskal算法解决。
代码
Prime算法:
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std; const int INF = 0xfffffff;
const int N = + ;
int map[N][N];
int dist[N]; //存储从起点到其余各点的距离,不断更新
int n; void prime()
{
int min_edge, min_node;
for (int i = ;i <= n;i++)
dist[i] = INF;
int ans = ;
int now = ;
for (int i = ;i < n;i++)
{
dist[now] = -;
min_edge = INF;
for (int j = ;j <= n;j++)
{
if (j != now && dist[j] >= )
{
if (map[now][j]>)
dist[j] = min(dist[j], map[now][j]);
if (dist[j] < min_edge)
{
min_edge = dist[j]; //min_edge存储与当前结点相连的最短的边
min_node = j;
}
}
}
ans += min_edge; //ans存储最小生成树的长度
now = min_node;
}
printf("%d\n", ans);
} int main()
{
//freopen("hdoj1233.txt", "r", stdin);
while (scanf("%d", &n) == && n)
{
memset(map, , sizeof(map));
int a, b, c;
int nums = n*(n - ) / ;
for (int i = ; i < nums; i++)
{
scanf("%d%d%d", &a, &b, &c);
map[a][b] = c;
map[b][a] = c;
}
prime();
}
return ;
}
Kruskal算法:
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std; struct Edge
{
int a, b, dist; Edge() {}
Edge(int a, int b, int d) :a(a), b(b), dist(d) {}
bool operator < (Edge edge) //按边长从短到长排序
{
return dist < edge.dist;
}
}; const int N = 100 + 10;
int p[N]; //并查集使用
vector<Edge> v;
int n; int find_root(int x)
{
if (p[x] == -)
return x;
else return find_root(p[x]);
} void kruskal()
{
memset(p, -, sizeof(p));
sort(v.begin(), v.end());
int ans = ;
for (int i = ; i < v.size(); i++)
{
int ra = find_root(v[i].a);
int rb = find_root(v[i].b);
if (ra != rb)
{
ans += v[i].dist;
p[ra] = rb;
}
}
printf("%d\n", ans);
} int main()
{
//freopen("hdoj1233.txt", "r", stdin);
while (scanf("%d", &n) == && n)
{
int a, b, d;
int nums = n*(n - ) / ;
v.clear();
for (int i = ; i < nums; i++)
{
scanf("%d%d%d", &a, &b, &d);
v.push_back(Edge(a, b, d));
}
kruskal();
}
}
hdoj1233 还是畅通工程(Prime || Kruskal)的更多相关文章
- HDU 1233 还是畅通工程(Kruskal算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 1875 畅通工程再续(kruskal)
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1232 畅通工程(Kruskal)
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 畅通工程(kruskal算法)
个人心得:日了狗,WR了俩个小时才发现是少了个vector清理,我也是醉了,不过后面还是对这个有了更好得了解,一是我得算法,而是学长改进 后的算法,改进后得算法还要判断所有村庄是否在连在一起,其实我觉 ...
- 继续畅通工程(kruskal prim)
kruskal算法 #include <cstdio > #include <algorithm> using namespace std; const int MaxSi ...
- HDU 畅通工程系列
畅通工程系列都是比较裸的最小生成树问题,且是中文题目,不赘述了. 1.HDU 1863 畅通工程 题意:一个省有很多村庄,其中一些之间是可以建公路的,每条公路都需要不同的代价,问代价最小的情况下将所有 ...
- hdu1875 畅通工程再续 最小生成树并查集解决---kruskal
http://acm.hdu.edu.cn/showproblem.php?pid=1875 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...
- HDU1875——畅通工程再续(最小生成树:Kruskal算法)
畅通工程再续 Description相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当 ...
- prime算法求最小生成树(畅通工程再续)
连着做了四道畅通工程的题,其实都是一个套路,转化为可以求最小生成树的形式求最小生成树即可 这道题需要注意: 1:因为满足路的长度在10到1000之间才能建路,所以不满足条件的路径长度可以初始化为无穷 ...
随机推荐
- ASP.NET中的另类控件
首先看一个aspx文件里的部分内容: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- LintCode 388: Kth Permutation
LintCode 388: Kth Permutation 题目描述 给定 n 和 k,求123..n组成的排列中的第 k 个排列. 样例 对于 n = 3, 所有的排列如下: 123 132 213 ...
- 创建分区swap分区
1.将文件系统卸载 #umount /sdc5 2.创建swap分区 #mkswap /dev/sdc5 3.激活swap分区 #swapon -a /dev/sdc5 4.查看swap分区情况 #s ...
- CodeForces - 1009B Minimum Ternary String
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...
- HDU 4720 Naive and Silly Muggles 平面几何
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4720 解题报告:给出一个三角形的三个顶点坐标,要求用一个最小的圆将这个三个点都包含在内,另外输入一个点 ...
- C++中全排列函数next_permutation用法
最近做了TjuOj上关于全排列的几个题,室友告诉了一个非常好用的函数,谷歌之,整理如下: next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_pe ...
- HDU 3449 Consumer (背包问题之有依赖背包)
题目链接 Problem Description FJ is going to do some shopping, and before that, he needs some boxes to ca ...
- 在JS数组指定位置插入元素
很多与数组有关的任务听起来很简单,但实际情况并不总是如此,而开发人员在很多时候也用不到他.最近我碰到了这样一个需求: 将一个元素插入到现有数组的特定索引处.听起来很容易和常见,但需要一点时间来研究它. ...
- 使用mysql的SUBSTRING_INDEX函数解决项目中编码非重复问题的实现方案!
一 SUBSTRING_INDEX函数介绍 作用:按关键字截取字符串 substring_index(str,delim,count) 说明:substring_index(被截取字段,关键字,关键字 ...
- python3之pymysql模块
1.python3 MySQL数据库链接模块 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 Pyt ...