Agri-Net

  题目大意:农夫有一片农场,现在他要把这些田地用管子连起来,田地之间有一定距离,铺设每一段管子的长度与这些田地与田地距离是一样的,问你最小的铺设方案。

  这一题很裸,Kruskal算法即可,不过一定要注意,这一题是多组数据输入,边的总数记得初始化!

  

 #include <iostream>
#include <functional>
#include <algorithm>
#define MAX 12100
#define MAX_N 110 using namespace std; typedef int Position;
typedef struct _edge
{
int cost;
Position to;
Position from;
}Edge_Set;
int fcomp(const void *a, const void *b)
{
return (*(Edge_Set *)a).cost - (*(Edge_Set *)b).cost;
} static Edge_Set edge[MAX * ];
static Position Set[MAX_N]; void Kruskal(const int, const int);
Position Find(Position);
bool If_Same(Position, Position);
void Union(Position, Position); int main(void)
{
int N, e_sum, tmp;
while (~scanf("%d", &N))
{
e_sum = ;
for (int i = ; i < N; i++)
{
for (int j = ; j < N; j++)
{
scanf("%d", &tmp);
if (i == j) continue;
edge[e_sum].from = i, edge[e_sum].to = j; edge[e_sum++].cost = tmp;
}
}
Kruskal(e_sum, N);
}
return ;
} Position Find(Position x)
{
if (Set[x] < )
return x;
else return Set[x] = Find(Set[x]);
} bool If_Same(Position x, Position y)
{
Position px, py;
px = Find(x); py = Find(y);
return px == py;
} void Union(Position x, Position y)
{
Position px, py;
px = Find(x); py = Find(y); if (px != py)
{
if (Set[px] < Set[py])
{
Set[px] += Set[py];
Set[py] = px;
}
else
{
Set[py] += Set[px];
Set[px] = py;
}
}
} void Kruskal(const int R_sum, const int Node_sum)
{
fill(Set, Set + Node_sum, -);
qsort(edge, R_sum, sizeof(Edge_Set), fcomp); Edge_Set e;
long long ans = ; for (int i = ; i < R_sum; i++)
{
e = edge[i];
if (!If_Same(e.from, e.to))
{
Union(e.from, e.to);
ans += e.cost;
}
}
printf("%lld\n", ans);
}

MST:Agri-Net(POJ 1258)的更多相关文章

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

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

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

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

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

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

  5. POJ 1258

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

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

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

  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 直接或间接连通的 最少 ...

随机推荐

  1. standford工具-parser

    stanford自然语言处理开源了很多工具,很实用也很方便,记录下来,以备后用. 第一篇就从句法分析开始吧(所用的平台都是java+eclipse). <一>操作 1.http://www ...

  2. web.xml配置解释

    web.xml中配置的加载优先级:首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的 ...

  3. Materialize一款不错的框架(装逼必备,想想一帮渣渣们还在说bootstrap的时候,你用materialize,高端洋气,别人仰望着,同事们鄙视的看着你还能不能愉快的玩耍的时候,那种孤高的感觉!-_-//意淫结束)

    这个materialize感觉比bootstrap好一点 当然啦中文文档还木有!所以想搞个materialize中文网的可以抢先咯! materialize是谷歌设计制作的一款框架. HOHO,出去别 ...

  4. JLS(Third Edition) Chapter12 Execution

    这一章详细说明在一个program执行时,发生的activities. 它根据JVM和组成program的类.接口.实例的生命周期 组织.   一个JVM从加载一个特定的类并调用它的main方法开始启 ...

  5. CLGeocoder "Lost connection to geod" #error# when use geocodeAddressString:completionHandler

      I got this warning when I tried to get destination using CLGeoCoder and the warning is coming out ...

  6. VBA 表格操作2 表格复制

    office excel文件有工作簿,里面存放一张张表,表的名字叫做标签名,簿名就是我们常见的的文件名,簿的类型有"一簿一表"与"一簿多表". 完成将多张表合并 ...

  7. BSA基础数据维护

    平台 BSA基础数据维护 .扇区五个字段的内容 本来值为0,经过107上计算解析,得出正常的数值.然后106上报(200050),得到回复(200051). 查看回复数据,是否有错误.比如提示104 ...

  8. LR学习笔记---参数设置 (转 温故而知新)

    LR在录制程序运行的过程中,VuGen(脚本生成器) 自动生成了包含录制过程中实际用到的数值的脚本,如果你企图在录制的脚本中使用不同的数值执行脚本的活动(如查询.提交等等),那么你必须用参数值取代录制 ...

  9. IE8 margin: auto 无法居中

    需要给body元素添加属性 body { text-align: center; width: 100%; } ok,可以正常居中.

  10. 12个Linux进程管理命令介绍

    导读 执行中的程序称作进程.当程序可以执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源.内存.安全属性和与之相关的状态.可以有多个进程关联到同一个程序,并同时执行不会互相干扰.操作 ...