poj 1251  && hdu 1301

Sample Input

9 //n 结点数
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output

216
30

prim算法

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=;
bool vis[MAXN];
int lowc[MAXN];
int n ;
int cost[MAXN][MAXN] ; int Prim()//点是0~n-1
{
int ans=;
memset(vis,false,sizeof(vis));
vis[]=true;
for(int i=;i<n;i++)lowc[i]=cost[][i];
for(int i=;i<n;i++)
{
int minc=INF;
int p=-;
for(int j=;j<n;j++)
if(!vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
if(minc==INF)return -;//原图不连通
ans+=minc;
vis[p]=true;
for(int j=;j<n;j++)
if(!vis[j]&&lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
return ans;
} int main()
{ // freopen("in.txt","r",stdin) ;
while(cin>>n)
{
if (n == )
break ;
char u , v;
int w , num ;
int i , j ;
for (i = ; i < n ; i++)
for (j = ; j < n ; j++)
cost[i][j] = INF ; for (i = ; i < n ; i++)
{
cin>>u>>num ;
while (num--)
{
cin>>v>>w ;
cost[u -'A'][v - 'A'] = w ;
cost[v - 'A'][u -'A'] = w ;
}
}
cout<<Prim()<<endl ; }
return ;
}

Kruskal算法

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; int n ;
const int MAXN=;//最大点数
const int MAXM=;//最大边数
int F[MAXN];//并查集使用
struct Edge
{
int u,v,w;
}edge[MAXM];//存储边的信息,包括起点/终点/权值 int tol;//边数,加边前赋值为0
void addedge(int u,int v,int w)
{ edge[tol].u=u;
edge[tol].v=v;
edge[tol++].w=w;
}
bool cmp(Edge a,Edge b)
{//排序函数,讲边按照权值从小到大排序
return a.w<b.w;
}
int find(int x)
{
if(F[x]==-)return x;
else return F[x]=find(F[x]);
}
int Kruskal()//传入点数,返回最小生成树的权值,如果不连通返回-1
{
memset(F,-,sizeof(F));
sort(edge,edge+tol,cmp);
int cnt=;//计算加入的边数
int ans=;
for(int i=;i<tol;i++)
{
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int t1=find(u);
int t2=find(v);
if(t1!=t2)
{
ans+=w;
F[t1]=t2;
cnt++;
}
if(cnt==n-)break;
}
if(cnt<n-)return -;//不连通
else return ans;
} int main()
{ // freopen("in.txt","r",stdin) ;
while(cin>>n)
{
if (n == )
break ;
char u , v;
int w , num ;
int i , j ;
tol = ;
for (i = ; i < n ; i++)
{
cin>>u>>num ;
while (num--)
{
cin>>v>>w ;
addedge(u,v,w) ;
}
}
cout<<Kruskal()<<endl ; }
return ;
}

poj 1258

Sample Input

4 //n
0 4 9 21 //邻接矩阵
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output

28

prim

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=;
bool vis[MAXN];
int lowc[MAXN];
int n ;
int cost[MAXN][MAXN] ; int Prim()//点是0~n-1
{
int ans=;
memset(vis,false,sizeof(vis));
vis[]=true;
for(int i=;i<n;i++)lowc[i]=cost[][i];
for(int i=;i<n;i++)
{
int minc=INF;
int p=-;
for(int j=;j<n;j++)
if(!vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
if(minc==INF)return -;//原图不连通
ans+=minc;
vis[p]=true;
for(int j=;j<n;j++)
if(!vis[j]&&lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
return ans;
} int main()
{ //freopen("in.txt","r",stdin) ;
while(cin>>n)
{
int w ;
int i , j ;
for (i = ; i < n ; i++)
for (j = ; j < n ; j++)
{
cin>>w ;
if(w==)
cost[i][j] = INF ;
else
cost[i][j] = w ;
}
cout<<Prim()<<endl ; }
return ;
}

hdu 1863

Sample Input
3 3 //边数 结点数
1 2 1 //一条边两边结点的id 边的权值
1 3 2
2 3 4
1 3
2 3 2
0 100

Sample Output
3
? //不连通就输出这个

Kruskal

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; int n ;
const int MAXN=;//最大点数
const int MAXM=;//最大边数
int F[MAXN];//并查集使用
struct Edge
{
int u,v,w;
}edge[MAXM];//存储边的信息,包括起点/终点/权值 int tol;//边数,加边前赋值为0
void addedge(int u,int v,int w)
{ edge[tol].u=u;
edge[tol].v=v;
edge[tol++].w=w;
}
bool cmp(Edge a,Edge b)
{//排序函数,讲边按照权值从小到大排序
return a.w<b.w;
}
int find(int x)
{
if(F[x]==-)return x;
else return F[x]=find(F[x]);
}
int Kruskal()//传入点数,返回最小生成树的权值,如果不连通返回-1
{
memset(F,-,sizeof(F));
sort(edge,edge+tol,cmp);
int cnt=;//计算加入的边数
int ans=;
for(int i=;i<tol;i++)
{
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int t1=find(u);
int t2=find(v);
if(t1!=t2)
{
ans+=w;
F[t1]=t2;
cnt++;
}
if(cnt==n-)break;
}
if(cnt<n-)return -;//不连通
else return ans;
} int main()
{ // freopen("in.txt","r",stdin) ;
int m ;
while(scanf("%d %d" , &m , &n) != EOF)
{
if (m == )
break ;
int i ;
int u , v , w ;
tol = ;
while(m--)
{
scanf("%d %d %d" , &u , &v , &w) ;
addedge(u , v , w) ;
}
int k = Kruskal() ;
if (k == -)
printf("?\n") ;
else
printf("%d\n" , k) ; }
return ;
}

poj 1287

Sample Input
1 0

2 3 //结点 边
1 2 37//u v w
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12

0

Sample Output

0
17
16
26

Kruskal

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; int n ;
const int MAXN=;//最大点数
const int MAXM=;//最大边数
int F[MAXN];//并查集使用
struct Edge
{
int u,v,w;
}edge[MAXM];//存储边的信息,包括起点/终点/权值 int tol;//边数,加边前赋值为0
void addedge(int u,int v,int w)
{ edge[tol].u=u;
edge[tol].v=v;
edge[tol++].w=w;
}
bool cmp(Edge a,Edge b)
{//排序函数,讲边按照权值从小到大排序
return a.w<b.w;
}
int find(int x)
{
if(F[x]==-)return x;
else return F[x]=find(F[x]);
}
int Kruskal()//传入点数,返回最小生成树的权值,如果不连通返回-1
{
memset(F,-,sizeof(F));
sort(edge,edge+tol,cmp);
int cnt=;//计算加入的边数
int ans=;
for(int i=;i<tol;i++)
{
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int t1=find(u);
int t2=find(v);
if(t1!=t2)
{
ans+=w;
F[t1]=t2;
cnt++;
}
if(cnt==n-)break;
}
if(cnt<n-)return -;//不连通
else return ans;
} int main()
{ //freopen("in.txt","r",stdin) ;
int m ;
while(scanf("%d %d" , &n , &m) != EOF)
{
if (n == )
break ;
int i ;
int u , v , w ;
tol = ;
if (n == && m == )
{
printf("0\n") ;
continue ;
}
while(m--)
{
scanf("%d %d %d" , &u , &v , &w) ;
addedge(u , v , w) ;
}
printf("%d\n" , Kruskal()) ; }
return ;
}

poj 2421

有的路已建 建好了的路权值设为0

Sample Input

3 // n
0 990 692 //邻接矩阵
990 0 179
692 179 0
1 //m
1 2 // u v
Sample Output

179

prim

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=;
bool vis[MAXN];
int lowc[MAXN];
int n ;
int cost[MAXN][MAXN] ; int Prim()//点是0~n-1
{
int ans=;
memset(vis,false,sizeof(vis));
vis[]=true;
for(int i=;i<n;i++)lowc[i]=cost[][i];
for(int i=;i<n;i++)
{
int minc=INF;
int p=-;
for(int j=;j<n;j++)
if(!vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
if(minc==INF)return -;//原图不连通
ans+=minc;
vis[p]=true;
for(int j=;j<n;j++)
if(!vis[j]&&lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
return ans;
} int main()
{ // freopen("in.txt","r",stdin) ;
while(cin>>n)
{
int w ;
int i , j ;
for (i = ; i < n ; i++)
for (j = ; j < n ; j++)
{
cin>>w ;
if(w==)
cost[i][j] = INF ;
else
cost[i][j] = w ;
}
int m ;
cin>>m ;
while(m--)
{
int x , y ;
cin>>x>>y ;
cost[x-][y-] = ;
cost[y-][x-] = ;
}
cout<<Prim()<<endl ; }
return ;
}

hdu 1233

n*(n-1)/2条边
Sample Input
3 //n
1 2 1 //u v w
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0

Sample Output
3
5

Kruskal

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; int n ;
const int MAXN=;//最大点数
const int MAXM=;//最大边数
int F[MAXN];//并查集使用
struct Edge
{
int u,v,w;
}edge[MAXM];//存储边的信息,包括起点/终点/权值 int tol;//边数,加边前赋值为0
void addedge(int u,int v,int w)
{ edge[tol].u=u;
edge[tol].v=v;
edge[tol++].w=w;
}
bool cmp(Edge a,Edge b)
{//排序函数,讲边按照权值从小到大排序
return a.w<b.w;
}
int find(int x)
{
if(F[x]==-)return x;
else return F[x]=find(F[x]);
}
int Kruskal()//传入点数,返回最小生成树的权值,如果不连通返回-1
{
memset(F,-,sizeof(F));
sort(edge,edge+tol,cmp);
int cnt=;//计算加入的边数
int ans=;
for(int i=;i<tol;i++)
{
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int t1=find(u);
int t2=find(v);
if(t1!=t2)
{
ans+=w;
F[t1]=t2;
cnt++;
}
if(cnt==n-)break;
}
if(cnt<n-)return -;//不连通
else return ans;
} int main()
{ // freopen("in.txt","r",stdin) ;
while(scanf("%d" , &n) != EOF)
{
if (n == )
break ;
int i ;
int u , v , w ;
tol = ; for (i = ; i <= (n-)*n/ ; i++)
{
scanf("%d %d %d" , &u , &v , &w) ;
addedge(u , v , w) ;
}
printf("%d\n" , Kruskal()) ; }
return ;
}

poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题的更多相关文章

  1. HDU 1233 最小生成树模板题,练练模板

    还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  2. POJ 1287 Networking (最小生成树模板题)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

  3. POJ 1258:Agri-Net Prim最小生成树模板题

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45050   Accepted: 18479 Descri ...

  4. 最小生成树模板题POJ - 1287-prim+kruskal

    POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出 ...

  5. POJ 1789 Truck History【最小生成树模板题Kruscal】

    题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...

  6. POJ 2289 Jamie's Contact Groups 【二分】+【多重匹配】(模板题)

    <题目链接> 题目大意: 有n个人,每个人都有一个或者几个能够归属的分类,将这些人分类到他们能够归属的分类中后,使所含人数最多的分类值最小,求出该分类的所含人数值. 解题分析: 看到求最大 ...

  7. POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题

    A rooted tree is a well-known data structure in computer science and engineering. An example is show ...

  8. POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  9. HDU 1863 畅通工程(Prim,Kruskal,邻接表模板)

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. Java Calendar使用总结

    JavaCalendar 类时间操作,这也许是创建和管理日历最简单的一个方案,示范代码很简单. 演示了获取时间,日期时间的累加和累减,以及比较. 原文地址:blog.csdn.NET/joyous/a ...

  2. HDU - 1828 Picture

    题目链接 题意  多个矩形重叠在一起,求出轮廓线的长度. 分析  把矩形分成横线和竖线来处理.现在分析横线的,竖线同理.矩形的坐标都是整数,且范围不大,故此题不需要离散化.从下往上扫描横线,每遇到一条 ...

  3. HostAliases向Pod中添加hosts解析

    前言 根据公司同一个项目需要不同的de/te/pe环境,由于相应环境调用的数据库等配置信息存在不同等因素,需要向Kubernetes集群中的Pod添加对应的hosts解析. 解决 以下以yaml文件自 ...

  4. python学习笔记8--面向对象编程

    一.面向对象编程 面向对象--Object Oriented Programming,简称oop,是一种程序设计思想.在说面向对象之前,先说一下什么是编程范式,编程范式你按照什么方式来去编程,去实现一 ...

  5. writen.c

    #include <unistd.h> #include <errno.h> ssize_t writen(int fd, const void *vptr, size_t n ...

  6. SNMP收集

    http://velep.com/archives/416.html     协议基本格式

  7. log4j2 的使用

    log4j2 是 log4j 的升级,更为方便,更为强大. log4j2.xml 的配置以及 log4j2的依赖包使用log4j2 并没有其他的依赖包,只是在使用log4j的情况下,需要别的进行桥接 ...

  8. 第16月底18天 phpstudy设置

    1.phpstudy设置-端口常规设置 E:\phpStudy\Apache\bin>httpd.exeAH00526: Syntax error on line 14 of E:/phpStu ...

  9. 2018-2019-2 网络对抗技术 20165227 Exp3 免杀原理与实践

    2018-2019-2 网络对抗技术 20165227 Exp3 免杀原理与实践 **免杀** - 一般是对恶意软件做处理,让它不被杀毒软件所检测.也是渗透测试中需要使用到的技术. - 要做好免杀,就 ...

  10. struts2在配置文件与JSP中用OGNL获取Action属性

    参考:Struts与OGNL结合 struts2在配置文件中可以调用Action的属性,在JSP页面也可以取出Action的属性值(前提是属性有get,set方法). 第一个例子: 1.Action中 ...