Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44670   Accepted: 18268

Description

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
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

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

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的更多相关文章

  1. Agri Net POJ1258 && Constructing Roads POJ2421

    题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...

  2. A过的题目

    1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...

  3. poj1258 Agri-Net (prim+heap)

    题目链接:poj1258 Agri-Net 这题我上个月做过,是个大水题,今天看见有人用prim+heap做的,就学习了下. #include<cstdio> #include<cs ...

  4. POJ-1258 Agri-Net---MST裸题Prim

    题目链接: https://vjudge.net/problem/POJ-1258 题目大意: 求MST 思路: 由于给的是邻接矩阵,直接prim算法 #include<iostream> ...

  5. Poj1258 Agri-Net (最小生成树 Prim算法 模板题)

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

  6. poj1258 Agri-Net(Prime || Kruskal)

    题目链接 http://poj.org/problem?id=1258 题意 有n个农场,现在要在n个农场之间铺设光纤使得n个农场连接起来,求铺设光纤的最短距离. 思路 最小生成树问题,使用Prime ...

  7. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

  8. poj1258 Agri-Net 最小生成树

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44032   Accepted: 18001 Descri ...

  9. Prim算法POJ1258

    http://poj.org/problem?id=1258 这道题是最简单的一个啦,,,, #include<stdio.h> #include<iostream> #inc ...

  10. POJ1258 基础最小生成树

    本文出自:http://blog.csdn.net/svitter 题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值.输出最小生成树的权值. 题解: prime算法的基本解法: 1.选择一 ...

随机推荐

  1. How to resolve error “Failed to resolve: org.jetbrains.kotlin:kotlin-stdlib-jre7…” when building in Android Studio Ask Question

    //implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation & ...

  2. Visual Studio Code配置技巧

    Visual Studio Code配置技巧 VS Code是啥 Visual Studio Code(以下简称 VS Code) 是一个免费.开源.跨平台的由微软开发的程序编辑器.它是用 TypeS ...

  3. poj2718Smallest Difference (穷竭搜索)

    escription - 题目描述 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数.除非构造的数恰好为0,否则不能以0打头. 举例来说,给定数 ...

  4. Island Transport 【HDU - 4280】【最大流Dinic】

    题目链接 可以说是真的把时间卡爆了,不断的修改了好多次之后才A了,一直T一直T,哭了…… 可以说是很练时间优化了,不断的改,不断的提交,最后竟然是改了Dinic中的BFS()中,我们一旦搜索到了T之后 ...

  5. 12.定义Lock类,用于锁定数据.三步走,锁的优缺点

    #在threading模块当中定义了一个Lock类,可以方便的使用锁定: # #1.创建锁 # mutex = threading.Lock() # # #2.锁定 ''' mutex.acquire ...

  6. IDF-CTF-不难不易的js加密 writeup

    题目链接: http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=28 就是这里 → http://ctf.idf.c ...

  7. 用vue.js写的一个瀑布流的组件

    用vue.js写的一个瀑布流的组件:https://segmentfault.com/a/1190000010741319 https://www.jianshu.com/p/db3cadc03402

  8. CSS的置换和非置换元素

    一个来自面试的坑. 面试的时候考官先问了行内元素和块级元素的区别,这个不难理解.然后一脚就踩进了,置换元素的坑.例如img就是行内置换元素,这种行内元素是可以设置宽高的. 什么是置换元素 一个内容不受 ...

  9. Linux服务正常启动,Linux服务器能访问,但是外部机器不能访问

    公司用到了jenkins,就在自己虚拟机里面部署了一个jenkins.部署成功之后,在Linux虚拟机里面能正常访问,但是外部真实机却不能访问.当时的第一反应就是觉得应该是权限问题,猜测会不会是jen ...

  10. spring 配置属性细节

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28233811 概要(红色为上一篇所讲,蓝 ...