题意:给一个无向图的邻接矩阵,求最小生成树。

解法:Kruskal算法。把边按边权排序,从小到大插入生成树中,如果一个边的两个点都在生成树中则不插入,用并查集维护。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
struct node
{
int u, v;
int num;
bool operator < (const node & tmp) const
{
return num < tmp.num;
}
}edge[10005];
int father[105];
void init()
{
for(int i = 0; i < 105; i++)
father[i] = i;
}
int Find(int x)
{
return father[x] == x ? father[x] : father[x] = Find(father[x]);
}
bool Union(int a, int b)
{
int c = Find(a), d = Find(b);
if(c == d)
return false;
father[c] = d;
return true;
}
int main()
{
int n;
while(~scanf("%d", &n))
{
init();
int cnt = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
int x;
scanf("%d", &x);
if(j > i)
{
edge[cnt].u = i;
edge[cnt].v = j;
edge[cnt++].num = x;
}
}
sort(edge, edge + cnt);
LL ans = 0;
for(int i = 0; i < cnt; i++)
{
if(Union(edge[i].u, edge[i].v))
ans += edge[i].num;
}
printf("%lld\n", ans);
}
return 0;
}

  

POJ 1258 Agri-Net的更多相关文章

  1. 最小生成树 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 // 顶点的最大个数 ( ...

  2. 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 ...

  3. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

  4. POJ 1258

    http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...

  5. poj - 1258 Agri-Net (最小生成树)

    http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...

  6. POJ 1258 Agri-Net(Prim算法求解MST)

    题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...

  7. (最小生成树)Agri-Net -- POJ -- 1258

    链接: http://poj.org/problem?id=1258 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  8. Prim算法求权数和,POJ(1258)

    题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...

  9. poj 1258 Agri-Net 解题报告

    题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 ...

  10. POJ 1258 Agri-Net(Prim)

    题目网址:http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

随机推荐

  1. mac使用wget下载网站(仿站)

    wget -c -r -np -k -L -p http://www.xxxx.com 参考 wget的安装 http://blog.csdn.net/ssihc0/article/details/7 ...

  2. Jquery异步请求简单实例(转)

    本文引用自Xingsoft. 一.Jquery向aspx页面请求数据 前台页面JS代码:             $("#Button1").bind("click&qu ...

  3. VS 2005部署应用程序提示“应用程序无法正常启动( 0x0150002)” 解决方案

    遇到这个问题,一定是缺少了CRT.MFC.ATL的DLL,不同版本的VS是不一样的.系统自带这些库的Release版,如果没有自带,打补丁就有了:系统不自带这些库的Debug版,所以Debug版的程序 ...

  4. Webstorm10.0.4注册码

    分享几个Webstorm10的注册码: (1) user name :EMBRACE ===== LICENSE BEGIN =====17739-1204201000002KkN!4z2O8JEyj ...

  5. Extjs-4.2.1(二)——使用Ext.define自定义类

    鸣谢:http://www.cnblogs.com/youring2/archive/2013/08/22/3274135.html --------------------------------- ...

  6. js数组反转

    var _li = test.getElementsByTagName("li"), arrayObj = [].slice.apply(_li),//_li用apply调用sli ...

  7. Nagios3完整配置文档

    第一章:简单快速安装nagios 1.1 准备软件包 在做安装之前确认要对该机器拥有root权限. 确认你安装好的linux系统上已经安装如下软件包再继续. Apache GCC编译器 GD库与开发库 ...

  8. FZU-1926+KMP

    题意:给定一篇文章和一些句子.询问句子是否在文章中出现. kmp模板题 /* kmp */ #include<stdio.h> #include<string.h> #incl ...

  9. 安卓从业者应该关注:Android 6.0的运行时权限

    Android 6.0,代号棉花糖,自发布伊始,其主要的特征运行时权限就很受关注.因为这一特征不仅改善了用户对于应用的使用体验,还使得应用开发者在实践开发中需要做出改变. 没有深入了解运行时权限的开发 ...

  10. 【流媒體】live555—VS2008 下live555编译、使用及测试

    [流媒體]live555—VS22008 下live555编译.使用及测试 Ⅰ live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如R ...