题目链接: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

最新文章

  1. 定时Job在IIS中潜在危险-IIS 定期回收
  2. Linux磁盘分区及配额123
  3. SQL转换时间的时分
  4. cygwin安装
  5. Oracle Flashback Technologies - 闪回被drop的表
  6. 第三百天了 how can I 坚持
  7. VsCode使用技巧
  8. Android 自定义属性(attrs.xml,TypedArray)
  9. App项目升级Xcode7&amp;iOS9(续) - This bundle is invalid. The bundle identifier contains disallowed characters
  10. Filemanager 的使用
  11. Docker: How to enable/disable HTTP Proxy in Toolbox
  12. Scala中foldLeft的总结
  13. springMVC学习之路4-最后的征程:整合hibernate
  14. pytest十四:doctest 框架
  15. [No000017A]改善C#程序的建议3:在C#中选择正确的集合进行编码
  16. PTA——输出各位数字
  17. Ubuntu 16.04 更换阿里源
  18. 什么是webpack?
  19. 最小生成树(模板 Kruskal)
  20. Redis总结和提取常用的和重要的命令

热门文章

  1. Runtime.getRuntime().exec(...)使用方法
  2. 数据结构——二叉树(Binary Trees)
  3. wchar_t*和char*之间的互相转换的那些事
  4. Codeforces 41D Pawn 简单dp
  5. 你喜欢SOAP吗?反正我不喜欢!
  6. JS+CSS打造三级折叠菜单,自动收缩其它级 js
  7. [转]组合数取模 Lucas定理
  8. PyQt主窗体设置停靠窗口(QDockWidget)的叠加顺序
  9. django集成微博内容
  10. Delphi启动/停止Windows服务,启动类型修改为&quot;自动&quot;