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在.非常 ...
随机推荐
- 为什么总是弹出报错“百度未授权使用地图API”?
今天打开网站的时候出现了这个问题“百度未授权使用地图API, 可能是因为您提供的密钥不是有效的百度开放平台密钥或此密钥未对本应用的百度地图JavasoriptAPI授权.…”经过研究终于知道什么原因了 ...
- Apache Shiro 集成Spring(二)
1.依赖: <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-cor ...
- DNS解析全过程详解
1.Chrome浏览器 会首先搜索浏览器自身的DNS缓存(缓存时间比较短,大概只有1分钟,且只能容纳1000条缓存),看自身的缓存中是否有www.linux178.com 对应的条目,而且没有过期,如 ...
- JAVA编程思想(1) - 一切都是对象
-"假设我们说还有一种不用的语言,那么我们就会发觉一个有些不同的世界" 1. 用引用操纵对象 每种编程语言都有自己的数据处理方式. 有些时候,程序猿必须时刻留意准备 ...
- C#虚方法和抽象方法的区别
一,如下例子 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespa ...
- Python3学习笔记——类
#!/usr/bin/env python #-*- coding:utf-8 -*- #面向对象(类+对象) 三大特性:封装.继承.多态 类的成员: 字段: 普通字段:保存在对象(实例)中,执行只能 ...
- ORM--SqlSugar
这个是很久之前就开始用的一款ORM,挺好用的,推荐~ 关键词: SqlSugar:一款小巧,并且功能齐全的ORM 参考手册网址:http://www.codeisbug.com/Home/Doc 多表 ...
- 【转】 linux下配置squid 服务器,最简单使用方式
linux下配置squid 1.什么是squid Squid cache(简称为Squid)是一个流行的自由软件(GNU通用公共许可证)的代理服务器和Web缓存服务器.Squid有广泛的用途,从作为网 ...
- 十万级百万级数据量的Excel文件导入并写入数据库
一.需求分析 最近接到一个需求,导入十万级,甚至可能百万数据量的记录了车辆黑名单的Excel文件,借此机会分析下编码过程; 首先将这个需求拆解,发现有三个比较复杂的问题: 问题一:Excel文件导入后 ...
- Java数组遍历
1.数组声明格式: 数据类型 [] 数组名 = new 数据类型[长度]: 数组长度一旦确定无法更改. 数组里的数据必须是相同类型或自动向上转型后兼容的类型 2.数组遍历 //一维数组 String ...