题目链接

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. [Luogu 3275] SCOI2011 糖果

    [Luogu 3275] SCOI2011 糖果 第一道差分约束.感谢 AZe. 我的理解是根据一些不等关系建一个图,在图上边跑一个最长路(有时候是最短路). 因为可能存在负环,所以必须用 SPFA! ...

  2. [Luogu 3258] JLOI2014 松鼠的新家

    [Luogu 3258] JLOI2014 松鼠的新家 LCA + 树上差分. 我呢,因为是树剖求的 LCA,预处理了 DFN(DFS 序),于是简化成了序列差分. qwq不讲了不讲了,贴代码. #i ...

  3. 2014年最佳的10款 PHP 开发框架

    PHP去年发生了翻天覆地的变化.似乎每个人都有一个想法一个好的框架应该是什么样子,但话又说回来,没有多少面积制品类型的框架或框架的最终实际使用在不同的生产项目. 你知道哪个框架选择为您的生产计划吗?你 ...

  4. webrtc前景如何

    首先WebRTC是什么? WebRTC --- Web browsers with Real-Time Communications (RTC). WebRTC是一个免费.开放的项目.使web浏览器通 ...

  5. 天梯赛 L2-024. (并查集) 部落

    题目链接 题目描述 在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不同的朋友圈.我们认为朋友的朋友都算在一个部落里,于是要请你统计一下,在一个给定社区中,到底有多少个互不相交的部落?并且检查 ...

  6. POJ 1128 Frame Stacking (拓扑排序)

    题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...

  7. C# 图片和Base64之间的转换

    public static Bitmap GetImageFromBase64String(string strBase) { try { MemoryStream stream = new Memo ...

  8. dubbox ExceptionMapper Filter request response 数据获取 数据传递

    dubbx虽然是基于jboss的resteasy实现restfull,但是对resteasy原生的配置却不支持(可能是考虑到dubbo本事的设计模式及实现难度,但是和大部分framework的设计风格 ...

  9. [转]CNN目标检测(一):Faster RCNN详解

    https://blog.csdn.net/a8039974/article/details/77592389 Faster RCNN github : https://github.com/rbgi ...

  10. CSS line-height与行内框

    一.line-height的定义 line-height,行高,是指文本行基线间的垂直距离. 1.    什么是基线? 一般而言,一个文本行一共有四条线,从上到下依次为顶线.中线.基线.底线:在英文中 ...