3714: [PA2014]Kuglarz
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 596 Solved: 343
[ Submit][ Status][ Discuss]
Description
魔术师的桌子上有n个杯子排成一行,编号为1,2,…,n,其中某些杯子底下藏有一个小球,如果你准确地猜出是哪些杯子,你就可以获得奖品。花费c_ij元,魔术师就会告诉你杯子i,i+1,…,j底下藏有球的总数的奇偶性。
采取最优的询问策略,你至少需要花费多少元,才能保证猜出哪些杯子底下藏着球?
Input
第一行一个整数n(1<=n<=2000)。
第i+1行(1<=i<=n)有n+1-i个整数,表示每一种询问所需的花费。其中c_ij(对区间[i,j]进行询问的费用,1<=i<=j<=n,1<=c_ij<=10^9)为第i+1行第j+1-i个数。
Output
输出一个整数,表示最少花费。
Sample Input
5
1 2 3 4 5
4 3 2 1
3 4 5
2 1
5
1 2 3 4 5
4 3 2 1
3 4 5
2 1
5
Sample Output
7
HINT
Source
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 2020;
const int maxm = maxn * maxn;
typedef long long LL;
struct E{
int x,y,w; E(){}
E(int x,int y,int w): x(x),y(y),w(w){}
bool operator < (const E &B) const{return w < B.w;}
}edgs[maxm];
int n,tot,fa[maxn];
LL Ans;
int getfa(int k) {return k == fa[k] ? k : fa[k] = getfa(fa[k]);}
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint();
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
edgs[++tot] = E(i - 1,j,getint());
for (int i = 1; i <= n; i++) fa[i] = i;
sort(edgs + 1,edgs + tot + 1);
for (int i = 1; i <= tot; i++)
{
int fx = getfa(edgs[i].x),fy = getfa(edgs[i].y);
if (fx == fy) continue; fa[fx] = fy; Ans += 1LL * edgs[i].w;
}
cout << Ans << endl;
return 0;
}