BZOJ3714 [PA2014]Kuglarz

来源:互联网 发布:医院药房软件系统 编辑:程序博客网 时间:2024/05/18 03:38

题意:n(1 <= n <= 2000)个杯子,某些杯子下有一个小球,其余没有,你可以花费c(i, j)元来询问[i, j]的杯子球的奇偶性,问最少花费多少元才能保证猜出每个杯子下是否有球。

分析:

昨天比赛的第二题,混了10分,完全没想到是个MST...gzz又一次AK,无限orz中...

参考小胖的奇偶,询问[i, j]相当于把i-1和j合并到一个并查集里,因为最后所有点会合并到一个并查集里,也就是个树,所以答案就是求一遍最小生成树。

记得答案要用long long,虽然xoj数据水不用long long也能过...

#include <cstdio>#include <algorithm>using namespace std;const int N = 2005;int n,x,tt,f[N];long long ans;struct Edge {int x, y, w;bool operator < (const Edge &rhs) const {return w < rhs.w;}}e[N*N];int fnd(int x) {return f[x] == x ? x : f[x] = fnd(f[x]);}int main() {scanf("%d", &n);for(int i = 1; i <= n; i++)for(int j = i; j <= n; j++)scanf("%d", &x), e[tt].x=i-1,e[tt].y=j,e[tt++].w=x;for(int i = 0; i <= n; i++) f[i] = i;sort(e, e+tt);for(int i = 0; i < tt; i++) if(fnd(e[i].x) != fnd(e[i].y))f[fnd(e[i].x)] = fnd(e[i].y), ans += e[i].w;printf("%lld", ans);return 0;}


1 0
原创粉丝点击