POJ 1258 Agri-Net( 裸最小生成树 )
**链接:****传送门! **
题意:一个裸最小生成树,采用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( 裸最小生成树 )的更多相关文章
- POJ 1258 Agri-Net(Prim求最小生成树)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64912 Accepted: 26854 Descri ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- POJ 1258:Agri-Net(最小生成树&&prim)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38918 Accepted: 15751 Descri ...
- 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 ...
- 最小生成树 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 // 顶点的最大个数 ( ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...
- 【裸最小生成树】 模板 poj 1258
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #def ...
- poj - 1258 Agri-Net (最小生成树)
http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...
随机推荐
- mongodb--group聚合运算
mongodb本质就是要做一个高性能,能简单则简单,不要把mongodb中的运算做的太复杂 count 最简单的一个聚合方法 distinct 选择结果中剔除重复的一个键值, 跟sql语句的效果是一样 ...
- 关于使用JetbrainsCrack破解idea
转载自:https://blog.csdn.net/android_ztz/article/details/73762603 一.前言 IDEA在业界被公认为JAVA最优秀的开发工具,但是它不像ecl ...
- 链表快排 & 基于链表的排序
以前只知道链表做插入(朴素.非二分)排序挺方便的.现在知道了(单)链表进行快速排序也是很好的(只是跟一般的快排的方式不一样). 参考: http://blog.csdn.net/otuhacker/a ...
- springmvc 监听器getWriter() has already been called for this response问题
springmvc 监听器getWriter() has already been called for this response问题 在监听器中,如果return true,就不要使用 respo ...
- 网络编程----堵塞、非堵塞和同步、异步IO
我是学渣.但我想进步. 本文是面试我的牛人问我的.你知道什么是堵塞.非堵塞和同步.异步IO么?自觉得是分布式系统程序猿的我居然不知道.学习吧. 首先介绍堵塞IO和非堵塞IO: 堵塞IO:是指说程序等待 ...
- Android Studio左边栏Project不见了?
非常多Android Stuio刚開始学习的人可能会一不小心把左边的Project栏给关了.结果发现找非常久也没找到怎么再打开Project栏. 如图.点击左下角button,Project就出来了.
- Spark部分:几个重要的端口汇总
50070:HDFSwebUI的端口号 8485:journalnode默认的端口号 9000:非高可用访问数rpc端口 8020:高可用访问数据rpc 8088:yarn的webUI的端口号 808 ...
- CF D. Beautiful numbers (数位dp)
http://codeforces.com/problemset/problem/55/D Beautiful Numbers : 这个数能整除它的全部位上非零整数.问[l,r]之间的Beautifu ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第8章节--配送SP2013Apps 总结
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第8章节--配送SP2013Apps 总结 SP商店提供开发者和ISVs权限訪问潜在地世界各地成百上千的S ...
- 数据库技术_Orcale技术(0002)_5分钟会用存储过程_存储过程实例
基础技术: 样例业务功能: 1.依据传入的类型A_TYPE联合查询PROCEDURE_TEST_A表.PROCEDURE_TEST_A_SUB表中的数据.并显示主要内容. 2.依据传入的类型A_TYP ...