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
题解
1 //It is made by Awson on 2017.10.15 2 #include <set> 3 #include <map> 4 #include <cmath> 5 #include <ctime> 6 #include <cmath> 7 #include <stack> 8 #include <queue> 9 #include <vector> 10 #include <string> 11 #include <cstdio> 12 #include <cstdlib> 13 #include <cstring> 14 #include <iostream> 15 #include <algorithm> 16 #define LL long long 17 #define Min(a, b) ((a) < (b) ? (a) : (b)) 18 #define Max(a, b) ((a) > (b) ? (a) : (b)) 19 #define sqr(x) ((x)*(x)) 20 using namespace std; 21 const int N = 2000; 22 const int INF = ~0u>>1; 23 24 int n, mp[N+5][N+5]; 25 int dist[N+5]; 26 bool vis[N+5]; 27 28 LL Prim() { 29 LL ans = 0; 30 for (int i = 1; i <= n; i++) dist[i] = mp[1][i]; 31 vis[1] = 1; 32 for (int t = 1; t < n; t++) { 33 int loc, minn = INF; 34 for (int i = 1; i <= n; i++) if (!vis[i] && dist[i] < minn) { 35 minn = dist[i], loc = i; 36 } 37 ans += minn; vis[loc] = 1; 38 for (int i = 1; i <= n; i++) dist[i] = Min(dist[i], mp[loc][i]); 39 } 40 return ans; 41 } 42 void work() { 43 scanf("%d", &n); n++; 44 for (int i = 1; i < n; i++) for (int j = i+1; j <= n; j++) scanf("%d", &mp[i][j]), mp[j][i] = mp[i][j]; 45 printf("%lld ", Prim()); 46 } 47 int main() { 48 work(); 49 return 0; 50 }