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在.非常 ...
随机推荐
- Java并发AtomicIntegerArray类
java.util.concurrent.atomic.AtomicIntegerArray类提供了可以以原子方式读取和写入的底层int数组的操作,还包含高级原子操作. AtomicIntegerAr ...
- python 字典(dictionary)一些方法
1.python 字典(Dictionary) keys() 函数以列表返回一个字典所有的键. keys()语法: dict.keys() 2.setdefault()方法 python字典setde ...
- nginx之域名重定向
一般网站默认的访问端口为80,当多个域名指向同一个服务器IP时,可以nginx进行重定向,分别指向不同的目的地址或其他主机. 在nginx目录下的conf/vhost子目录下建两个conf文件,hos ...
- html/js部分问题和小结
2015/9/8 1.js中不要试图去处理由[变量:变量]组成的map(不过可以通过DWR.addOptions添加),而是处理[bh:变量][xm:变量],然后通过data[i].bh,data[i ...
- ubuntu16.04安装jdk1.8(java1.8)
使用ppa方式安装 1.添加ppa $sudo add-apt-repository ppa:webupd8team/java $sudo apt-get update 2.安装oracle-java ...
- db2中将表中的某一字段设置为自增长
DB2可以使用generated always as identity 将某一个字段指定为自增长的字段: 这表示id自动是一个自增长的字段,它从1开始增加每次增加1.也可以通过generated 将字 ...
- LeetCode Array Easy 283. Move Zeroes
Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...
- 用react+redux+webpack搭建项目一些疑惑
--save-dev开发用 例如:webpack --save开发和发布用 例如:react
- linux---postgresql的用户角色权限
PostgreSQL是通过角色来管理数据库访问权限的,我们可以将一个角色看成是一个数据库用户,或者一组数据库用户.角色可以拥有数据库对象,如表.索引,也可以把这些对象上的权限赋予其它角色,以控制哪些用 ...
- 由hbase.client.scanner.caching参数引发的血案(转)
转自:http://blog.csdn.net/rzhzhz/article/details/7536285 环境描述 Hadoop 0.20.203.0Hbase 0.90.3Hive 0.80.1 ...