poj 1258 最小生成树 模板
- POJ 最小生成树模板
- Kruskal算法
- #include<iostream>
- #include<algorithm>
- #include<stdio.h>
- #include<string.h>
- #include<ctype.h>
- #include<stdlib.h>
- #include<limits.h>
- #include<math.h>
- #include<queue>
- #include<stack>
- #define max(a, b) a>b?a:b;
- #define min(a, b) a<b?a:b;
- using namespace std;
- const int N = ;
- typedef struct rode
- {
- int x, y, l;
- } RODE;
- RODE maps[N];
- int f[N];
- int cmp(const void *a, const void *b)
- {
- RODE *c, *d;
- c=(RODE *)a;
- d=(RODE *)b;
- return c -> l - d -> l;
- }
- void Kruskal(int k);
- int Find(int x);
- int main()
- {
- int n, i, j, k, a;
- while(scanf("%d", &n)!=EOF)
- {
- k=;
- for(i=; i<n; i++)
- {
- f[i]=i;
- }
- for(i=; i<n; i++)
- for(j=; j<n; j++)
- {
- scanf("%d", &a);
- maps[k].x=i;
- maps[k].y=j;
- maps[k].l=a;
- k++;
- }
- qsort(maps, k, sizeof(maps[]), cmp);
- Kruskal(k);
- }
- return ;
- }
- void Kruskal(int k)
- {
- int ans=;
- for(int i=; i<k; i++)
- {
- int ru=Find(maps[i].x);
- int rv=Find(maps[i].y);
- if(ru!=rv)
- {
- f[ru]=rv;
- ans+=maps[i].l;
- }
- }
- printf("%d\n", ans);
- }
- int Find(int x)
- {
- if(f[x]!=x)
- f[x]=Find(f[x]);
- return f[x];
- }
- 另一种 prim算法 和dijkstra算法很像
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- int Map[][];
- const int oo=0xfffffff;
- int visit[];
- int dist[];
- int prim(int s,int n)
- {
- int ans=;
- memset(visit,,sizeof(visit));
- for(int i=; i<=n; i++)
- dist[i]=Map[s][i];
- visit[s]=;
- for(int i=; i<n; i++)
- {
- int index;
- int Min=oo;
- for(int j=; j<=n; j++)
- {
- if(!visit[j]&&dist[j]<Min)
- {
- index=j;
- Min=dist[j];
- }
- }
- ans+=Min;
- visit[index]=;
- for(int j=; j<=n; j++)
- {
- if(!visit[j]&&dist[j]>Map[index][j])
- {
- dist[j]=Map[index][j];
- }
- }
- }
- return ans;
- }
- int main()
- {
- int n;
- while(scanf("%d",&n)!=EOF)
- {
- for(int i=; i<=n; i++)
- {
- for(int j=; j<=n; j++)
- {
- Map[i][j]=oo;
- }
- }
- for(int i=; i<=n; i++)
- {
- for(int j=; j<=n; j++)
- {
- scanf("%d",&Map[i][j]);
- }
- }
- printf("%d\n",prim(,n));
- }
- return ;
- }
poj 1258 最小生成树 模板的更多相关文章
- POJ 1258 最小生成树
23333333333 完全是道水题.因为是偶自己读懂自己做出来的..T_T.prim的模板题水过. DESCRIPTION:John竞选的时候许诺会给村子连网.现在给你任意两个村子之间的距离.让你求 ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258
#include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...
- POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...
- Poj 2187 凸包模板求解
Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- POJ-图论-最小生成树模板
POJ-图论-最小生成树模板 Kruskal算法 1.初始时所有结点属于孤立的集合. 2.按照边权递增顺序遍历所有的边,若遍历到的边两个顶点仍分属不同的集合(该边即为连通这两个集合的边中权值最小的那条 ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- POJ 1258 Agri-Net(最小生成树,模板题)
用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...
随机推荐
- BNU 34990 Justice String (hash+二分求LCP)
思路:枚举第一个字符串的位置,然后枚举最长公共前缀的长度,时间即会下降-- #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- IAR模板--怎样在IARproject中创建和使用模板
怎样在IARproject中创建和使用模板 1.编辑和使用模板的方式: 路径为:Edit -> Code Templates -> Edit Templates 例如以下图: water ...
- Unity动态字体在手机上出现字体丢失问题解决
在我们游戏的开发过程中,在部分手机上运行游戏的时候,出现了字体丢失的问题,出问题的手机似乎用的都是高通芯片. 使用的unity是4.2.0版本,ngui是3.4.9版本. 在unity的论坛及unit ...
- ifconfig 命令
许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...
- cobbler+koan
cobbler+koan自动重装客户机 koan是kickstart-over-a-network的缩写,它是cobbler的客户端帮助程序,koan允许你通过网络提供虚拟机,也允许你重装已经存在 ...
- Android Studio 2.3版本 Run项目不能自动启动APP的问题 (转)
参考: http://blog.csdn.net/lucasey/article/details/61071377 Android Studio 升级到2.3版本后 运行项目后,只是安装上了,而APP ...
- Spring Cloud 微服务四:熔断器Spring cloud hystrix
前言:在微服务架构中,一般都是进程间通信,有可能调用链都比较长,当有底层某服务出现问题时,比如宕机,会导致调用方的服务失败,这样就会发生一连串的反映,造成系统资源被阻塞,最终可能造成雪崩.在sprin ...
- Lumen开发:lumen源码解读之初始化(4)——服务提供(ServiceProviders)与路由(Routes)
版权声明:本文为博主原创文章,未经博主允许不得转载. 前面讲了singleton和Middleware,现在来继续讲ServiceProviders和Routes,还是看起始文件bootstrap/a ...
- android shape的用法总结
参考代码: <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corner ...
- ASIHTTP 框架,同步、 异步请求、 上传 、 下载
ASIHTTPRequest详解 ASIHTTPRequest 是一款极其强劲的 HTTP 访问开源项目.让简单的 API 完成复杂的功能,如:异步请求,队列请求,GZIP 压缩,缓存,断点续传,进度 ...