比赛 20190521热身赛 评测结果 AAAAAAAAAA
题目名称 挖水井 最终得分 100
用户昵称 djj 运行时间 0.101 s
代码语言 C++ 内存使用 14.80 MiB
提交时间 2019-05-21 18:17:47
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
	while (c <='9' && c >='0') { x = x * 10 + c - '0'; c = getchar();}
	return x * f;
}

struct hhh { int u, v, w;} edge[100010];
int fa[310], c, n, tot, ans, h, ww, k;
bool cmp(hhh l, hhh r) { return l.w < r.w;}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]);}

int main() {
	freopen("water.in", "r", stdin);
	freopen("water.out", "w", stdout);
	n = read();
	for (register int i = 1; i <= n; i ++)
		fa[i] = i;
	for (register int i = 1; i <= n; i ++) {
		ww = read();
		edge[++ tot].u = 0;
		edge[tot].v = i;
		edge[tot].w = ww;
	}
	for (register int i = 1; i <= n; i ++)
		for (register int j = 1; j <= n; j ++) {
			h = read();
			edge[++ tot].u = i;
			edge[tot].v = j;
			edge[tot].w = h; 
		}
	sort(edge + 1, edge + tot + 1, cmp);
	for (register int i = 1; i <= tot; i ++) {
		int x = find(edge[i].u), y = find(edge[i].v);
		if (x == y) continue;
		ans += edge[i].w;
		fa[x] = y;
		k ++;
		if (k == n) break;
	}
	printf("%d\n", ans);
	return 0;
}