普里姆求解:

#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(最小生成树-克鲁斯卡尔和普里姆求解)的更多相关文章

  1. 最小生成数 克鲁斯卡尔 普里姆 matlab

    克鲁斯卡尔: function T=MST_Kruskal(G) n=0; if isfield(G,'w') && ~isempty(G.w) && size(G.w ...

  2. hdu 1301 Jungle Roads 最小生成树

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

  3. 洛谷P3366【模板】最小生成树-克鲁斯卡尔Kruskal算法详解附赠习题

    链接 题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M&l ...

  4. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  5. 图->连通性->最小生成树(克鲁斯卡尔算法)

    文字描述 上一篇博客介绍了最小生成树(普里姆算法),知道了普里姆算法求最小生成树的时间复杂度为n^2, 就是说复杂度与顶点数无关,而与弧的数量没有关系: 而用克鲁斯卡尔(Kruskal)算法求最小生成 ...

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

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

  7. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  8. 贪心算法(Greedy Algorithm)之最小生成树 克鲁斯卡尔算法(Kruskal&#39;s algorithm)

    克鲁斯卡尔算法(Kruskal's algorithm)是两个经典的最小生成树算法的较为简单理解的一个.这里面充分体现了贪心算法的精髓.大致的流程能够用一个图来表示.这里的图的选择借用了Wikiped ...

  9. 贪心算法(Greedy Algorithm)最小生成树 克鲁斯卡尔算法(Kruskal&#39;s algorithm)

    克鲁斯卡尔算法(Kruskal's algorithm)它既是古典最低的一个简单的了解生成树算法. 这充分反映了这一点贪心算法的精髓.该方法可以通常的图被表示.图选择这里借用Wikipedia在.非常 ...

随机推荐

  1. spring cloud学习--eureka 02

    开启eureka client的注解@EnableDiscoveryClient的功能类DiscoveryClient梳理图 获取server url位于类EndpointUtils的getServi ...

  2. Redis项目实战,一些经验总结

    来源:https://my.oschina.net/u/920698/blog/3031587 背景 Redis 是一个开源的内存数据结构存储系统. 可以作为数据库.缓存和消息中间件使用. 支持多种类 ...

  3. 插件化框架解读之Class文件与Dex文件的结构(一)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 Class文件 Class文件是Java虚拟机定义并被其所识别的 ...

  4. PHP多选测试练习

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. tf.keras 解决plot_model 的配置问题

    https://blog.csdn.net/ha010/article/details/103367311

  6. kvm学习笔记(一,基础概念)

    前言 阿里云的云主机,采用的kvm,今天就花了点时间研究了下. 一.安装 官方文档参考:http://www.linux-kvm.org/page/HOWTO 二.快速建立一个基于vnc的虚拟机安装 ...

  7. pycharm查找替换快捷键

    查找:CTRL + F 替换:CTRL + R 如果想删除,替换那一栏不填就可以了

  8. Tensorflow中的图(tf.Graph)和会话(tf.Session)详解

    Tensorflow中的图(tf.Graph)和会话(tf.Session) Tensorflow编程系统 Tensorflow工具或者说深度学习本身就是一个连贯紧密的系统.一般的系统是一个自治独立的 ...

  9. NFA 、DFA 简述

    转载请注明出处 https://www.cnblogs.com/majianming/p/11823697.html 目前常见的正则表达引擎总体分为2种,DFA (确定型有穷状态自动机) 和 NFA ...

  10. Delphi 格式化函数 Format函数

    function Format(const Format: string; const Args: array of const): string; function Format(const For ...