[PA 2014]Kuglarz
生活随笔
收集整理的這篇文章主要介紹了
[PA 2014]Kuglarz
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Description
魔術(shù)師的桌子上有n個(gè)杯子排成一行,編號(hào)為1,2,…,n,其中某些杯子底下藏有一個(gè)小球,如果你準(zhǔn)確地猜出是哪些杯子,你就可以獲得獎(jiǎng)品。花費(fèi)c_ij元,魔術(shù)師就會(huì)告訴你杯子i,i+1,…,j底下藏有球的總數(shù)的奇偶性。
采取最優(yōu)的詢問(wèn)策略,你至少需要花費(fèi)多少元,才能保證猜出哪些杯子底下藏著球?
Input
第一行一個(gè)整數(shù)n(1<=n<=2000)。
第i+1行(1<=i<=n)有n+1-i個(gè)整數(shù),表示每一種詢問(wèn)所需的花費(fèi)。其中c_ij(對(duì)區(qū)間[i,j]進(jìn)行詢問(wèn)的費(fèi)用,1<=i<=j<=n,1<=c_ij<=10^9)為第i+1行第j+1-i個(gè)數(shù)。
Output
輸出一個(gè)整數(shù),表示最少花費(fèi)。
Sample Input
51 2 3 4 5
4 3 2 1
3 4 5
2 1
5
Sample Output
7題解
求一棵最小生成樹(shù)...
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 }?
轉(zhuǎn)載于:https://www.cnblogs.com/NaVi-Awson/p/7670872.html
總結(jié)
以上是生活随笔為你收集整理的[PA 2014]Kuglarz的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 四则运算01
- 下一篇: Prototype模式