POJ 1258-Agri-Net (Kruskal)
题目链接:Agri-Net
最小生成树水题,数组开的和题目描写叙述一样,可是就是RE,有填了个0,还好这个题用 库鲁斯卡尔 敲了一遍,发现了点问题,曾经写的库鲁卡尔模板有点问题,多写了步没用的操作,已修正。
题意:就是一个农夫想选镇长。。。。建一条路,使之能到达全部点,距离最短。
scanf输入
796K | 32MS |
cin输入
832K | 125MS |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
const int N = 11000;
using namespace std;
int n,ans,l;
struct node{
int u,v,w;
}edge[10000];
int father[N];
int cmp(const void *a,const void *b)
{
node *X = (struct node *)a;
node *Y = (struct node *)b;
return X->w - Y->w;
}
int find(int x)
{
return (father[x]==x)?(x):find(father[x]);
} void Kruskal()
{
ans = 0;
int uu,vv;
for(int i = 0;i<n;i++)
father[i] = i;
for(int i = 0;i<l;i++)
{
uu = find(edge[i].u);
vv = find(edge[i].v);
if(uu!=vv)
{
father[uu] = vv;
ans += edge[i].w;
}
}
cout<<ans<<endl;
}
int main()
{
int a;
while(cin>>n)
{
l = 0;
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
cin>>a;
edge[l].u = i; edge[l].v = j; edge[l].w = a;
l++;
}
}
qsort(edge,l,sizeof(edge[0]),cmp);
Kruskal();
}
return 0;
}
832K | 125MS |
POJ 1258-Agri-Net (Kruskal)的更多相关文章
- poj 1258 Agri-Net 最小生成树 kruskal
点击打开链接 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33733 Accepted: 13539 ...
- POJ 1258 Agri-Net (Prim&Kruskal)
题意:FJ想连接光纤在各个农场以便网络普及,现给出一些连接关系(给出邻接矩阵),从中选出部分边,使得整个图连通.求边的最小总花费. 思路:裸的最小生成树,本题为稠密图,Prim算法求最小生成树更优,复 ...
- 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 ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- 最小生成树 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(最小生成树 Prim+Kruskal)
题目链接: 传送门 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Description Farmer John has been elec ...
- POJ 1258
http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...
- poj - 1258 Agri-Net (最小生成树)
http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...
- POJ 1789 Truck History (Kruskal 最小生成树)
题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...
- POJ 1258 Agri-Net(Prim算法求解MST)
题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
随机推荐
- 我在北京找工作(二):java实现算法<1> 冒泡排序+直接选择排序
工作.工作.找工作.经过1个多星期的思想斗争还是决定了找JAVA方面的工作,因为好像能比PHP的工资高点.呵呵 :-) (其实我这是笑脸,什么QQ输入法,模拟表情都没有,忒不人性化了.) 言归正传, ...
- 啊华北哦好咕~~(╯﹏╰)b
http://v.qq.com/boke/page/c/h/0/c01173tzeh0.html http://v.qq.com/boke/page/r/7/x/r0117l07r7x.html ht ...
- XMPP--- error : linker command failed with exit code 1
error: linker command failed with exit code 1 (use -v to see invocation) 错误原因:libidn.a文件没添加上去 解决方法:l ...
- 免费利用网页版谷歌翻译实现任意语言转换php版
本文源发布地址: http://ourgarden.cn/2013/07/20/%E5%85%8D%E8%B4%B9%E5%88%A9%E7%94%A8%E7%BD%91%E9%A1%B5%E7%89 ...
- HDU1163【九余数定理】【水题】
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- js window.onload事件
1.最简单的调用方式 直接写到html的body标签里面,如: ? 1 2 3 4 <html> <body onload="func()"& ...
- vim的漫漫长征路
在系统的学习vim之前,先在网上找了些教程大概了解下,因为首先我们要先将vim给用起来,然后在系统的学习过程中不断的充实自己对vim的理解. ----------------------------- ...
- POJ 1458 最长公共子序列 LCS
经典的最长公共子序列问题. 状态转移方程为 : if(x[i] == Y[j]) dp[i, j] = dp[i - 1, j - 1] +1 else dp[i, j] = max(dp[i - 1 ...
- 简单实用的下拉菜单(CSS+jquery)
原文 简单实用的下拉菜单(CSS+jquery) 没什么可以说的,直接上例子 html+jquery代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...
- Java之GUI编程(一)
GUI全称Graphical User Interfaces,意为图形用户户界面,又称为图形用户接口.GUI指的就是採用图形方式显示的计算机操作用户界面,打个例如吧.我们点击QQ图标,就会弹出一个QQ ...