题目链接:Agri-Net

最小生成树水题,数组开的和题目描写叙述一样,可是就是RE,有填了个0,还好这个题用 库鲁斯卡尔 敲了一遍,发现了点问题,曾经写的库鲁卡尔模板有点问题,多写了步没用的操作,已修正。

题意:就是一个农夫想选镇长。。。。建一条路,使之能到达全部点,距离最短。

scanf输入

796K 32MS

cin输入

832K 125MS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
const int N = 11000;
using namespace std;
int n,ans,l;
struct node{
int u,v,w;
}edge[10000];
int father[N];
int cmp(const void *a,const void *b)
{
node *X = (struct node *)a;
node *Y = (struct node *)b;
return X->w - Y->w;
}
int find(int x)
{
return (father[x]==x)?(x):find(father[x]);
} void Kruskal()
{
ans = 0;
int uu,vv;
for(int i = 0;i<n;i++)
father[i] = i;
for(int i = 0;i<l;i++)
{
uu = find(edge[i].u);
vv = find(edge[i].v);
if(uu!=vv)
{
father[uu] = vv;
ans += edge[i].w;
}
}
cout<<ans<<endl;
}
int main()
{
int a;
while(cin>>n)
{
l = 0;
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
cin>>a;
edge[l].u = i; edge[l].v = j; edge[l].w = a;
l++;
}
}
qsort(edge,l,sizeof(edge[0]),cmp);
Kruskal();
}
return 0;
}
832K 125MS

POJ 1258-Agri-Net (Kruskal)的更多相关文章

  1. poj 1258 Agri-Net 最小生成树 kruskal

    点击打开链接 Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33733   Accepted: 13539 ...

  2. POJ 1258 Agri-Net (Prim&Kruskal)

    题意:FJ想连接光纤在各个农场以便网络普及,现给出一些连接关系(给出邻接矩阵),从中选出部分边,使得整个图连通.求边的最小总花费. 思路:裸的最小生成树,本题为稠密图,Prim算法求最小生成树更优,复 ...

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

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

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

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

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

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

  7. POJ 1258

    http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...

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

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

  9. POJ 1789 Truck History (Kruskal 最小生成树)

    题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...

  10. POJ 1258 Agri-Net(Prim算法求解MST)

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

随机推荐

  1. vs2010 条件断点 has changed是什么意思?

    在vs2010 断点设置 condition里,有2个选项可以选择: 一个是true,另个是has changed,true好理解,如果表达式为真则停止.但是has changed是什么意思.看了官网 ...

  2. pycurl,Python cURL library

    pycurl — A Python interface to the cURL library Pycurl包是一个libcurl的Python接口.pycurl已经成功的在Python2.2到Pyt ...

  3. POJ 1472 Coins (多重背包+滚动数组)

    Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 25827 Accepted: 8741 Description Pe ...

  4. HDU 4741 Save Labman No.004 2013 ACM/ICPC 杭州网络赛

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4741 题意:给你两条异面直线,然你求着两条直线的最短距离,并求出这条中垂线与两直线的交点. 需要注意的是 ...

  5. HDU 4720 Naive and Silly Muggles (外切圆心)

    Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

  6. php5.5 + apache2.4 安装配置

    php5.5 做了大量的更新,在与apache搭配的时候如何选择也很有讲究,这里我们以64位 php5.6 和 Apache2.4为例介绍如何配置. 工具/原料 Win7/8 64位 php5.5.6 ...

  7. 畅通工程续(Dijkstra算法)

    对Dijkstra算法不是很熟悉,写一下思路,希望通过写博客加深理解 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时, ...

  8. BZOJ 1977: [BeiJing2010组队]次小生成树 Tree( MST + 树链剖分 + RMQ )

    做一次MST, 枚举不在最小生成树上的每一条边(u,v), 然后加上这条边, 删掉(u,v)上的最大边(或严格次大边), 更新答案. 树链剖分然后ST维护最大值和严格次大值..倍增也是可以的... - ...

  9. BZOJ 1103: [POI2007]大都市meg( 树链剖分 )

    早上数学考挂了...欲哭无泪啊下午去写半个小时政治然后就又可以来刷题了.. 树链剖分 , 为什么跑得这么慢... ------------------------------------------- ...

  10. 深入理解-HashMap

    一.HashMap概述 HashMap 在家族中位置:实现了Map接口,继承AbstractMap类.HashMap 允许key/value 都为null. 二.HashMap存储结构 HashMap ...