$ \color{#0066ff}{ 题目描述 }$

小E在好友小W的家中发现一幅神奇的图画,对此颇有兴趣。它可以被看做一个包含N×M个像素的黑白图像,为了方便起见,我们用0表示白色像素,1表示黑色像素。小E认为这幅图画暗藏玄机,因此他记录下了这幅图像中每行、每列的黑色像素数量,以回去慢慢研究其中的奥妙。

有一天,小W不慎将图画打湿,原本的图像已经很难分辨。他十分着急,于是找来小E,希望共同还原这幅图画。根据打湿后的图画,他们无法确定真正的图像,然而可以推测出每个像素原本是黑色像素的概率Pij%。那么,一个完整的图像的出现概率就可以定义为:

其中Sij表示在还原后的图像中,像素是白色(0)还是黑色(1)。换句话说,一个完整图像出现概率就等于其所有黑色像素的出现概率之积。显然,图像的黑色像素不能包含概率为0的像素。

然而,小E对此也无能为力。因此他们找到了会编程的小F,也就是你,请你根据以上信息,告诉他们最有可能是原始图像的答案是什么。

\(\color{#0066ff}{输入格式}\)

输入文件image.in的第一行是两个正整数N和M,表示图像大小。

接下来N行每行包含M个整数,表示每个像素是黑色像素的概率为Pij%。0 ≤ Pij < 100。

接下来一行有N个非负整数,表示每一行中黑色像素的个数。

接下来一行有M个非负整数,表示每一列中黑色像素的个数。

\(\color{#0066ff}{输出格式}\)

输出文件image.out包含一个N×M的01矩阵,表示你还原出的图像。输出不包含空格。图像每行、每列中1的个数必须与输入一致,且是所有可能的图像中出现概率最大的一个。输入数据保证至少存在一个可能的图像。如果有多种最优图像,任意输出一种即可。

\(\color{#0066ff}{输入样例}\)

2 2
90 10
20 80
1 1
1 1

\(\color{#0066ff}{输出样例}\)

10
01

\(\color{#0066ff}{数据范围与提示}\)

样例解释:共有两种可能的图像:

01 10 和 10 01 前者的出现概率是0.1×0.2=0.02,后者的出现概率是0.9×0.8=0.72,故后者是最优图像。

对于20%的数据,N , M ≤ 5;

对于100%的数据,N , M ≤ 100。

\(\color{#0066ff}{题解}\)

一看数据范围,显然的费用流啦

但是注意这里是一个\(\prod\)

于是我们把边权变为概率

反向边稍微改变一下,就是它分之一,这样就满足了

然而。。。这题居然卡EK

于是得写ZKW费用流才能过qwq

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int inf = 0x7fffffff;
const int maxn = 5050;
struct node {
int to, can;
double dis;
node *nxt, *rev;
node(int to = 0, int can = 0, double dis = 0, node *nxt = NULL): to(to), can(can), dis(dis), nxt(nxt) {
rev = NULL;
}
};
std::deque<int> q;
node *head[maxn], *cur[maxn];
double dis[maxn];
const double eps = 1e-6;
bool choose[120][120];
bool vis[maxn];
int n, m, s, t;
LL ans;
void add(int from, int to, double dis, int can) {
head[from] = new node(to, can, dis, head[from]);
}
void link(int from, int to, double dis, int can) {
add(from, to, dis, can);
add(to, from, 1.0 / dis, 0);
head[from]->rev = head[to];
head[to]->rev = head[from];
}
bool spfa() {
for(int i = s; i <= t; i++) dis[i] = -1, vis[i] = false, cur[i] = head[i];
q.push_front(s);
dis[s] = 1.0;
while(!q.empty()) {
int tp = q.front(); q.pop_front();
vis[tp] = false;
for(node *i = head[tp]; i; i = i->nxt)
if(dis[i->to] < dis[tp] * i->dis && i->can) {
dis[i->to] = dis[tp] * i->dis;
if(!vis[i->to]) {
if(!q.empty() && dis[i->to] < dis[q.front()]) q.push_back(i->to);
else q.push_front(i->to);
vis[i->to] = true;
}
}
}
return dis[t] > 0;
}
int dfs(int x, int change) {
if(x == t || !change) return change;
int flow = 0, ls;
vis[x] = true;
for(node *i = cur[x]; i; i = i->nxt) {
cur[x] = i;
if(!vis[i->to] && fabs(dis[i->to] - (dis[x] * i->dis)) <= eps && (ls = dfs(i->to, std::min(i->can, change)))) {
flow += ls;
change -= ls;
i->can -= ls;
i->rev->can += ls;
if(!change) break;
}
}
vis[x] = false;
return flow;
}
void zkw() {
while(spfa()) dfs(s, inf);
for(int i = 1; i <= n; i++)
for(node *o = head[i]; o; o = o->nxt) {
if(o->to == s) continue;
if(!o->can) choose[i][o->to - n] = true;
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) printf("%d", choose[i][j]);
puts("");
}
} int main() {
n = in(), m = in(), s = 0, t = n + m + 1;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
int x = in();
if(x) link(i, n + j, x * 0.01, 1);
}
for(int i = 1; i <= n; i++) link(s, i, 1.0, in());
for(int i = 1; i <= m; i++) link(n + i, t, 1.0, in());
zkw();
return 0;
}

最新文章

  1. 关于tomcat小知识
  2. 提取数据库字段里面的值,并改变+图片懒加载,jquery延迟加载
  3. DataGridview 绑定泛型List&lt;T&gt;
  4. dij最短路优先队列堆的时候,加边
  5. 读写其他应用程序的SharedPreference
  6. Spring中依赖注入的使用和配置
  7. Axel linux下多线程下载工具
  8. 写给系统管理员的25个PHP安全实践
  9. Windows 7/8 创建WIFI热点
  10. codeforces C. DZY Loves Sequences
  11. NewSQL——优化的SQL存储引擎(TokuDB, MemSQL)+?
  12. MVC Razor视图引擎
  13. 基于visual Studio2013解决面试题之0401非递归遍历二叉树
  14. BSA Network Shell系列-scriptutil命令
  15. javascript中Date常用方法
  16. HDU2710-Max Factor-分解质因子
  17. 把存储过程结果集SELECT INTO到临时表
  18. Postgre SQL连接服务器失败
  19. 栈和堆(Stack &amp;&amp; Heap)
  20. Nand flash code

热门文章

  1. leetcode693
  2. 页面中CSS的四种引入方式的介绍与比较
  3. 将.sql文件导入powerdesigner的实现方法详解
  4. pom.xml配置指定仓库
  5. Subscript &amp; Inheritance
  6. 数论Keynote
  7. 数据结构》关于差分约束的两三事(BZOJ2330)
  8. 常用Xcode文档位置,修改Xcode项目模板地址总结,以及常用地址,随时更新。
  9. 686. Repeated String Match 字符串重复后的子字符串查找
  10. Win10 DHCP和Static IP 切换