POJ-1258-Agri Ned
链接: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的更多相关文章
- 最小生成树 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 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 ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- POJ 1258
http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...
- poj - 1258 Agri-Net (最小生成树)
http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...
- POJ 1258 Agri-Net(Prim算法求解MST)
题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- (最小生成树)Agri-Net -- POJ -- 1258
链接: http://poj.org/problem?id=1258 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- Prim算法求权数和,POJ(1258)
题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...
- poj 1258 Agri-Net 解题报告
题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 ...
- POJ 1258 Agri-Net(Prim)
题目网址:http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
随机推荐
- Java 出现“Illegal key size”错误的解决方案
用AES加密时出现"java.security.InvalidKeyException: Illegal key size"异常. 如果密钥大于128, 会抛出上述异常.因为密钥长 ...
- 在jboss中部署可执行jar, deploy executable jar in jboss
首先,题目是个伪命题, jboss容器是不支持直接部署可执行jar包的,jar只会被加载当作lib对待.这里提供了一个小的变通方案. 今天我遇到个问题,把我们的项目中的监控模块独立成一个小项目部署,监 ...
- Java生成UUID不重复的id值
在Java中创建UUID在网上查资料才知道在Java中,变成了UUID.创建方式也出奇简单System.out.println( java.util.UUID.randomUUID());
- system调用命令行命令而不显示命令行窗口
system调用命令行命令而不显示命令行窗口 通常用system调用命令行命令时都会弹出黑底白字的命令行窗口,下面的代码可以不显示弹出的命令行窗口. 代码如下 #pragma comment( lin ...
- 脚踏实地学C#3-装箱和拆箱
装箱:一种接受值类型的值,根据这个值在堆中创建一个完整的引用类型对象并返回对象的引用(堆地址)的隐式转换 int i_number = 2; //在栈中声明int类型i_Number变量并初始化 ob ...
- RightScale发布2017年度云调查报告
RightScale最近发布了他们的年度云报告(RightScale 2017云现状报告,RightScale 2017 State of the Cloud Report),这份报告包括了云计算在采 ...
- codeforces 569D D. Symmetric and Transitive(bell数+dp)
题目链接: D. Symmetric and Transitive time limit per test 1.5 seconds memory limit per test 256 megabyte ...
- BZOJ-3439:Kpm的MC密码(Trie+DFS序+主席树)
背景 想Kpm当年为了防止别人随便进入他的MC,给他的PC设了各种奇怪的密码和验证问题(不要问我他是怎么设的...),于是乎,他现在理所当然地忘记了密码,只能来解答那些神奇的身份验证问题了... 描述 ...
- MT8735A平台配置MT6630
1. codegen配置 2. ProjectConfig CUSTOM_HAL_ANT = mt6630_ant_m1 CUSTOM_HAL_COMBO = mt6630 MTK_BT_CHIP = ...
- bzoj4006
斯坦纳树 比之前要求高了一些 其实利用斯坦纳树的dp[i][s]以i为根,S为状态就行了,先跑一遍斯坦纳树,预处理出dp数组,记住每个S的最小值,然后再dp,这里dp必须要求同一种颜色的状态都必须在S ...