POJ 1258-Agri-Net (Kruskal)
2024-08-26 03:19:33
题目链接: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 |
最新文章
- 定时Job在IIS中潜在危险-IIS 定期回收
- Linux磁盘分区及配额123
- SQL转换时间的时分
- cygwin安装
- Oracle Flashback Technologies - 闪回被drop的表
- 第三百天了 how can I 坚持
- VsCode使用技巧
- Android 自定义属性(attrs.xml,TypedArray)
- App项目升级Xcode7&;iOS9(续) - This bundle is invalid. The bundle identifier contains disallowed characters
- Filemanager 的使用
- Docker: How to enable/disable HTTP Proxy in Toolbox
- Scala中foldLeft的总结
- springMVC学习之路4-最后的征程:整合hibernate
- pytest十四:doctest 框架
- [No000017A]改善C#程序的建议3:在C#中选择正确的集合进行编码
- PTA——输出各位数字
- Ubuntu 16.04 更换阿里源
- 什么是webpack?
- 最小生成树(模板 Kruskal)
- Redis总结和提取常用的和重要的命令
热门文章
- Runtime.getRuntime().exec(...)使用方法
- 数据结构——二叉树(Binary Trees)
- wchar_t*和char*之间的互相转换的那些事
- Codeforces 41D Pawn 简单dp
- 你喜欢SOAP吗?反正我不喜欢!
- JS+CSS打造三级折叠菜单,自动收缩其它级 js
- [转]组合数取模 Lucas定理
- PyQt主窗体设置停靠窗口(QDockWidget)的叠加顺序
- django集成微博内容
- Delphi启动/停止Windows服务,启动类型修改为";自动";