[PA 2014]Kuglarz
生活随笔
收集整理的這篇文章主要介紹了
[PA 2014]Kuglarz
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
魔術師的桌子上有n個杯子排成一行,編號為1,2,…,n,其中某些杯子底下藏有一個小球,如果你準確地猜出是哪些杯子,你就可以獲得獎品?;ㄙMc_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
51 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\n", Prim()); 46 } 47 int main() { 48 work(); 49 return 0; 50 }?
轉載于:https://www.cnblogs.com/NaVi-Awson/p/7670872.html
總結
以上是生活随笔為你收集整理的[PA 2014]Kuglarz的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 四则运算01
- 下一篇: Prototype模式