法一
前置知识:位运算。
因为 都非常小,所以我们可以考虑枚举选取的红牌的集合,再看每一张蓝牌选取后是否让答案变大,也就是比较选取了的红牌中与这张蓝牌组成好对的数量与 之间的大小,如果能让答案变大那就需要选取这张蓝牌。因为确定了选取的红牌的集合后蓝牌之间的决策互不影响,所以这个算法是正确的。时间复杂度为 。如果在求解一个红牌的集合与一张蓝牌组成好对的数量时加上位运算的优化可以做到
无优化的代码:
#include<bits/stdc++.h>
#define LL long long
#define mes(s, x) memset(s, x, sizeof s)
#define Maxn 25
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
char c[Maxn][Maxn];
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int n = read(), m = read(), x = read(), y = read(), ans = 0, tmp, s;
for(int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
for(int i = 0; i < 1 << n; i++){//枚举子集
tmp = 0;
for(int j = 1; j <= n; j++) if((i >> (j - 1)) & 1) tmp -= x;
for(int k = 1; k <= m; k++){
s = 0;
for(int j = 1; j <= n; j++) if(((i >> (j - 1)) & 1) && c[j][k] == '1') s++;
if(s > y) tmp += s - y;
}
ans = max(tmp, ans);
}
printf("%d\n", ans);
return 0;
}有优化的代码:
#include<bits/stdc++.h>
#define LL long long
#define mes(s, x) memset(s, x, sizeof s)
#define Maxn 25
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
char c[Maxn][Maxn];
int a[Maxn];//与一张蓝牌组成好对的红牌集合
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int n = read(), m = read(), x = read(), y = read(), ans = 0, tmp, s;
for(int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
if(c[j][i] == '1') a[i] |= 1 << (j - 1);
}
}
for(int i = 0; i < 1 << n; i++){//枚举子集
tmp = 0;
for(int j = 1; j <= n; j++) if((i >> (j - 1)) & 1) tmp -= x;
for(int j = 1; j <= m; j++){
s = __builtin_popcount(i & a[j]);
if(s > y) tmp += s - y;
}
ans = max(tmp, ans);
}
printf("%d\n", ans);
return 0;
}法二
前置知识:最小割。
需要注意,网络流并不属于 NOI 大纲划定的入门级或提高级考察范围。
我们可以使用最小割算法解决这个问题,我们需要构造一个图使得其最小割等于为方案中未包含的好对数与选取的牌的代价之和的最小值(选红牌的代价为 ,选蓝牌的代价为 。)。好对的总数减去图的最小割即为答案。
我们把每张牌当成一个节点,再建立一个源点 和一个汇点 。钦定一组割的方案中与 在一个集合的节点为选取了的牌,那么首先每张牌要向 连一条容量为选这一张牌的代价的有向边。
在构造未包含的好对数时可以用二元关系最小割的方法分析。
考虑一个好对 ,只有在 同时属于 集合时选取的牌可以包含这一个好对,那么有:
解得 ,,。为了方便,我们可以直接令 ,。也就是说,对于每一个好对 ,我们需要从源点向 连一条容量为 的有向边,从 向 连一条容量为 的有向边。这样图就建完了。
注意到这一做法可以扩展到好对不是一红一蓝的情况,因为分析中并没有用到二分图的性质。
如果使用 dinic 算法求解最小割,那么时间复杂度不会超过 。
代码:
#include<bits/stdc++.h>
#define LL long long
#define mes(s, x) memset(s, x, sizeof s)
#define Maxn 50
#define Maxm 2005
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
int hd[Maxn], nxt[Maxm], v[Maxm], f[Maxm], cnt;
void addedge0(int x, int y, int z){
nxt[++cnt] = hd[x];
hd[x] = cnt;
v[cnt] = y;
f[cnt] = z;
}
void addedge(int x, int y, int z){
// printf("%d %d %d\n", x, y, z);
addedge0(x, y, z);
addedge0(y, x, 0);
}
void clear(){
mes(hd, -1);
cnt = -1;
}
int d[Maxn], cur[Maxn], lid, rid, s, t;
bool bfs(){
mes(d, -1);
d[s] = 0;
queue<int> q;
q.push(s);
int x, y;
while(q.size()){
x = q.front();
q.pop();
for(int j = hd[x]; j != -1; j = nxt[j]){
y = v[j];
if(f[j] && d[y] == -1){
d[y] = d[x] + 1;
q.push(y);
}
}
}
return d[t] != -1;
}
LL dfs(int i, LL a){
if(a == 0 || i == t) return a;
LL s = 0, x;
int j, y;
while(cur[i] != -1){
j = cur[i];
y = v[j];
if(d[i] + 1 == d[y]){
x = dfs(y, min(a, 1ll * f[j]));
a -= x;
s += x;
f[j] -= x;
f[j ^ 1] += x;
if(a == 0) break;
}
cur[i] = nxt[j];
}
return s;
}
LL dinic(){
LL sum = 0;
while(bfs()){
for(int i = lid; i <= rid; i++) cur[i] = hd[i];
sum += dfs(s, inf);
}
return sum;
}
char c[25][25];
int a[25];
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
clear();
int n = read(), m = read(), x = read(), y = read(), d = 0;
lid = s = 0, rid = t = n + m + 1;
for(int i = 1; i <= n; i++){
scanf("%s", c[i] + 1);
for(int j = 1; j <= m; j++){
if(c[i][j] == '1'){
a[i]++;
d++;
addedge(i, n + j, 1);
}
}
}
for(int i = 1; i <= n; i++){
addedge(s, i, a[i]);
addedge(i, t, x);
}
for(int i = 1; i <= m; i++){
addedge(n + i, t, y);
}
printf("%d\n", d - dinic());
return 0;
}