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

题意:一个裸最小生成树,采用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. error at ::0 can't find referenced pointcut...解决方法

    error at ::0 can't find referenced pointcut...解决方法 学习了:http://dyldragon.iteye.com/blog/512612 升级aspe ...

  2. swift 笔记 (十四) —— 构造过程

    构造过程 为了生成类.结构体.枚举等的实例,而做的准备过程,叫做构造过程. 为了这个过程,我们一般会定义一个方法来完毕,这种方法叫做构造器.当然它的逆过程,叫做析构器,用于在实例被释放前做一些清理工作 ...

  3. Java 8 类型转换及改进

    为对象的类型做强制转换是一种很不好的设计.但在某些情况下,我们没有其它选择.Java自诞生的那一天起,就具备这样的功能. 我觉得Java 8在一定程度改善了这项古老的技术. 静态转型 Java中最经常 ...

  4. Linux系统调用具体解释(怎样从用户空间进入内核空间)

    系统调用概述 计算机系统的各种硬件资源是有限的,在现代多任务操作系统上同一时候执行的多个进程都须要訪问这些资源,为了更好的管理这些资源进程是不同意直接操作的,全部对这些资源的訪问都必须有操作系统控制. ...

  5. Linux命令(九)——系统监视和进程控制

    与windows系统一样,linux系统中也有很多进程在同时运行,每个进程都有一个识别码PID,它是进程的唯一识别标志. 一.进程的类型 1.系统进程 在操作系统启动后,系统环境平台运行所加载的进程, ...

  6. c++11 实现半同步半异步线程池

    感受: 随着深入学习,现代c++给我带来越来越多的惊喜- c++真的变强大了. 半同步半异步线程池: 事实上非常好理解.分为三层 同步层:通过IO复用或者其它多线程多进程等不断的将待处理事件加入到队列 ...

  7. 在Mac OSX系统的Docker机上启用Docker远程API功能

    在Mac OSX系统的Docker机上启用Docker远程API功能 作者:chszs,未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs D ...

  8. 关于ShapeDrawable应用的一些介绍(上)

    在Android中, 很多时候系统原生的控件的格式并不能满足我们的需求,我们想要更加好看点的样式,像什么圆角矩形啊,颜色渐变啊,阴影效果啊等等的,这个时候就是我们的 ShapeDrawable发挥效果 ...

  9. DDos攻击篇

    DDoS(Distributed Denial of Service,分布式拒绝服务)攻击的主要目的是让指定目标无法提供正常服务,甚至从互联网上消失,是目前最强大.最难防御的攻击之一. 1.1. SY ...

  10. Top 5 Timed Events[转]

    Event                                               Waits    Time (s) Ela Time --------------------- ...