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. SpringBoot对比SpringMVC,SpringMVC 处理请求过程

    (问较多:1.SpringBoot对比SpringMVC.2.SpringMVC 处理请求过程.问:springboot的理解 Spring,Spring MVC,Spring Boot 三者比较 S ...

  2. bzoj千题计划307:bzoj5248: [2018多省省队联测]一双木棋

    https://www.lydsy.com/JudgeOnline/problem.php?id=5248 先手希望先手得分减后手得分最大,后手希望先手得分减后手得分最小 棋盘的局面一定是阶梯状,且从 ...

  3. 服务器上的XML

    若想让浏览器能访问Web项目,需要配置服务器里的XML文件,XML文件是类似于HtML文件的纯文本文件,可以通过Web服务器轻松的存储和生成. XML可以通过ASP,PHP,数据库生成XML

  4. VS2015(Xamarin)开发安卓WebApp笔记

    有关WebApp的开发,大多数人都用了第三方框架,如Cordova等.我这里没有用到这类框架,而是新建了一个WebView嵌入Assets(本地资源)来完成这个App,由于第一个练习App希望对初学者 ...

  5. pytorch中如何使用DataLoader对数据集进行批处理

    最近搞了搞minist手写数据集的神经网络搭建,一个数据集里面很多个数据,不能一次喂入,所以需要分成一小块一小块喂入搭建好的网络. pytorch中有很方便的dataloader函数来方便我们进行批处 ...

  6. 五校联考 running (欧拉函数)

    题面 \(solution:\) 讲真吧,这道题真的出得,嗯,太恐怖了.考场上这道题真的把我看懵了,这道题以前是见过的,但欧拉函数?我学过吗?一道容斥都要超时的题目,我都要为我自己点根香了,拿着gcd ...

  7. 交换机console口连接

    http://www.webkaka.com/info/archives/knowledge/2010/08/30452/

  8. B. Array

    题目链接:http://codeforces.com/contest/224/problem/B 具体大意: 输入n,m. 给你一个区间,让你找某一段区间中包含m个不同的数,并且这段区间中的某一个小区 ...

  9. swift计算 switch case

    var year = var month = var day = ; let daysOfFeb = year % == && year% != || year % == ?: var ...

  10. Dom4j向XML中增加节点与属性——(四)

    先获取到节点,然后在节点山添加Element 添加节点 添加属性 设置开始标签与结束标签的值book.addElement("描述").addAttribute("nam ...