**链接:****传送门! **

题意:一个裸最小生成树,采用Kruskal。


/*************************************************************************
> File Name: poj1258.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月19日 星期一 18时20分30秒
************************************************************************/ #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int MAX_N = 110;
struct edge{
int from , to , cost;
}E[MAX_N*MAX_N]; int par[MAX_N]; void init_union_find_set() { for(int i = 0 ; i < MAX_N ; i++) par[i] = i; }
int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
bool same(int x,int y) { return find(x) == find(y); }
void union_data(int x,int y){ x = find(x); y = find(y); if(x!=y) par[y] = x; } bool cmp(edge a,edge b){
return a.cost < b.cost;
} int Kruskal(int n , int size){
init_union_find_set();
sort(E,E+size,cmp);
int ret = 0;
for(int i = 0 ; i < size ; i++){
if( !same(E[i].from,E[i].to) ){
union_data(E[i].from,E[i].to);
ret += E[i].cost;
}
}
return ret;
} int main(){
int n , cost;
while(~scanf("%d",&n)){
int cnt = 0;
memset(E,0,sizeof(E));
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
scanf("%d",&cost);
if(cost){ E[cnt].from = i , E[cnt].to = j , E[cnt++].cost = cost; }
}
}
int ret = Kruskal(n,cnt);
printf("%d\n",ret);
}
return 0;
}

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

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

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

  2. POJ 1258:Agri-Net Prim最小生成树模板题

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

  3. POJ 1258:Agri-Net(最小生成树&amp;&amp;prim)

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

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

  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|| POJ 2485 Highways MST

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

  7. POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...

  8. 【裸最小生成树】 模板 poj 1258

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #def ...

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

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

随机推荐

  1. CodeForces - 274A - k-Multiple Free Set

    先上题目 k-Multiple Free Set time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. 0912MySQL 执行计划explain详解

    转自http://blog.itpub.net/29773961/viewspace-1767044/ 该博客内容是比较全的,虽然写的比较晦涩,多读几遍还是不错的 explain命令是查看查询优化器如 ...

  3. C#--线程存储数据的机制

    面试题:线程存储数据的机制 Local variables 局部变量 临时存储 栈 Instance class fields 对象存储 堆 (堆的大小只有硬件的限制) Static local va ...

  4. [bzoj1708][Usaco2007 Oct]Money奶牛的硬币_动态规划_背包dp

    Money奶牛的硬币 bzoj-1708 Usaco-2007 Oct 题目大意:在创立了她们自己的政权之后,奶牛们决定推广新的货币系统.在强烈的叛逆心理的驱使下,她们准备使用奇怪的面值.在传统的货币 ...

  5. sqlserve 数据类型具体解释

    decimal   精确数值型  decimal 数据类型能用来存储从-10的38次幂-1到10的38次幂-1的固定精度和范围的数值型数据.使用这样的数据类型时,必须指定范围和精度. 范围是小数点左右 ...

  6. PixelUtils:像素转换工具

    /** 像素转换工具 */ public class PixelUtils { /** * The context. */ private static Context mContext = Cust ...

  7. git的经常使用命令

    $ git config --global user.name "姓名" $ git config --global user.email "xxx@qq.com&quo ...

  8. UVa 12525 Boxes and Stones (dp 博弈)

    Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. ...

  9. SharePoint创建一个简单的Visio Web部件图

    SharePoint创建一个简单的Visio Web部件图 Visio有很多强大的Mash-up混聚功能,使它能够轻松集成到SharePoint 2010中. 1. 打开Visio 2010,创建新的 ...

  10. 【NOIP 2011】 Mayan游戏

    [题目链接] https://www.luogu.org/problemnew/show/P1312 [算法] 深度优先搜索 [代码] #include<bits/stdc++.h> us ...