题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301

//HDOJ1301
#include<iostream>
#include<cstring>
using namespace std;
#define MAX 99999
#define LEN 30
int dist[LEN];//某点的权值 起始点到目标点的权值
int map[LEN][LEN];//某点到某点两点之间的权值
bool isvisitd[LEN];//表示某点是否访问过 //初始化map数组 设置为无穷大
void init(int n){
for(int i = ;i<=n;i++) {
for(int j = ;j<=n;j++)
map[i][j] = MAX;
}
} //prim最小生成树的算法
int prim(int n){
int i,j,min,pos,sum;
sum = ; //最小生成树的权值 //初始化,表示没有一点走过
memset(isvisitd,false,sizeof(isvisitd)); //初始化给dist数组赋值
for(i = ;i<=n;i++){
dist[i] = map[][i];
} isvisitd[] = true;//标记1已被访问 从1开始 //找到权值最小点并记下位置
for(i = ;i<n;i++){
min = MAX;
for(j = ;j<=n;j++){
if(!isvisitd[j]&&dist[j]<min){
min = dist[j];
pos = j;//记录下该位置
}
} sum+=min;
isvisitd[pos] = true; //更新权值
for(j = ;j<=n;j++) {
if(!isvisitd[j]&&dist[j]>map[pos][j]){
dist[j] = map[pos][j];
}
} }
return sum;
}
int main(){
int i,j,n,m,len;
char start,end;
while(scanf("%d",&n)!=EOF){
if(n==){
break;
} init(n);//初始化 for(i=;i<n-;i++){
cin>>start>>m;
for( j = ;j<m;j++){
cin>>end>>len;
map[i+][end-'A'+] = len;
map[end-'A'+][i+] = len;
}
}
cout<<prim(n)<<endl;
}
return ; }

运行结果如下:

最小生成树 Prim算法的实现及应用  http://blog.csdn.net/worker90/article/details/6642959

HDOJ 1301 Jungle Roads的更多相关文章

  1. POJ 1251 && HDU 1301 Jungle Roads (最小生成树)

    Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...

  2. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

  3. hdu 1301 Jungle Roads krusckal,最小生成树,并查集

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

  4. POJ 1251 & HDU 1301 Jungle Roads

    题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...

  5. hdu 1301 Jungle Roads

    http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...

  6. Hdu 1301 Jungle Roads (最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...

  7. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

  8. POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】

    题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解  Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...

  9. 最小生成树 || HDU 1301 Jungle Roads

    裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...

随机推荐

  1. 向CDH5集群中添加新的主机节点

    向CDH5集群中添加新的主机节点 步骤一:首先得在新的主机环境中安装JDK,关闭防火墙.修改selinux.NTP时钟与主机同步.修改hosts.与主机配置ssh免密码登录.保证安装好了perl和py ...

  2. flex编译命令相关

    最近碰到几次flex组件集版本问题,mx容器包含s组件,错误百出,会一直提示皮肤文件错误,上网查了一下,只要在工程属性中--->Flex编译器--->附加的编译参数中加入如下命令行即可:- ...

  3. 如何通过Android Studio发布library到jCenter和Maven Central

    http://www.jianshu.com/p/3c63ae866e52# 在Android Studio里,如果你想引入任何library到自己的项目中,只需要很简单的在module的build. ...

  4. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  5. LeetCode100:Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  6. 根据路径获得文件名以及Aspose.Cells.dll操作excel 以及使用iTextSharp.text.pdf.PdfReader.dll对PDF的操作

    string result = Regex.Match(str,@"[^\\]+$").Value;//正则表达式 this.listBox1.Items.Add(Path.Get ...

  7. HTML5简介及HTML5的发展前景

    WEB技术发展越来越迅速,HTML5的到来更是把WEB技术推向了巅峰,目前HTML5技术已经日趋成熟,不仅在PC段,HTML5更是在移动终端上也有广泛的应用,HTML5的未来十分光明,值得我们去学习. ...

  8. drop,truncate与delete的区别

    注意:这里说的delete是指不带where子句的delete语句 相同点 truncate和不带where子句的delete, 以及drop都会删除表内的数据 不同点: 1. truncate和 d ...

  9. Linux之sed,awk(流编辑器)

    sed:  s----substitute(替换) 1. 文本替换(使用-i选项,可以将结果应用于原文件) many people在进行替换之后,借助重定向来保存文件(未使用-i选项): $ sed  ...

  10. mac 安装PIL

    PIL是python Image library 在mac终端中通过以下命令安装: # download curl -O -L http://effbot.org/media/downloads/Im ...