Agri-Net —poj1258
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 44670 | Accepted: 18268 |
Description
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
Output
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
求把所有town连接起来所需最少边权值和,把边权值按从小到大排序,如果当前边权值连接的两边没有关系就把当前边权值相加,如果有关系当前值就没有价值,不用浪费资源联通这两个town。到最后肯定会把所有town连接~
#include<iostream>
#include<algorithm> using namespace std; #define N 1000005 int k, n, a, f[N]; struct node
{
int x, y, w;
}P[N]; int cmp(const void *u, const void *b)
{
node c, d;
c = *(node *)u;
d = *(node *)b; return c.w-d.w;
} void init()
{
for(int i = ; i < N; i++)
f[i] = i;
}
int found(int a)
{
if(f[a] != a)
f[a] = found(f[a]);
return f[a];
} int main()
{
while(cin >> n)
{
k = ; init(); for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
{
cin >> a;
P[k].w = a;
P[k].x = i, P[k].y = j;
k++;
} qsort(P, k, sizeof(P[]), cmp); int ans = ; for(int i = ; i < k; i++)
{
int nx = found(P[i].x), ny = found(P[i].y); if(nx != ny)
{
f[nx] = ny;
ans += P[i].w;
}
}
//int ans = kuscral(1, n); cout << ans << endl;
}
return ;
}
Agri-Net —poj1258的更多相关文章
- Agri Net POJ1258 && Constructing Roads POJ2421
题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...
- A过的题目
1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...
- poj1258 Agri-Net (prim+heap)
题目链接:poj1258 Agri-Net 这题我上个月做过,是个大水题,今天看见有人用prim+heap做的,就学习了下. #include<cstdio> #include<cs ...
- POJ-1258 Agri-Net---MST裸题Prim
题目链接: https://vjudge.net/problem/POJ-1258 题目大意: 求MST 思路: 由于给的是邻接矩阵,直接prim算法 #include<iostream> ...
- Poj1258 Agri-Net (最小生成树 Prim算法 模板题)
题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- poj1258 Agri-Net(Prime || Kruskal)
题目链接 http://poj.org/problem?id=1258 题意 有n个农场,现在要在n个农场之间铺设光纤使得n个农场连接起来,求铺设光纤的最短距离. 思路 最小生成树问题,使用Prime ...
- 最小生成树 prime poj1258
题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...
- poj1258 Agri-Net 最小生成树
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44032 Accepted: 18001 Descri ...
- Prim算法POJ1258
http://poj.org/problem?id=1258 这道题是最简单的一个啦,,,, #include<stdio.h> #include<iostream> #inc ...
- POJ1258 基础最小生成树
本文出自:http://blog.csdn.net/svitter 题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值.输出最小生成树的权值. 题解: prime算法的基本解法: 1.选择一 ...
随机推荐
- poj1163The Triangle(动态规划,记忆化搜索)
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calc ...
- IDF-CTF-简单的js加密 writeup
题目链接: http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=43 知识点:js语法 这里这里→ http://c ...
- Communications link failure mysql自动停止 连接拒绝 mysqld dead but sub。。。
服务器环境中 JAVA 连接数据库 Communications link failure, Contection refused 网上很多这种情况,解决基本上是将127.0.0.1换成localho ...
- 一波骚操作,我把 SQL 执行效率提高了 10,000,000 倍!
作者:风过无痕-唐 http://www.cnblogs.com/tangyanbo/p/4462734.html 场景 我用的数据库是mysql5.6,下面简单的介绍下场景 课程表: create ...
- 《剑指offer》面试题7 用两个栈实现队列 Java版
书中方法:队列是先进先出的,栈是先进后出的,试想把一串数压入A栈,接着一个个出栈并压入B栈,便会完成"头在下"到"头在上"的转变.B栈内还有元素时,直接出栈表示 ...
- Cnblogs 的 MetaWeblog 的接口发生了变化
Cnblogs 的 MetaWeblog 的接口发生了变化 */--> Cnblogs 的 MetaWeblog 的接口发生了变化 最近把 emacs 重新配置了一下,把 cnblogs 包也重 ...
- 最长公共上升子序列 (LIS+LCS+记录)
[题目描述] 给出两个序列,求出最长公共上升子序列的长度,并输出其中一个解. [题目链接] http://noi.openjudge.cn/ch0206/2000/ [算法] 经典问题,结合了LIS和 ...
- C#设计模式:装饰者模式(Decorator Pattern)
一,装饰者模式(Decorator Pattern):装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能. 二,在以上代码中我们是中国人是根本行为,我们给中国人装饰我会说英语 ...
- http响应代码解释
200:成功响应 302:找到,但是请求的资源在另外一个不同的url中. 400:错误请求.这个请求不能被服务器所理解,客户端必须修改请求. 401:未认证,这个请求需要用户认证. 404:未找到.服 ...
- SpringCloud Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded
出现此问题,有可能是spring cloud 与spring boot 版本不匹配引发的问题,此次用的版本是:Finchley.RC1 经过一番关键字查找,发现spring cloud 与spring ...