链接:https://vjudge.net/problem/POJ-1258#author=fuxianda

题意:

有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离 
任意两个村庄之间的距离小于 100,000.

思路:

最小生成树

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXM = 10000+10;
const int MAXN = 100+10; struct Node
{
double _x,_y;
}node[MAXN]; struct Path
{
int _l,_r;
double _value;
bool operator < (const Path & that)const{
return this->_value < that._value;
}
}path[MAXM]; int Father[MAXN];
double a[MAXN];
int n, m, v;
int s, p;
int pos;//边数 int Get_F(int x)
{
return Father[x] = (Father[x] == x) ? x : Get_F(Father[x]);
} void Init(int x)
{
for (int i = 1;i <= x;i++)
Father[i] = i;
} double Get_Len(Node a,Node b)
{
return sqrt((a._x - b._x) * (a._x - b._x) + (a._y - b._y) * (a._y - b._y));
} int main()
{
while (cin >> n)
{
Init(n);
pos = 0;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
cin >> v;
path[++pos]._l = i;
path[pos]._r = j;
path[pos]._value = v;
}
}
sort(path + 1,path + 1 + pos);
LL res = 0;
for (int i = 1;i <= pos;i++)
{
int tl = Get_F(path[i]._l);
int tr = Get_F(path[i]._r);
if (tl != tr)
{
Father[tl] = tr;
res += path[i]._value;
}
}
cout << res << endl;
} return 0;
}

  

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

  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. @GetMapping和@PostMapping接收参数的格式

    一.1.使用@Controller 注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面 若返回json等内容到页面,则需要加@ResponseBody注解 ...

  2. bzoj4670: 佛罗里达

    这题直接随机化+贪心就可以爆踩过去,我加了个退火增加容错率而已....其实你随机的次数够多根本不需要... 然后来自肉丝哥哥的正经做法: 先钦定D(A)>D(B),那么可以枚举D(A),然后再去 ...

  3. 配置maven环境变量并安装jar包到本地仓库

    1.下载maven安装包,解压,解压目录如下: 2.配置M2_HOME变量为上一步的路径: 3.配置PATH变量,添加%M2_HOME%\bin;  查看是否配置成功 mvn -v : 4.安装jar ...

  4. BZOJ 1620 [Usaco2008 Nov]Time Management 时间管理:贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1620 题意: 有n个工作,每一个工作完成需要花费的时间为tim[i],完成这项工作的截止日 ...

  5. im资源

    https://github.com/oikomi/FishChatServer https://github.com/subrosa-io https://github.com/WhisperSys ...

  6. python中format()方法格式化字符串

    format()是python2.6新增的一个格式化字符串的方法,功能非常强大,有可能在未来完全替代%格式化方法,相比 % ,format()的优点有: 1 .格式化时不用关心数据类型的问题,form ...

  7. [SDOI2012]任务安排

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2726 [算法] 此题与POJ1180非常相似 但是 , 此题中的t值可能为负 , 这 ...

  8. windows切换到谷歌浏览器黑屏问题

    打开谷歌浏览器,找到右上角,点击设置,如下: 点击高级设置,如下: 关闭使用硬件加速模式,重新打开浏览器,即可.

  9. 如何判断一个for循环执行完毕

    在外面一个变量a=arr.leng; 然后就是进行for循环, 在for循环下面进行判断,因为如果结束那么i的值就会>=a;if条件成立的话,可以在里面进行循环完毕要做的操作.

  10. ubuntu 16.04 安装 Matlab R2016b后启动出现的问题

    (1)报以下错误: License checkout failed.License Manager Error -95MATLAB is unable to connect to the licens ...