Agri-Net

题目链接:

http://acm.hust.edu.cn/vjudge/contest/124434#problem/H

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

##题意:

求最小的花费使得各点联通.


##题解:

裸的最小生成树.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 110
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

struct node{

int left,right,cost;

}road[maxn*maxn];

int cmp(node x,node y) {return x.cost<y.cost;}

int p[maxn],m,n;

int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}

int kruskal()

{

int ans=0;

for(int i=1;i<=n;i++) p[i]=i;

sort(road+1,road+m+1,cmp);

for(int i=1;i<=m;i++)

{

int x=find(road[i].left);

int y=find(road[i].right);

if(x!=y)

{

ans+=road[i].cost;

p[x]=y;

}

}

return ans;

}

int dis[maxn][maxn];

int main(int argc, char const *argv[])

{

//IN;

while(scanf("%d", &n) != EOF)
{
m = 0;
memset(road,0,sizeof(road)); for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
scanf("%d", &dis[i][j]);
}
} for(int i=1; i<=n; i++) {
for(int j=i+1; j<=n; j++) {
road[++m].left = i;
road[m].right = j;
road[m].cost = dis[i][j];
}
} int ans=kruskal(); printf("%d\n", ans);
} return 0;

}

POJ 1258 Agri-Net (最小生成树)的更多相关文章

  1. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

  2. POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)

    题目链接: 传送门 Agri-Net Time Limit: 1000MS     Memory Limit: 10000K Description Farmer John has been elec ...

  3. POJ 1258 Agri-Net(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...

  4. poj 1258 Agri-Net【最小生成树(prime算法)】

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

  5. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. 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 ...

  7. 最小生成树 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 // 顶点的最大个数 ( ...

  8. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

  9. poj - 1258 Agri-Net (最小生成树)

    http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...

随机推荐

  1. OPENGL画图类库

    链接  https://www.opengl.org/wiki/Language_bindings http://blog.csdn.net/luozhuang/article/details/421 ...

  2. toad for sqlserver5.7

    toad for sqlserver5.7 虽然SSMS很好很强大,不过有时候使用一些第三方工具可以使MSSQL DBA们更加的方便管理MSSQL toad for sqlserver5.7就是这样一 ...

  3. TUXEDO错误解决方案

    错误1: root@tfjus:/opt/tuxedo/simpapp# buildclient -f simpcl.c -o simpcl simpcl.c: In function 'main': ...

  4. error LNK2005 new,delete 等已经在LIBCMT.lib(delete.obj) 中定义 错误修正

    http://blog.csdn.net/funnyskyf/article/details/5938597 1>uafxcw.lib(afxmem.obj) : error LNK2005: ...

  5. css配合js模拟的select下拉框

    css配合js模拟的select下拉框 <!doctype html> <html> <head> <meta charset="utf-8&quo ...

  6. eclipse export Android jar with jni

    /*********************************************************************** * eclipse export Android ja ...

  7. OK335xS psplash make-image-header.sh hacking

    /***************************************************************************** * OK335xS psplash mak ...

  8. RTP协议分析

      目录(?)[-] 第1章     RTP概述 RTP是什么 RTP的应用环境 相关概念 流媒体 第2章     RTP详解 RTP的协议层次 传输层的子层 应用层的一部分 RTP的封装 RTCP的 ...

  9. 【转】eclipse中egit插件使用

    原文网址:http://my.oschina.net/songxinqiang/blog/192567 eclipse和git这个两个工具的使用人数都是相当多的,在eclipse里面也有egit插件来 ...

  10. db file scattered read 等待事件

    db file scattered read 等待事件: 我们经常会见到db file scattered read  等待事件,在生产环境中,这个等待事件可能更为常见.这个事件表明用户进程正在读数据 ...