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

题意:一个裸最小生成树,采用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. 心急的C小加 贪心算法

    心急的C小加 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的 ...

  2. Spring MVC-表单(Form)标签-文本框(Text Box)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_textbox.htm 说明:示例基于Spring MVC 4.1.6. 以下示例 ...

  3. How to pass external configuration properties to storm topology?

    How to pass external configuration properties to storm topology? I want to pass some custom configur ...

  4. HDU 2830 Matrix Swapping II (预处理的线性dp)

    Matrix Swapping II Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. GitLab创建项目

    创建自己的项目:通过地址进入 在文件夹下使用git bash进行 git init,然后ctrl+右键使用TortoiseGit>右键setting 然后再右键setting 拷贝代码时注意要h ...

  6. Linux命令(十)——Shell程序设计一(变量与操作符)

    Shell是linux系统中用户和系统交互的接口,它除了作为命令解释器以外,还是一种高级程序设计语言.在前面介绍的linux命令中,Shell都作为命令解释器出现.下面分两节简单介绍脚本程序设计中的语 ...

  7. Nginx 源码安装和调优

    常见web架构: LAMP  =Linux+Apache+Mysql+PHP LNMP  =Linux+Nginx+Mysql+PHP   nginx概述: 知道:1  不知道:2 Nginx (&q ...

  8. ubuntu修改capslock键,单独使用为esc,组合使用时为ctrl+

    一.下面这部分可以将capslock与ctrl互换 将下面的代码放入-/.Xmodmap中, remove Lock = Caps_Lock remove Control = Control_L ke ...

  9. Shell case esac语句【转】

    本文转载自:http://c.biancheng.net/cpp/view/7006.html C语言中文网推出辅导班啦,包括「C语言辅导班.C++辅导班.算法/数据结构辅导班」,全部都是一对一教学: ...

  10. hdoj--1864--最大保险额(背包)

    最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...