Agri-Net(sdut_1262)
2016-04-18 21:14
197 查看
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; struct node { int u; int v; int w; }q[11234]; int a[110]; int d[110][110]; bool cmp(struct node a, struct node b) { return a.w < b.w; } int f(int x) { while(x != a[x]){ x = a[x]; } return x; } int mer(int x, int y) { int f1, f2; f1 = f(x); f2 = f(y); if(f1 != f2){ a[f1] = f2; return 1; }else { return 0; } } int main() { int n; int cnt; while(~scanf("%d", &n)){ int i, j; for(i =1;i <= n;i++){ a[i] = i; } for(i = 1;i <= n;i++){ for(j = 1;j <= n;j++){ scanf("%d", &d[i][j]); } } cnt = 0; for(i =1;i <= n;i++){ for(j =i+1;j <= n;j++){ q[cnt].u = i; q[cnt].v = j; q[cnt].w= d[i][j]; cnt++; } } sort(q,q+cnt,cmp); int sum = 0; int cou = 0; for( i =0;i < cnt;i++){ if(mer(q[i].u,q[i].v)){ sum += q[i].w; cou++; } if(cou == n-1){ break; } } printf("%d\n", sum); } return 0; }
相关文章推荐
- SQL中的连接查询及其优化原则
- JDBC基础学习(三)—处理BLOB类型数据
- Groovy入门(四)——集合Map
- Linux 进程的 Uninterruptible sleep(D) 状态
- python之self
- css position弹性盒子测试
- AFNetworkReachabilityManager监听手机网络状态的变化
- 基于KVM的Mirantis Fuel 8.0环境搭建
- UVA 10054 The Necklace (无向图的欧拉回路)
- 异常:Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
- Restore IP Addresses
- HDU 1299 Diophantus of Alexandria (数论)
- 某驱动的内核调试检测学习内核调试引擎加载机制
- Android中的style和theme
- HDU 5665 lucky
- oracle 数据库移动数据文件具体操作
- 排序算法---希尔排序
- 即点即改
- 利用RHSocketKit构建自定义协议通信
- 简单对抗某个驱动的反调试