[PA 2014]Kuglarz

扫码查看

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

Sample Output

7

题解

求一棵最小生成树...

 //It is made by Awson on 2017.10.15
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
using namespace std;
const int N = ;
const int INF = ~0u>>; int n, mp[N+][N+];
int dist[N+];
bool vis[N+]; LL Prim() {
LL ans = ;
for (int i = ; i <= n; i++) dist[i] = mp[][i];
vis[] = ;
for (int t = ; t < n; t++) {
int loc, minn = INF;
for (int i = ; i <= n; i++) if (!vis[i] && dist[i] < minn) {
minn = dist[i], loc = i;
}
ans += minn; vis[loc] = ;
for (int i = ; i <= n; i++) dist[i] = Min(dist[i], mp[loc][i]);
}
return ans;
}
void work() {
scanf("%d", &n); n++;
for (int i = ; i < n; i++) for (int j = i+; j <= n; j++) scanf("%d", &mp[i][j]), mp[j][i] = mp[i][j];
printf("%lld\n", Prim());
}
int main() {
work();
return ;
}
05-03 21:44
查看更多