poj 1251 最小生成树模板题

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算法

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8
 9 const int INF=0x3f3f3f3f;
10 const int MAXN=110;
11 bool vis[MAXN];
12 int lowc[MAXN];
13 int n ;
14 int cost[MAXN][MAXN] ;
15
16 int Prim()//点是0~n-1
17 {
18     int ans=0;
19     memset(vis,false,sizeof(vis));
20     vis[0]=true;
21     for(int i=1;i<n;i++)lowc[i]=cost[0][i];
22     for(int i=1;i<n;i++)
23     {
24         int minc=INF;
25         int p=-1;
26         for(int j=0;j<n;j++)
27             if(!vis[j]&&minc>lowc[j])
28             {
29                 minc=lowc[j];
30                 p=j;
31             }
32             if(minc==INF)return -1;//原图不连通
33             ans+=minc;
34             vis[p]=true;
35             for(int j=0;j<n;j++)
36                 if(!vis[j]&&lowc[j]>cost[p][j])
37                     lowc[j]=cost[p][j];
38     }
39     return ans;
40 }
41
42 int main()
43 {
44
45    // freopen("in.txt","r",stdin) ;
46     while(cin>>n)
47     {
48         if (n == 0)
49             break ;
50         char u , v;
51         int w , num ;
52         int i , j ;
53         for (i = 0 ; i < n ; i++)
54             for (j = 0 ; j < n ; j++)
55                cost[i][j] = INF ;
56
57         for (i = 1 ; i < n ; i++)
58         {
59             cin>>u>>num ;
60             while (num--)
61             {
62                 cin>>v>>w ;
63                 cost[u -‘A‘][v - ‘A‘] = w ;
64                 cost[v - ‘A‘][u -‘A‘] = w ;
65             }
66         }
67         cout<<Prim()<<endl ;
68
69     }
70     return 0 ;
71 }

Kruskal算法

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8
 9 int n ;
10 const int MAXN=110;//最大点数
11 const int MAXM=10000;//最大边数
12 int F[MAXN];//并查集使用
13 struct Edge
14 {
15     int u,v,w;
16 }edge[MAXM];//存储边的信息,包括起点/终点/权值
17
18 int tol;//边数,加边前赋值为0
19 void addedge(int u,int v,int w)
20 {
21
22     edge[tol].u=u;
23     edge[tol].v=v;
24     edge[tol++].w=w;
25 }
26 bool cmp(Edge a,Edge b)
27 {//排序函数,讲边按照权值从小到大排序
28     return a.w<b.w;
29 }
30 int find(int x)
31 {
32     if(F[x]==-1)return x;
33     else return F[x]=find(F[x]);
34 }
35 int Kruskal()//传入点数,返回最小生成树的权值,如果不连通返回-1
36 {
37     memset(F,-1,sizeof(F));
38     sort(edge,edge+tol,cmp);
39     int cnt=0;//计算加入的边数
40     int ans=0;
41     for(int i=0;i<tol;i++)
42     {
43         int u=edge[i].u;
44         int v=edge[i].v;
45         int w=edge[i].w;
46         int t1=find(u);
47         int t2=find(v);
48         if(t1!=t2)
49         {
50             ans+=w;
51             F[t1]=t2;
52             cnt++;
53         }
54         if(cnt==n-1)break;
55     }
56     if(cnt<n-1)return -1;//不连通
57     else return ans;
58 }
59
60 int main()
61 {
62
63    // freopen("in.txt","r",stdin) ;
64     while(cin>>n)
65     {
66         if (n == 0)
67             break ;
68         char u , v;
69         int w , num ;
70         int i , j ;
71         tol = 0 ;
72         for (i = 1 ; i < n ; i++)
73         {
74             cin>>u>>num ;
75             while (num--)
76             {
77                 cin>>v>>w ;
78                 addedge(u,v,w) ;
79             }
80         }
81         cout<<Kruskal()<<endl ;
82
83     }
84     return 0 ;
85 }

时间: 06-12

poj 1251 最小生成树模板题的相关文章

poj 1251(最小生成树)

Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensi

POJ 1113 凸包模板题

上模板. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <utility> #include <stack> #include <queue> #include <map> #include

Agri-Net POJ 1258(最小生成树模板)

原题 题目链接 题目分析 比较明显的最小生成树模板题,题目给的输入是邻接矩阵,处理一下用prim算法就可以算出最小生成树了. 代码 1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio> 5 #include <cmath> 6 #include <cstring> 7 #include <string> 8

还是畅通工程——最小生成树模板题

题目链接 题意: 给定n个村庄,m=(n*(n-1)/2)条关系  u,v,w 表示  u到 v之间的距离是 w 题解: 裸最小生成树模板题 代码: #include<iostream> #include<stdio.h> #include<math.h> #include<algorithm> #include<vector> using namespace std; typedef long long ll; const int maxn =

POJ2560 (最小生成树模板题)

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through. Consider Dick's back to

(heu step 6.1.1)Constructing Roads(最小生成树模板题:求让n个点连通的最小费用)

题目: Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 207 Accepted Submission(s): 135   Problem Description There are N villages, which are numbered from 1 to N, and you should bu

POJ Oulipo KMP 模板题

http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41051   Accepted: 16547 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a mem

POJ Oulipo(KMP模板题)

题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack&

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

POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出发,每次走这个点没走过的最小边权值,这样不断找下去就可以找出,本质就是贪心算法 而kruskal是利用并查集,先按照边权值大小排序,然后从小的边开始往里面添加边,利用并查集判断是否在一个联通分量里面(就是是否相连)如果不相 连就建立边,从而建图,注意,节点编号如果是从1->n,那么相应初始化就应该从