POJ 1258
来源:互联网 发布:智能问答系统源码 编辑:程序博客网 时间:2024/05/16 05:55
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;#define maxx 105int k,p[maxx];int n;struct Edge{ int u,v,w;}edge[maxx*maxx/2];bool cmp(Edge a,Edge b){ return a.w<b.w;}int findpa(int x){ return p[x]==x?x:p[x]=findpa(p[x]);}int kruskal(){ for(int i=1;i<=n;i++) p[i]=i; sort(edge,edge+k,cmp); int ans=0,num=0; for(int i=0;i<k;i++) { int p1=findpa(edge[i].u); int p2=findpa(edge[i].v); if(p1!=p2){ num++; ans+=edge[i].w; p[p1]=p2;}if(num==n-1)return ans; } return ans;}int main(){int x;while(scanf("%d",&n)!=EOF){ k=0; for(int i=1;i<=n;i++)for(int j=1;j<=n;j++) { scanf("%d",&x); if(i<j){ edge[k].u=i; edge[k].v=j; edge[k].w=x; k++;} } int ans=kruskal(); printf("%d\n",ans);}return 0;}
0 0
- Poj 1258
- poj 1258
- POJ 1258
- poj 1258
- POJ 1258
- POJ 1258
- poj 1258
- poj 1258
- poj 1258
- POJ 1258
- poj 1258
- poj 1258
- poj 1258
- POJ 1258
- POJ 1258
- POJ 1258
- POJ 1258
- POJ - 1258
- Tracking-Learning-Detection (TLD算法总结)
- JAVA基本语法--java的数据类型
- 插值器 animation动画
- 【特征检测】FAST特征点检测算法
- Android Studio快捷键整理
- POJ 1258
- CentOS 安装 Maven
- QQ游戏百万人同时在线服务器架构实现
- tokudb分区表建立cluster index报错
- 产生不大于100的随机正整数,用户通过键盘对随机正整数进行猜测,猜大了给用户提示猜大,猜小提示猜小, 猜对了则提示“恭喜,猜对了”,结束游戏。
- 使用itms-services安装ipa
- leetcode--twoSum
- 【leetcode】26. Remove Duplicates from Sorted Array
- HDU 1233