poj 3723 MST
来源:互联网 发布:nginx 新增模块 编辑:程序博客网 时间:2024/05/23 01:16
点击打开链接
#include <iostream>#include <algorithm>#include <cstdio>using namespace std;const int M = 51010;struct edge{long u;long v;long long cost;bool operator <(const edge &t)const{return cost>t.cost; }}e[M];long n,p,m,fa[M];void Inin(){for(int i=0;i<n+p;i++) //{fa[i]=i;}}int find(int x){if(x!=fa[x]){fa[x]=find(fa[x]); //路径压缩}return fa[x];}void Union(int x,int y){int a=find(x);int b=find(y);if(a!=b){fa[a]=b;}}void Kruskal(){Inin();// union-set 初始化 long long ans=0;for(int i=0;i<m;i++){if(find(e[i].u)!=find(e[i].v)){Union(e[i].u,e[i].v);ans+=e[i].cost; // 节约的钱要最大 }}cout<<(n+p)*10000-ans<<endl;}int main() {int t;cin>>t;while(t--){cin>>n>>p>>m;for(int i=0;i<m;i++){scanf("%d%d%ld",&e[i].u,&e[i].v,&e[i].cost);e[i].v+=n; //偏移 }sort(e,e+m);Kruskal(); //每次将两个连通分量合并成一个时 花费最小 }return 0;}
0 0
- POJ 3723 Conscription MST
- poj 3723 MST
- POJ 3723 Conscription(mst)
- [MST] poj 1679
- poj 1861 Network MST
- poj 1751 Highways MST
- poj The Unique MST
- poj-1861-MST
- poj 1251 MST
- poj 1287 MST(kruskal)
- POJ 1861 - MST - Krustral
- POJ 3625 - MST
- POJ 1251 MST Krustral
- The Unique MST POJ
- The Unique MST POJ
- The Unique MST POJ
- The Unique MST POJ
- The Unique MST POJ
- 通过 PHP 跳出 iframe 框架
- iOS学习笔记:交换Button中图片与文字的左右位置
- WatchOS 2 problem:dyld: Library not loaded
- 64和10进制的相互转换-PHP实现
- String参数是传值还是传递引用的测试
- poj 3723 MST
- opencv-图片特征点提取
- spark中多表连接优化实例
- Java读取文件内容并转换为字符串
- hdu 1505 City Game dp
- js中的windows对象
- hive 记录帖 待整理
- 【Unity3D自学记录】鼠标拖拽物体移动
- JAVA中用三种方法将字符串持久化到文件中