最小生成树 prime poj1287

poj1287 裸最小生成树

AC代码

 1 #include "map"
 2 #include "queue"
 3 #include "math.h"
 4 #include "stdio.h"
 5 #include "string.h"
 6 #include "iostream"
 7 #include "algorithm"
 8 #define abs(x) x > 0 ? x : -x
 9 #define max(a,b) a > b ? a : b
10 #define min(a,b) a < b ? a : b
11
12 using namespace std;
13
14 int arc[55][55];
15
16 void Prime(int n){
17     int i,j,k,m;
18     int sumweight = 0;
19     int addnew[55];
20     int lowcost[55];
21     for(i=1; i<=n; i++){
22         addnew[i] = 0;
23         lowcost[i] = arc[1][i];
24     }
25     addnew[1] = 1;
26     for(i=1; i<n; i++){
27         k = 0,m = 999999;
28         for(j=2; j<=n; j++){
29             if(!addnew[j] && lowcost[j]<m){
30                 m = lowcost[j];
31                 k = j;
32             }
33         }
34         sumweight += lowcost[k];
35         addnew[k] = 1;
36         for(j=2; j<=n; j++){
37             if(!addnew[j] && arc[k][j]<lowcost[j])
38                 lowcost[j] = arc[k][j];
39         }
40     }
41     printf("%d\n",sumweight);
42 }
43
44 int main(){
45     int n,m,i,a,b,c;
46     while(scanf("%d",&n)&&n){
47         memset(arc,111,sizeof(arc));
48         scanf("%d",&m);
49         while(m--){
50             scanf("%d%d%d",&a,&b,&c);
51             if(arc[a][b]>c){
52                 arc[a][b] = c;
53                 arc[b][a] = c;
54             }
55         }
56         Prime(n);
57     }
58     return 0;
59 }
时间: 2024-10-14 14:32:28

最小生成树 prime poj1287的相关文章

最小生成树 prime poj1258

题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set&

poj1258 Agri-Net +hdu 1233 还是畅通工程 (最小生成树Prime算法)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43215   Accepted: 17683 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

hdu 1875 最小生成树 prime版

最小生成树prime版 大致的步骤 首先选取一个到集合最近的点 然后标记起在集合内部 然后更新最短距离 畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24846    Accepted Submission(s): 8035 Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不

POJ - 1258(最小生成树.prime)

题目链接: http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68462   Accepted: 28369 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet conn

最小生成树 prime kruskal

带权图分为有向和无向 无向图的最短路径又叫做最小生成树,有prime算法和kruskal算法: 有向图的最短路径算法,有dijkstra算法和floyd算法. 生成树的概念:联通图G的一个子图如果是一棵包含G的所有顶点的树,则该子图称为G的生成树 生成树是联通图的极小连通子图.所谓极小是指:若在树中任意增加一条边,则 将出现一个回路:若去掉一条边,将会使之编程非连通图.生成树各边的权 值总和称为生成素的权.权最小的生成树称为最小生成树,常用的算法有prime算法和kruskal算法. 最小生成树

hdu1875(最小生成树prime)

思路:一开始想用贪心来着,发现贪心有缺陷,然后就用了最小生成树来写,这里用了prime算法,首先,先建个图,两点之间的边的权值就是两个点的距离,然后直接prime模板 代码 #include<iostream> #include<algorithm> #include<cstring> #include<math.h> #include<cstdio> const int inf=0x7fffffff; using namespace std;

POJ - 2485(最小生成树.prime)

题目链接: http://poj.org/problem?id=2485 题目: Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36525   Accepted: 16329 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the

poj 2031 Building a Space Station【最小生成树prime】【模板题】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepted: 2855 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

最小生成树 prime+heap

改一个错误真不容易,刚开始没有加上used数组,没有判断每个顶点是否已经加入到数组当中,结果同一个顶点被pop不止一次. struct edge{int to,cost;}; typedef pair<int,int> P;//first集合X到i的最短距离,second点的编号 int ranchNumber,roadNumber;//顶点个数,道路个数 int mincost[10005];//每个顶点到集合x的最短距离 bool used[10005];//每个顶点是否已经加入到集合中