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 ...
随机推荐
- Python升级已经安装的第三方库
Python升级已经安装的第三方库 主要两步操作,查看需要升级库,升级库.如下: pip list # 列出安装的库 pip list --outdated # 列出有更新的库 pip install ...
- Java 递归算法,遍历文件夹下的所有文件。
用递归算法遍历文件下的所有子文件夹和子文件 文件夹遍历方法 public void getFileList(String strPath){ File f=new File(strPath); try ...
- 理解HTML解析过程
浏览器解析html的过程是:接受网络数据->将二进制码变成字符->将字符变为unicode code points.->tokenizer ->tree constructor ...
- Lucene小例子
package org.itat.test; import java.io.File; import java.io.FileReader; import java.io.IOException; i ...
- codeforces 701C C. They Are Everywhere(尺取法)
题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- 「网络流24题」「LuoguP3358」 最长k可重区间集问题(费用流
题目描述 对于给定的开区间集合 I 和正整数 k,计算开区间集合 I 的最长 k可重区间集的长度. 输入输出格式 输入格式: 的第 1 行有 2 个正整数 n和 k,分别表示开区间的个数和开区间的可重 ...
- Mutual information and Normalized Mutual information 互信息和标准化互信息
实验室最近用到nmi( Normalized Mutual information )评价聚类效果,在网上找了一下这个算法的实现,发现满意的不多. 浙江大学蔡登教授有一个,http://www.zju ...
- zynq交叉编译环境设置
环境准备 Vmware 虚拟机:ubuntu14.04 下载安装文件 环境设置 按照上面地址下载 xilinx-2011.09-50-arm-xilinx-linux-gnueabi.bin 将das ...
- samba 安装运行
samba 安装步骤 1.若之前有安装过相关软件包,先卸载之:sudo apt-get autoremove samba samba-commonsudo apt-get autoremove sys ...
- Sense2vec with spaCy and Gensim
如果你在2015年做过文本分析项目,那么你大概率用的是word2vec模型.Sense2vec是基于word2vec的一个新模型,你可以利用它来获取更详细的.与上下文相关的词向量.本文主要介绍该模型的 ...