hduoj-1301 Jungle Roads(最小生成树-克鲁斯卡尔和普里姆求解)
普里姆求解:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;
const int MAX=;
const int INF=;
int n;//村庄数
int G[MAX][MAX],d[MAX];
bool isVisit[MAX]={false};
int prim(){
fill(d,d+MAX,INF);
d[]=;
int ans=;
for(int i=;i<n;i++){
int u=-,MIN=INF;
for(int j=;j<n;j++){
if(isVisit[j]==false&&d[j]<MIN){
u=j;
MIN=d[j];
}
}
if(u==-) return -;
isVisit[u]=true;
ans+=d[u];
for(int v=;v<n;v++){
if(isVisit[v]==false&&G[u][v]!=INF&&d[v]>G[u][v]){
d[v]=G[u][v];
}
}
}
return ans;
}
int main(){
while(scanf("%d",&n)&&n!=){
getchar();
fill(G[],G[]+MAX*MAX,INF);
int temp1,temp2;
char c1[],c2[];
for(int i=;i<n-;i++){
scanf("%s %d",&c1,&temp1);
for(int j=;j<temp1;j++){
scanf("%s %d",&c2,&temp2);
G[c1[]-'A'][c2[]-'A']=G[c2[]-'A'][c1[]-'A']=temp2;
}
}
int ans=prim();
printf("%d\n",ans);
} return ;
}
-克鲁斯卡尔:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;
const int MAX=;
const int MAXE=;
const int INF=;
int d[MAX],father[MAX];
int n,m=;
bool isVisit[MAX]={false};
struct edge{
int node1,node2;
int weight;
}E[MAXE];
bool cmp(edge e1,edge e2){
return e1.weight<e2.weight;
}
int findFather(int x){
int a=x;
while(x!=father[x]){
x=father[x];
}
while(a!=father[a]){
int temp=a;
a=father[a];
father[temp]=x;
}
return x;
}
int kruskal(){
int ans=,num_edge=;
for(int i=;i<n;i++)
father[i]=i;
sort(E,E+m,cmp);
for(int i=;i<m;i++){
int f1=findFather(E[i].node1);
int f2=findFather(E[i].node2);
if(f1!=f2){
father[f1]=f2;
ans+=E[i].weight;
num_edge++;
//边数等于顶点数-1结束
if(num_edge==n-) break;
}
}
if(num_edge!=n-) return -;
else return ans;
} int main(){
while(scanf("%d",&n)&&n!=){
getchar();
//fill(G[0],G[0]+MAX*MAX,INF);
char c1[],c2[];
int temp1,temp2;
for(int i=;i<n-;i++){
scanf("%s %d",&c1,&temp1);
for(int j=;j<temp1;j++){
scanf("%s %d",&c2,&temp2);
E[m].node1=c1[]-'A';
E[m].node2=c2[]-'A';
E[m].weight=temp2;
m++;
}
}
int ans=kruskal();
printf("%d\n",ans); } return ;
}
hduoj-1301 Jungle Roads(最小生成树-克鲁斯卡尔和普里姆求解)的更多相关文章
- 最小生成数 克鲁斯卡尔 普里姆 matlab
克鲁斯卡尔: function T=MST_Kruskal(G) n=0; if isfield(G,'w') && ~isempty(G.w) && size(G.w ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- 洛谷P3366【模板】最小生成树-克鲁斯卡尔Kruskal算法详解附赠习题
链接 题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M&l ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- 图->连通性->最小生成树(克鲁斯卡尔算法)
文字描述 上一篇博客介绍了最小生成树(普里姆算法),知道了普里姆算法求最小生成树的时间复杂度为n^2, 就是说复杂度与顶点数无关,而与弧的数量没有关系: 而用克鲁斯卡尔(Kruskal)算法求最小生成 ...
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- 贪心算法(Greedy Algorithm)之最小生成树 克鲁斯卡尔算法(Kruskal's algorithm)
克鲁斯卡尔算法(Kruskal's algorithm)是两个经典的最小生成树算法的较为简单理解的一个.这里面充分体现了贪心算法的精髓.大致的流程能够用一个图来表示.这里的图的选择借用了Wikiped ...
- 贪心算法(Greedy Algorithm)最小生成树 克鲁斯卡尔算法(Kruskal's algorithm)
克鲁斯卡尔算法(Kruskal's algorithm)它既是古典最低的一个简单的了解生成树算法. 这充分反映了这一点贪心算法的精髓.该方法可以通常的图被表示.图选择这里借用Wikipedia在.非常 ...
随机推荐
- windows 安装多个mysql
安装多个mysql,其实很简单,网上资料也很多,我整理一下,也跟着来凑个热闹. 1.下载mysql zip 解压到指定目录,我这边就3个,更多也类似 d:\mysql1 d:\mysql2 d:\my ...
- 【数据驱动】python之mysql的操作
1.准备工作 在本篇中,我们使用python版本为python3.7.在python3中,连接mysql数据库我们需要使用pymysql这个第三方库.我们可以直接使用pip命令来安装,安装的命令为: ...
- [fw]error: aggregate value used where an integer was expected
一個自訂struct型態的變數,若想要轉換為unsigned,直接使用cast,gcc(version 4.4.3)編譯會回報錯誤. 例如: struct _test { unsigned hour ...
- [Java 教程 02] 开发环境搭建
在上一篇文章对Java做了一个简单介绍之后,我想大家都已经对她有一个初步的认识了吧!那踏入正式学习使用Java之前,我们有一步是不得不做的,它是什么呢?没有错,就是我们本篇文章的标题所说,搭建Java ...
- sql查询静态数据
select * from ( ,,,'高中')) AS Education ( EducationId,EducationName )
- Codeforces 356D 倍增优化背包
题目链接:http://codeforces.com/contest/356/problem/D 思路(官方题解):http://codeforces.com/blog/entry/9210 此题需要 ...
- 带你逐行阅读redux源码
带你逐行阅读redux源码 redux版本:2019-7-17最新版:v4.0.4 git 地址:https://github.com/reduxjs/redux/tree/v4.0.4 redux目 ...
- CodeForces 隐藏标签
设置css 记录. 以防忘记.
- C#语言是专门为.NET提供的开发语言(visual studio 2008序列号)
.net是微软的一个平台在这个平台上的语言有C#.C++.VB等,其中C#是专门为.NET平台开发的语言,语法简洁美观.建议安装visual studio 2008,你可以去微软官方下载90天试用版, ...
- 【Python CheckiO 题解】SP
题目描述 [Speech Module]:输入一个数字,将其转换成英文表达形式,字符串中的所有单词必须以一个空格字符分隔. [输入]:一个数字(int) [输出]:代表数字的英文字符串(str) [前 ...