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在.非常 ...
随机推荐
- redux请求数据流程
redux请求数据流程 store里面的index.js文件 import {createStore,combineReducers,applyMiddleware} from "redux ...
- Linux学习笔记0-CentOS7关闭防火墙
关闭防火墙 systemctl stop firewalld.service //停止firewall systemctl disable firewalld.service //禁止firewall ...
- CentOS 6.5安装aria2(转载)
CentOS 6.5安装aria2 由于yum install aria2无法找到安装包,试了好几个源,都找不到,于是自己找了一些地址: 1.下载安装包: # wget http://ftp.tu-c ...
- #Ubuntu16.0.4 LTS 安装RabbitMQ
1.ubuntu下安装配置rabbitmq-server服务器环境:ubuntu16.0.4(向下兼容14.0.4)软件版本:RabbitMQ 3.7.5 .Erlang 20.1.7参考文档:htt ...
- Docker基础(下)
Docker基础(下) 链接:https://pan.baidu.com/s/1u8Tg5qB4ZZHEK6GqCJkjwg 提取码:u8hb 复制这段内容后打开百度网盘手机App,操作更方便哦 5. ...
- Electron-vue实战(一)—搭建项目与安装Element UI
Electron-vue实战—搭建项目与安装Element UI 作者:狐狸家的鱼 本文链接 GitHub:sueRimn 一.新建项目1.初始化项目打开cmd,新建一个项目,我使用的是electro ...
- oracle时间问题 与case then
思路一 --select * from nls_session_parameters where parameter='NLS_DATE_LANGUAGE'; -- 修改会话的默认日期格式 ALTER ...
- MySQL 8.0.12安装教程 (windows 64位)
先去官网下载点击的MySQL的下载 下载完成后解压 解压完是这个样子,(解压后并没有Data目录,要手动创建,Data目录是自己创建的设置mysql数据库的数据的存放目录,解压后的目录也没有的my. ...
- cocos2D-X not config ndk path
{ 双击击那个error,那个路径就加上了 File = >local.properties }
- js 中数组传递到后台controller 批量删除
/*批量删除*/function datadel(url) { var ids=[]; $("input[type='checkbox']:checked").each(funct ...