Agri-Net 1558 Agri-Net
来源:互联网 发布:sift python 编辑:程序博客网 时间:2024/06/06 01:38
PS: 简单的最小生成树, 省赛热身。
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>using namespace std;const int maxn = 110;struct edges { int u, v, w;};int n;vector<edges> G;int f[maxn+5];int getFather(int x) { if(x==f[x]) return x; else return f[x] = getFather(f[x]);}bool cmp(edges a, edges b) { return a.w < b.w;}int work() { sort(G.begin(), G.end(), cmp); for(int i = 1; i < maxn; i++) f[i] = i; int cnt = 0; int ans = 0; edges t; for(int i = 0; i < (int)G.size(); i++) { t = G[i]; int t1 = getFather(t.u); int t2 = getFather(t.v); if(t1!=t2) { f[t1] = t2; ans += t.w; cnt++; if(cnt==n-1) break; } } return ans;}int main(){ int tmp; edges t; while(scanf("%d", &n)!=EOF) { G.clear(); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { scanf("%d", &tmp); if(i!=j) { t.u = i; t.v = j; t.w = tmp; G.push_back(t); } } } int res = work(); printf("%d\n", res); } return 0;}
0 0
- Agri-Net 1558 Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- codeforces A. Shaass and Oskols 题解
- 关于i++和++i的问题 -----具体到比较纠结的i=(i++)+(i++)问题
- 在VMware虚拟机上安装Ubuntu 10.04 (网址)
- fzoj 2170 花生的序列
- VMware Workstation 7.1注册机(序列号/注册码)
- Agri-Net 1558 Agri-Net
- Minimum coverage matrix(2013省赛第一题)
- POJ题目分类(不定期更新)
- CIPAddressCtrl类的使用(IP地址与CString的互相转化)
- undefined reference问题总结
- POJ-1149
- Java 集合(String类)
- JQuery_2.1.0_日记 5.1
- POJ-3687