Redraw Beautiful Drawings

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2909 Accepted Submission(s):
942

Problem Description
Alice and Bob are playing together. Alice is crazy
about art and she has visited many museums around the world. She has a good
memory and she can remember all drawings she has seen.

Today Alice
designs a game using these drawings in her memory. First, she matches K+1 colors
appears in the picture to K+1 different integers(from 0 to K). After that, she
slices the drawing into grids and there are N rows and M columns. Each grid has
an integer on it(from 0 to K) representing the color on the corresponding
position in the original drawing. Alice wants to share the wonderful drawings
with Bob and she tells Bob the size of the drawing, the number of different
colors, and the sum of integers on each row and each column. Bob has to redraw
the drawing with Alice's information. Unfortunately, somtimes, the information
Alice offers is wrong because of Alice's poor math. And sometimes, Bob can work
out multiple different drawings using the information Alice provides. Bob gets
confused and he needs your help. You have to tell Bob if Alice's information is
right and if her information is right you should also tell Bob whether he can
get a unique drawing.

 
Input
The input contains mutiple testcases.

For each
testcase, the first line contains three integers N(1 ≤ N ≤ 400) , M(1 ≤ M ≤ 400)
and K(1 ≤ K ≤ 40).
N integers are given in the second line representing the
sum of N rows.
M integers are given in the third line representing the sum of
M columns.

The input is terminated by EOF.

 
Output
For each testcase, if there is no solution for Bob,
output "Impossible" in one line(without the quotation mark); if there is only
one solution for Bob, output "Unique" in one line(without the quotation mark)
and output an N * M matrix in the following N lines representing Bob's unique
solution; if there are many ways for Bob to redraw the drawing, output "Not
Unique" in one line(without the quotation mark).
 
Sample Input
2 2 4
4 2
4 2
4 2 2
2 2 5 0
5 4
1 4 3
9
1 2 3 3
 
Sample Output
Not Unique
Impossible
Unique
1 2 3 3
 
题意:在N*M的表格内填入数字,使得每行每列相加等于对应的值,且填入的数字小于K值;
   如果不存在这样的表格,输出Impossible;如果存在多个符合条件的表格,输出Not     Unique,否则,如果表格唯一的话,输出Unique,并输出表格;
思路:网络流可以做,当然网络流+最大流更好!
 
 
网络流(687MS)
 
 
#include<iostream>//网络流
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#define maxn 1<<29
using namespace std;
struct edge
{
int from,to,cap,flow;
};
vector<int>g[];
vector<edge>edges;
int m,n,ma;
bool vis[];
int d[];
int cur[];
int fl[][];
bool cc[][];
void init()
{
edges.clear();
int mm=m+n+;
for(int i=;i<=mm;i++)g[i].clear();
}
void add(int u,int v,int c)
{
edges.push_back((edge){u,v,c,});
g[u].push_back(edges.size()-);
edges.push_back((edge){v,u,,});
g[v].push_back(edges.size()-);
}
bool bfs(int s,int t)
{
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty())
{
int u=q.front();
q.pop();
int size=g[u].size();
for(int i=;i<size;i++)
{
edge &e=edges[g[u][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[u]+;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int u,int t,int mi)
{
if(u==t||mi==)return mi;
int flow=,f;
int size=g[u].size();
for(int &i=cur[u];i<size;i++)
{
edge &e=edges[g[u][i]];
if(d[u]+==d[e.to]&&(f=dfs(e.to,t,min(mi,e.cap-e.flow)))>)
{
e.flow+=f;
edges[g[u][i]^].flow-=f;
flow+=f;
mi-=f;
if(mi==)break;
}
}
return flow;
}
int dinic(int s,int t)
{
int flow=;
while(bfs(s,t))
{
memset(cur,,sizeof(cur));
flow+=dfs(s,t,maxn);
}
return flow;
}
bool go()
{
for(int i=;i<=n;i++)
{
int size=g[i].size();
for(int j=;j<size;j++)
{
edge &e=edges[g[i][j]];
if(e.to>n&&e.to<=m+n)
{
//cout<<e.from<<" "<<e.to<<" "<<e.flow<<endl;
fl[i][e.to-n]=e.flow;
}
}
}
memset(cc,,sizeof(cc));
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
for(int k=j+;k<=m;k++)
{
bool v1=,v2=;
if(fl[i][j]!=ma&&fl[i][k]!=)
{
if(cc[k][j])return true;
v1=;
}
if(fl[i][j]!=&&fl[i][k]!=ma)
{
if(cc[j][k])return true;
v2=;
}
if(v1)cc[j][k]=;
if(v2)cc[k][j]=;
}
}
}
return false;
}
int main()
{
int u,v,c;
int s1,s2;
while(scanf("%d%d%d",&n,&m,&ma)!=EOF)
{
init();
s1=s2=;
for(int i=;i<=n;i++)
{
scanf("%d",&c);
add(,i,c);
s1+=c;
for(int j=;j<=m;j++)
{
add(i,n+j,ma);
}
}
for(int i=;i<=m;i++)
{
scanf("%d",&c);
add(n+i,m+n+,c);
s2+=c;
}
int ans=dinic(,m+n+);
if(ans!=s1||ans!=s2)printf("Impossible\n");
else if(go())printf("Not Unique\n");
else
{
printf("Unique\n");
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
printf("%d",fl[i][j]);
if(j==m)printf("\n");
else printf(" ");
}
}
}
}
return ;
}
 网络流+最大流(234MS)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <numeric>
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXV = MAXN << ;
const int MAXE = * MAXN * MAXN;
const int INF = 0x3f3f3f3f;
struct ISAP
{
int head[MAXV], cur[MAXV], gap[MAXV], dis[MAXV], pre[MAXV];
int to[MAXE], next[MAXE], flow[MAXE];
int n, ecnt, st, ed;
void init(int n)
{
this->n = n;
memset(head + , -, n * sizeof(int));
ecnt = ;
}
void add_edge(int u, int v, int c)
{
to[ecnt] = v;
flow[ecnt] = c;
next[ecnt] = head[u];
head[u] = ecnt++;
to[ecnt] = u;
flow[ecnt] = ;
next[ecnt] = head[v];
head[v] = ecnt++; }
void bfs()
{
memset(dis + , 0x3f, n * sizeof(int));
queue<int> que;
que.push(ed);
dis[ed] = ;
while(!que.empty())
{
int u = que.front();
que.pop();
gap[dis[u]]++;
for(int p = head[u]; ~p; p = next[p])
{
int v = to[p];
if(flow[p ^ ] && dis[u] + < dis[v])
{
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} int max_flow(int ss, int tt)
{
st = ss, ed = tt;
int ans = , minFlow = INF;
for(int i = ; i <= n; ++i)
{
cur[i] = head[i];
gap[i] = ; }
bfs();
int u = pre[st] = st;
while(dis[st] < n)
{
bool flag = false;
for(int &p = cur[u]; ~p; p = next[p])
{
int v = to[p];
if(flow[p] && dis[u] == dis[v] + )
{
flag = true;
minFlow = min(minFlow, flow[p]);
pre[v] = u;
u = v;
if(u == ed)
{
ans += minFlow;
while(u != st)
{
u = pre[u];
flow[cur[u]] -= minFlow;
flow[cur[u] ^ ] += minFlow; }
minFlow = INF; }
break; } }
if(flag) continue;
int minDis = n - ;
for(int p = head[u]; ~p; p = next[p])
{
int &v = to[p];
if(flow[p] && dis[v] < minDis)
{
minDis = dis[v];
cur[u] = p; }
}
if(--gap[dis[u]] == ) break;
++gap[dis[u] = minDis + ];
u = pre[u]; }
return ans; } int stk[MAXV], top;
bool sccno[MAXV], vis[MAXV];
bool dfs(int u, int f, bool flag)
{
vis[u] = true;
stk[top++] = u;
for(int p = head[u]; ~p; p = next[p]) if(flow[p])
{
int v = to[p];
if(v == f) continue;
if(!vis[v])
{
if(dfs(v, u, flow[p ^ ])) return true; }
else if(!sccno[v]) return true; }
if(!flag)
{
while(true)
{
int x = stk[--top];
sccno[x] = true;
if(x == u) break; } }
return false; }
bool acycle()
{
memset(sccno + , , n * sizeof(bool));
memset(vis + , , n * sizeof(bool));
top = ;
return dfs(ed, , ); }
} G;
int row[MAXN], col[MAXN];
int mat[MAXN][MAXN];
int n, m, k, ss, tt;
void solve()
{
int sumr = accumulate(row + , row + n + , );
int sumc = accumulate(col + , col + m + , );
if(sumr != sumc)
{
puts("Impossible");
return ; }
int res = G.max_flow(ss, tt);
if(res != sumc)
{
puts("Impossible");
return ; }
if(G.acycle())
{
puts("Not Unique"); }
else
{
puts("Unique");
for(int i = ; i <= n; ++i)
{
for(int j = ; j < m; ++j) printf("%d ", G.flow[mat[i][j]]);
printf("%d\n", G.flow[mat[i][m]]); } } }
int main()
{
while(scanf("%d%d%d", &n, &m, &k) != EOF)
{
for(int i = ; i <= n; ++i) scanf("%d", &row[i]);
for(int i = ; i <= m; ++i) scanf("%d", &col[i]);
ss = n + m + , tt = n + m + ;
G.init(tt);
for(int i = ; i <= n; ++i) G.add_edge(ss, i, row[i]);
for(int i = ; i <= m; ++i) G.add_edge(n + i, tt, col[i]);
for(int i = ; i <= n; ++i)
{
for(int j = ; j <= m; ++j)
{
mat[i][j] = G.ecnt ^ ;
G.add_edge(i, n + j, k); }
}
solve(); }
}

这类题目,这个作为模版哦!

类似的题目有hdu4975;解题报告;寻找&星空の孩子

Redraw Beautiful Drawings(hdu4888)网络流+最大流的更多相关文章

  1. hdu4888 Redraw Beautiful Drawings 最大流+判环

    hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/6553 ...

  2. HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

    Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  3. 【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】

    传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因 ...

  4. HDU Redraw Beautiful Drawings 推断最大流是否唯一解

    点击打开链接 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 ...

  5. HDU4888 Redraw Beautiful Drawings(最大流唯一性判定:残量网络删边判环)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4888 Description Alice and Bob are playing toget ...

  6. hdu4888 Redraw Beautiful Drawings(最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4888 题意:给一个矩阵没行的和和每列的和,问能否还原矩阵,如果可以还原解是否唯一,若唯一输出该矩阵. ...

  7. HDU 4888 Redraw Beautiful Drawings(最大流+判最大流网络是否唯一)

    Problem Description Alice and Bob are playing together. Alice is crazy about art and she has visited ...

  8. hdu 4888 2014多校第三场1002 Redraw Beautiful Drawings 网络流

    思路:一開始以为是高斯消元什么的.想让队友搞,结果队友说不好搞,可能是网络流.我恍然,思路立刻就有了. 我们建一个二部图.左边是行,右边是列,建个源点与行建边,容量是该行的和.列与新建的汇点建边.容量 ...

  9. hdu 4888 Redraw Beautiful Drawings 网络流

    题目链接 一个n*m的方格, 里面有<=k的数, 给出每一行所有数的和, 每一列所有数的和, 问你能否还原这个图, 如果能, 是否唯一, 如果唯一, 输出还原后的图. 首先对行列建边, 源点向行 ...

随机推荐

  1. 【新手向】阿里云上ubuntu+flask+gunicorn+nginx服务器部署(二)项目部署

    本项目实现的是类似于ins的图片分享网站.继续(一),当nginx的配置已修改好后,要在远程服务器上部署网站,只需要几个步骤: 1 前期准备 2 将运行网站的代码从github上下载过来 3 下载依赖 ...

  2. [Vue] vue-cli3.0安装

    1. node.js安装https://nodejs.org/en/download/ 2.npm的安装 由于新版的nodejs已经集成了npm,所以之前npm也一并安装好了.同样可以通过输入 &qu ...

  3. canvas 实现签名效果

    效果图 概述 在线签名,现在在很多场景下都能看到,而且在移动端见的比较多. 用canvas和svg都可以实现,而且跨平台能力也很好. canvas基于像素,提供 2D 绘制函数,提供的功能更原始,适合 ...

  4. 在ubuntu中我们使用sudo apt-get install 或者dpkg -i *.deb安装软件时,常常提示“有未能满足的依赖关系“,解决方法

    很早之前在ubuntu安装软件时遇到的问题,今天打开ubuntu看到了,总结如下: 在ubuntu中我们使用sudo apt-get install 或者dpkg -i *.deb安装软件常常提示“有 ...

  5. 上台阶问题(递归,DFS)

    题目 一共39层台阶.如果我每一步迈上1个台阶或者两个台阶,先迈左脚,再迈右脚,然后左右交换,最后一步迈右脚,也就是一共要走偶数步,那么,上完39级台阶,有多少种不同的方法? 思路 采用递归的思想,边 ...

  6. POJ 2656

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  7. 强烈鄙视那些:自己完全不用android手机,却在做android开发的人

    前言: 最近参加android技术交流会,看到几个同时用mac和iphone的人,以为他们是全栈工程师(android和ios都会) , 谁知道交流下来,居然只是做android开发的,ios根本不会 ...

  8. ThreadLocal管理Connection

    ThreadLocal管理Connection 每一个用户都对应有一个单独线程,每一个线程都有一个数据库连接对象Connection对象接待它. 一个用户对应一个线程,这个线程中的Connection ...

  9. ASP.NET Core 1.0 中使用 Log 日志配置

    https://github.com/aspnet/Logging https://docs.asp.net/en/latest/fundamentals/logging.html ASP.NET C ...

  10. 如何查找SHELL的进程号并杀死

    一.shell查找进程并杀死 #!/bin/sh tomcat_id=`ps -ef | grep tomcat | grep -v "grep" | awk '{print $2 ...