比赛 |
至少完成十道练习 |
评测结果 |
AAAAAAAAAA |
题目名称 |
最优布线问题 |
最终得分 |
100 |
用户昵称 |
kZime |
运行时间 |
1.199 s |
代码语言 |
C++ |
内存使用 |
35.04 MiB |
提交时间 |
2017-05-20 18:01:14 |
显示代码纯文本
# include <bits/stdc++.h>
# define MAXN 1503
# define getchar readc
using namespace std;
char buf[1<<18], *fs, *ft;
inline char readc() {
return (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1<<18, stdin)), fs == ft) ? EOF : *fs++;
}
inline int gn() {
int k = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) k = k * 10 + c - '0';
return k * f;
}
struct edge {
int fr, to, ne, w;
inline edge() {
to = ne = 0;
}
inline edge(int fr_, int to_, int ne_, int w_) {
fr = fr_;
to = to_;
ne = ne_;
w = w_;
}
}e[MAXN * MAXN];
int cnt, head[MAXN], n;
inline void addedge(int fr, int to, int w) {
e[++cnt] = edge(fr, to, head[fr], w), head[fr] = cnt;
}
inline bool cmp(edge a, edge b) {
return a.w < b.w;
}
int fa[MAXN];
int getf(int x) {
if(x == fa[x]) return x;
else {
fa[x] = getf(fa[x]);
return fa[x];
}
}
inline void marge(int a, int b) {
int af = getf(a), bf = getf(b);
fa[bf] = af;
}
inline int KRUSCAL() {
int ans = 0;
for(int i = 1; i <= n; i++) fa[i] = i;
sort(e + 1, e + 1 + cnt, cmp);
for(int i = 1; i <= cnt; i++) {
int x = e[i].fr, y = e[i].to;
if(getf(x) != getf(y)) {
ans += e[i].w;
marge(x, y);
}
}
return ans;
}
int main() {
freopen("wire.in", "r", stdin);
freopen("wire.out", "w", stdout);
n = gn();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
int z = gn();
if(j > i) addedge(i, j, z);
}
}
printf("%d\n", KRUSCAL());
}