题目链接

http://poj.org/problem?id=1251

题意

有n个村庄,村庄之间有道路连接,求一条最短的路径能够连接起所有村庄,输出这条最短路径的长度。

思路

最小生成树问题,使用普利姆算法(Prime)或者克鲁斯卡尔算法(Kruskal)解决。

代码

Prime算法:

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int INF = 0xfffffff;
const int N = ;
int n;
int jungle[N][N];
int dist[N]; //记录从起点到其余各点的距离并不断更新 int prime()
{
int min_edge, min_node;
for (int i = ; i < n; i++)
dist[i] = INF;
int now = ;
int ans = ;
for (int i = ; i < n - ; i++)
{
dist[now] = -; //标记结点now+'A'被访问过了
min_edge = INF;
for (int j = ; j < n; j++)
{
if (j != now && dist[j]>=)
{
if (jungle[now][j]>)
dist[j] = min(dist[j], jungle[now][j]);
if (dist[j] < min_edge)
{
min_edge = dist[j]; //选取从当前结点到其余各点的最短路径
min_node = j;
}
}
}
now = min_node;
ans += min_edge; //当前最小生成树的长度
}
return ans;
} int main()
{
//freopen("poj1251.txt", "r", stdin);
while (cin >> n && n)
{
memset(jungle, , sizeof(jungle));
for (int i = ;i < n-;i++)
{
char p, q;
int v, w;
cin >> p >> v;
for (int j = ;j < v;j++)
{
cin >> q >> w;
jungle[p - 'A'][q - 'A'] = w;
jungle[q - 'A'][p - 'A'] = w;
}
}
int ans = prime();
cout << ans << endl;
}
return ;
}

Kruskal算法:

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int N = ;
int s[N], e[N], v[N]; //分别存储每一条路的起点、终点、长度
int p[N]; //并查集使用
int n;
int cnt; //存储有多少条路 bool cmp(int i, int j)
{
return v[i] < v[j];
} int find_root(int x)
{
if (p[x] == -)
return x;
else return find_root(p[x]);
} int kruskal()
{
memset(p, -, sizeof(p));
int r[N];
for (int i = ; i < cnt; i++)
r[i] = i;
sort(r, r + cnt, cmp); //根据路径长度v[]的大小对数组r[]排序
int ans = ;
for (int i = ; i < cnt; i++)
{
int cur = r[i];
int a = find_root(s[cur]);
int b = find_root(e[cur]);
if (a != b)
{
ans += v[cur];
p[a] = b;
}
}
return ans;
} int main()
{
//freopen("poj1251.txt", "r", stdin);
while (cin >> n && n)
{
cnt = ;
char p, q;
int nums, w;
for (int i = ; i < n - ; i++)
{
cin >> p >> nums;
for (int j = ; j < nums; j++)
{
cin >> q >> w;
s[cnt] = p - 'A';
e[cnt] = q - 'A';
v[cnt] = w;
cnt++;
}
}
int ans = kruskal();
cout << ans << endl;
}
return ;
}

poj1251 Jungle Roads(Prime || Kruskal)的更多相关文章

  1. POJ1251 Jungle Roads (最小生成树&Kruskal&Prim)题解

    题意: 输入n,然后接下来有n-1行表示边的加边的权值情况.如A 2 B 12 I 25 表示A有两个邻点,B和I,A-B权值是12,A-I权值是25.求连接这棵树的最小权值. 思路: 一开始是在做莫 ...

  2. hdoj1102 Constructing Roads(Prime || Kruskal)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意 有n个村庄(编号1~n),给出n个村庄之间的距离,开始时n个村庄之间已经有了q条路,现在需 ...

  3. HDU-1301 Jungle Roads(最小生成树[Prim])

    Jungle Roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  4. poj1258 Agri-Net(Prime || Kruskal)

    题目链接 http://poj.org/problem?id=1258 题意 有n个农场,现在要在n个农场之间铺设光纤使得n个农场连接起来,求铺设光纤的最短距离. 思路 最小生成树问题,使用Prime ...

  5. hdoj1879 继续畅通工程(Prime || Kruskal)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1879 思路 这题和hdoj1102很像,图中的有一些路已经修好了,对于这些已经修好的路,我们令还需要修 ...

  6. hdoj1863 畅通工程(Prime || Kruskal)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1863 思路 最小生成树问题,使用Prime算法或者Kruskal算法解决.这题在hdoj1233的基础 ...

  7. hdoj1233 还是畅通工程(Prime || Kruskal)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1233 思路 最小生成树问题,使用Prime算法或者Kruskal算法解决. 代码 Prime算法: # ...

  8. 九度OJ 1154:Jungle Roads(丛林路径) (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:832 解决:555 题目描述: The Head Elder of the tropical island of Lagrishan has ...

  9. POJ1251 Jungle Roads(Kruskal)(并查集)

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23882   Accepted: 11193 De ...

随机推荐

  1. bzoj千题计划129:bzoj2007: [Noi2010]海拔

    http://www.lydsy.com/JudgeOnline/problem.php?id=2007 1.所有点的高度一定在0~1之间, 如果有一个点的高度超过了1,那么必定会有人先上坡,再下坡, ...

  2. [POI2007]Zap

    bzoj 1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Descriptio ...

  3. 【转载】wondows下wget的使用

    原文地址:http://www.cnblogs.com/Randy0528/archive/2011/10/21/2219831.html 感觉要放弃windows了,,,哎,,,, 下载window ...

  4. Git Cannot rebase: You have unstaged changes.

    Cannot rebase: You have unstaged changes. 那说明你有修改过的文件git stashgit pull --rebase (每次push之前最好这样做一次)git ...

  5. 【转】一个简单的WCF回调实例

    代码下载:http://files.cnblogs.com/AlwinXu/CallbackService-master.zip 本文转自: http://adamprescott.net/2012/ ...

  6. Docker 初相见

    Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Li ...

  7. java 获得字符串中最大重复子串长度

    参考:http://blog.csdn.net/csdn_yaobo/article/details/50338025 要找一串字符串中,重复的字串长度,.例如ABCX1&ABC,中ABC重复 ...

  8. c++的类型转换(转)

    类型转换机制可以分为:隐式类型转换 和 显示类型转换(强制类型转换) C中的类型转换: 事情要从头说起,这个头就是C语言.我们已经习惯了使用C-like类型转换,因为它强大而且简单. 主要有以下两种形 ...

  9. 树形dp(B - Computer HDU - 2196 )

    题目链接:https://cn.vjudge.net/contest/277955#problem/B 题目大意:首先输入n代表有n个电脑,然后再输入n-1行,每一行输入两个数,t1,t2.代表第(i ...

  10. dropload的使用记录

    这次做一个H5的页面,需要用到上拉加载,下拉刷新的功能,在网上看到ximen写的dropload.js可以满足需求(此处致谢作者),但是用的时候还是踩了一些坑,这里记录下来备忘. 一些小问题:1. m ...