POJ 1258 Agri-Net
来源:互联网 发布:朝鲜有多穷 知乎 编辑:程序博客网 时间:2024/06/16 18:45
最小生成树
kruskal算法
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;#define N 1002struct node { int u, v, w; node() {} node(int _u, int _v, int _w):u(_u), v(_v), w(_w) {}};vector<node> edge;int n,f[N];bool cmp(const node &x, const node &y) { return x.w < y.w;}int find_set(int x) { if (f[x] == x) return x; return f[x] = find_set(f[x]);}int Kruskal() { sort(edge.begin(), edge.end(), cmp); for (int i=1; i<=n; i++) f[i] = i; int ans = 0; for (int i=0, u, v, w; i<edge.size(); i++) { u = edge[i].u, v = edge[i].v, w = edge[i].w; u = find_set(u), v = find_set(v); if (u == v) continue; f[u] = v; ans += w; } return ans;}int main() { while (scanf("%d", &n) == 1) { edge.clear(); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { int c; scanf("%d",&c); if(i == j) c = 0x7fffffff; edge.push_back(node(i, j, c)); edge.push_back(node(j, i, c)); } } printf("%d\n", Kruskal()); } return 0;}
0 0
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj 1258-Agri-net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- POJ+1258++Agri-Net
- Poj 1258 Agri-Net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj-1258-Agri-Net
- poj-1258 Agri-Net
- Poj 1258 Agri-Net
- POJ 1258 - Agri-Net
- 【OpenCV_C++】学习杂记
- 指数型母函数
- Oracle 11g安装指南
- 利用SPI编写类似sockscap的代理工具
- HDU 3068 最长回文(初遇manacher)
- POJ 1258 Agri-Net
- 在oracle database中,一个datafile 可以被drop的条件
- 欢迎使用CSDN-markdown编辑器
- 【Jason's_ACM_解题报告】Division
- HDU 1874 畅通工程续
- 在归档模式下,恢复一个被offline drop的datafile的方法
- 【Jason's_ACM_解题报告】Maximum Product
- ARM7异常与中断
- PL/SQL 总结