4007: [JLOI2015]战争调度
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 282 Solved: 159
[ Submit][ Status][ Discuss]
Description
脸哥最近来到了一个神奇的王国,王国里的公民每个公民有两个下属或者没有下属,这种
关系刚好组成一个 n 层的完全二叉树。公民 i 的下属是 2 * i 和 2 * i +1。最下层的公民即叶子
节点的公民是平民,平民没有下属,最上层的是国王,中间是各级贵族。现在这个王国爆发了
战争,国王需要决定每一个平民是去种地以供应粮食还是参加战争,每一个贵族(包括国王自
己)是去管理后勤还是领兵打仗。一个平民会对他的所有直系上司有贡献度,若一个平民 i 参
加战争,他的某个直系上司 j 领兵打仗,那么这个平民对上司的作战贡献度为 wij。若一个平民
i 种地,他的某个直系上司 j 管理后勤,那么这个平民对上司的后勤贡献度为 fij,若 i 和 j 所
参加的事务不同,则没有贡献度。为了战争需要保障后勤,国王还要求不多于 m 个平民参加
战争。国王想要使整个王国所有贵族得到的贡献度最大,并把这件事交给了脸哥。但不幸的是,
脸哥还有很多 deadline 没有完成,他只能把这件事又转交给你。你能帮他安排吗?
Input
第一行两个数 n;m。接下来 2^(n-1) 行,每行n-1 个数,第 i 行表示编号为 2^(n-1)-1+ i 的平民对其n-1直系上司的作战贡献度,其中第一个数表示对第一级直系上司,即编号为 (2^(n-1)-1+ i)/2 的贵族的作战贡献度 wij,依次往上。接下来 2^(n-1)行,每行n-1个数,第i行表示编号为 2^(n-1)-1+ i的平民对其n-1个直系上司的后勤贡献度,其中第一个数表示对第一级直系上司,即编号为 (2^(n-1)-1+ i)/2 的贵族的后勤贡献度 fij ,依次往上。
Output
一行一个数表示满足条件的最大贡献值
Sample Input
3 4
503 1082
1271 369
303 1135
749 1289
100 54
837 826
947 699
216 389
503 1082
1271 369
303 1135
749 1289
100 54
837 826
947 699
216 389
Sample Output
6701
HINT
对于 100% 的数据,2 <= n <= 10,m <= 2n 1,0 <= wij ;fij <= 2000
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = (1<<12);
int n,m,Ans,g[maxn][maxn];
vector <int> f[maxn],w[maxn];
void Dfs(int x,int L)
{
if (L == n)
{
for (int o = 0; o < (1<<n-1); o++)
{
int sw,sf,op = o<<3; sw = sf = 0;
for (int i = 3; i <= n + 1; i++)
if (op&(1<<i)) sw += w[x][i-3];
else sf += f[x][i-3];
g[x][op|5] = sw; g[x][op] = sf;
}
return;
}
int lc = x<<1,rc = x<<1|1;
Dfs(lc,L+1); Dfs(rc,L+1);
for (int o = 0; o < (1<<L); o++)
{
int op = o<<n-L+2;
for (int A = 0; A < 2; A++)
for (int B = 0; B < 2; B++)
{
int lo = op|(A<<n-L+1),ro = op|(B<<n-L+1);
for (int i = 0; i <= (1<<n-L-1); i++)
for (int j = 0; j <= (1<<n-L-1); j++)
{
int k = i + j; if (k > m) continue;
g[x][op|k] = max(g[x][op|k],g[lc][lo|i] + g[rc][ro|j]);
}
}
}
}
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(); m = getint();
for (int i = 1; i <= (1<<n-1); i++)
for (int j = 1; j < n; j++)
{
int x = getint();
w[(1<<n-1)-1+i].push_back(x);
}
for (int i = 1; i <= (1<<n-1); i++)
for (int j = 1; j < n; j++)
{
int x = getint();
f[(1<<n-1)-1+i].push_back(x);
}
Dfs(1,1);
for (int o = 0; o < (1<<n+2); o++) Ans = max(Ans,g[1][o]);
cout << Ans;
return 0;
}