点击打开链接

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. IE和FireFox中JS兼容之event .

    event对象 IE 中可以直接使用 event 对象,而 FF 中则不可以,解决方法之一如下:var theEvent = window.event || arguments.callee.call ...

  2. gcc、g++

    http://hi.baidu.com/zhangcunli8499/item/257e187360b48b2bd6a89cc6 g++ src/*.cpp -I include/ -I includ ...

  3. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  4. BlueStacks 设置代理服务器 Proxifier指定任意程序的代理服务器

    详见地址: http://www.ccproxy.com/proxifier-tou-ming-dai-li.htm BlueStacks如何使用代理服务器 http://www.360doc.com ...

  5. 【web】 亿级Web系统搭建——单机到分布式集群

      当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架 ...

  6. thymeleaf之fragment

    MUEAS项目,web前端采用thymeleaf作为展示层.这个view解析器,个人觉得非常不错.简单而且性能也比较好!个人觉得比JSP和freemarker之类,简单易用! 今天简单记录一下frag ...

  7. linux crontab 实现每秒执行(转)

    linux crontab 命令,最小的执行时间是一分钟.如需要在小于一分钟内重复执行,可以有两个方法实现. 1.使用延时来实现每N秒执行 创建一个php做执行动作,非常简单,就是把当前时间写入log ...

  8. LINQ学习入门教程(一)

    LINQ 查询简介       Linq 是一跨各种数据源和数据格式的数据模型:它在查询是,始终是把它作为一种对象来操作,可以使用基本相同的编码模型查询和数据的转换XML,SQL,ADO数据等: Li ...

  9. IOS开发-UIBarButtonItem系统自带图标总结

    1.这四个返回的是后面的单词. UIBarButtonSystemItemDone UIBarButtonSystemItemCancel UIBarButtonSystemItemEdit UIBa ...

  10. MySQL计算时间差

    MySQL计算两个日期的时间差函数:TIMESTAMPDIFF 语法: TIMESTAMPDIFF(interval, datetime_expr1, datetime_expr2) interval ...