点击打开链接

Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 33733   Accepted: 13539

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

给n个点,求最小生成树。这次用的kruskal + 并查集 实现的

#include<stdio.h>
#include<algorithm>
using namespace std;
struct Edge
{
int s, t, pow;
};
Edge edge[25010];
int total;
int n;//输入点的个数
int father[500];
//并查集模板
int getfather(int p)
{
if(father[p] == p)
return p;
else
return father[p] = getfather(father[p]);
}
void merge_(int a, int b)
{
a = getfather(a);
b = getfather(b);
if(a == b)
return ;
else
father[a] = b;
}
void init_set()
{
int i;
for(i = 0; i <= n; i++)
father[i] = i;
}
//kruskal
bool cmp(Edge a, Edge b)
{
return a.pow < b.pow;//´ÓСµ½´óÅÅÐò
}
int kruskal()
{
sort(edge, edge + total, cmp);
bool used[500] = {0};
int i;
int count = 0;
int ans = 0;
init_set();
for(i = 1; i < total; i++)
{
if(getfather(edge[i].s) != getfather(edge[i].t))
{
merge_(edge[i].s, edge[i].t);
ans += edge[i].pow;
count ++;
if(count == n - 1)
break;
}
}
return ans;
}
void addedge(int s, int t, int pow)
{
edge[total].s = s;
edge[total].t = t;
edge[total].pow = pow;
total ++;
}
int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%d", &n) != EOF)
{
int i;
total = 0;
for(i = 1; i <= n; i++)
{
int j;
for(j = 1; j <= n; j++)
{
int num;
scanf("%d", &num);
if(i == j)
continue;
addedge(i, j, num);
}
}
printf("%d\n", kruskal());
}
return 0;
}

poj 1258 Agri-Net 最小生成树 kruskal的更多相关文章

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

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

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

    Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...

  3. poj 3522 Slim Span (最小生成树kruskal)

    http://poj.org/problem?id=3522 Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions ...

  4. POJ 1751 Highways 【最小生成树 Kruskal】

    Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23070   Accepted: 6760   Speci ...

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

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

  6. POJ - 1287 Networking 【最小生成树Kruskal】

    Networking Description You are assigned to design network connections between certain points in a wi ...

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

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

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

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

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

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

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

随机推荐

  1. JAVA:借用OpenOffice将上传的Word文档转换成Html格式

    为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...

  2. bootstrap3-typeahead 自动补全

    很酷的一个自动补全插件 http://twitter.github.io/typeahead.js 在bootstrap中使用typeahead插件,完成自动补全 相关的文档:https://gith ...

  3. selenium+python自动化之pycharm

    方法: 1.注册机界面,选择License serve.填入http://www.cnblogs.com/testdream/ 2.点击ok 3.注册界面选择:Activation coede 4.打 ...

  4. MVC4 WebAPI(一)

    http://www.cnblogs.com/wk1234/archive/2012/04/28/2468491.html 不管是因为什么原因,结果是在新出的MVC中,增加了WebAPI,用于提供RE ...

  5. Java ArrayList操作

    import java.util.ArrayList; import java.util.List; import java.util.Iterator; public class Study { p ...

  6. 黄聪:HtmlAgilityPack,C#实用的HTML解析类 ---- HtmlNode类

    HtmlAgilityPack中的HtmlNode类与XmlNode类差不多,提供的功能也大同小异.下面来看看该类提供功能. 一.静态属性 public static Dictionary<st ...

  7. memcached应用场景(转)

     memcached最吸引人的地方主要在于它的分布式.分布式对于互联网应用来讲,按照用途基本上可划分为三种方式:分布式计算.分布式存储和两者兼而有之.memcached是分布式存储的一种.我们常见的分 ...

  8. CE_现金账户转账汇入汇出交易(案例)(未完成)

    2014-07-15 BaoXinjian 一.摘要 二.案例   通过 Oracle Payments 结算事务处理 通过付款模板 事物处理子类型 已验证 -> 正在结算中 ->

  9. cf111D Petya and Coloring 组合数学,二项式反演

    http://codeforces.com/contest/111/problem/D Little Petya loves counting. He wants to count the numbe ...

  10. mysql多表字段名重复的情况

    CREATE TABLE `card` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `json_str` ) NOT NULL, `f` ,) unsigne ...