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

题意:一个裸最小生成树,采用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. Ajax json 数据格式

    ajax : 是么是同步 什么事异步 同步现象:客户端发送请求到服务端,当服务端返回响应之前,客户端都处于等待卡死状态. 异步现象:客户端发送请求到服务器端,无论服务器是否返回,客户端都可以随意做其他 ...

  2. POJ 3207

    还是那句话,做2SAT题时,找出矛盾点基本上可解了.这道题也是这样 题意是说给出一个圆上的 n 个点(0~n-1编号),然后在指定的 m 对点之间各连一条线(可以在圆内,也可以在圆外,可以是曲线,这点 ...

  3. 《编程导论(Java)&#183;2.1.2 啊,我看到了多态》-什么是多态(polymorphism)

    1.不明觉厉 很多人学习多态时,会认为. 之所以不明觉厉,由于多态的定义:事物存在的多种表现形态:而后,有人将重载(overload).改写(override).多态变量和泛型归结于同一个术语&quo ...

  4. 【android】uses-permission和permission具体解释

    1.<uses-permission>: 官方描写叙述: If an application needs access to a feature protected by a permis ...

  5. 0x55 环形与后效性问题

    poj2228 分第一天是否熟睡DP两次 #include<cstdio> #include<iostream> #include<cstring> #includ ...

  6. php中一个经典的!==的用法

    php中一个经典的!==的用法 <?php $str = 'Every time you bleed for reaching greatness.'; $cha = 'E'; if(strpo ...

  7. 【HAOI 2008】 移动玩具

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1054 [算法] 广度优先搜索 [代码] #include<bits/stdc+ ...

  8. axis2调用webservice教训

    总结教训,axis2client调用WS接口时url不能加?wsdl,而用cxf调用时则要加上. 今天用axis2的RpcServerClient调用https的webservice接口,在设置完op ...

  9. jupyter在特定环境特定目录中启动

    代码如下: @echo off start %windir%\System32\cmd.exe "/c" D:\Anaconda\Scripts\activate.bat # 启动 ...

  10. C# txt文件操作

    //打开文件到流 FileStream fs=new FileStream(path,FileMode.Open,FileAccess.ReadWrite); //写文件流的方法 StreamWrit ...