HDU 4888 Redraw Beautiful Drawings 网络流 建图
题意:
给定n, m, k
以下n个整数 a[n]
以下m个整数 b[n]
用数字[0,k]构造一个n*m的矩阵
若有唯一解则输出这个矩阵。若有多解输出Not Unique,若无解输出Impossible
思路:网络流,,。
n行当成n个点,m列当成m个点
从行-列连一条流量为k的边,然后源点-行连一条a[i]的边, 列-汇点 流量为b[i]
瞎了,该退役了 T^T
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; #define ll int #define N 1005
#define M 200000
#define inf 107374182
#define inf64 1152921504606846976
struct Edge{
ll from, to, cap, nex;
}edge[M*2];//注意这个一定要够大 不然会re 还有反向弧 ll head[N], edgenum;
void add(ll u, ll v, ll cap, ll rw = 0){ //假设是有向边则:add(u,v,cap); 假设是无向边则:add(u,v,cap,cap);
Edge E = { u, v, cap, head[u]};
edge[ edgenum ] = E;
head[u] = edgenum ++; Edge E2= { v, u, rw, head[v]};
edge[ edgenum ] = E2;
head[v] = edgenum ++;
}
ll sign[N];
bool BFS(ll from, ll to){
memset(sign, -1, sizeof(sign));
sign[from] = 0; queue<ll>q;
q.push(from);
while( !q.empty() ){
ll u = q.front(); q.pop();
for(ll i = head[u]; i!=-1; i = edge[i].nex)
{
ll v = edge[i].to;
if(sign[v]==-1 && edge[i].cap)
{
sign[v] = sign[u] + 1, q.push(v);
if(sign[to] != -1)return true;
}
}
}
return false;
}
ll Stack[N], top, cur[N];
ll Dinic(ll from, ll to){
ll ans = 0;
while( BFS(from, to) )
{
memcpy(cur, head, sizeof(head));
ll u = from; top = 0;
while(1)
{
if(u == to)
{
ll flow = inf, loc;//loc 表示 Stack 中 cap 最小的边
for(ll i = 0; i < top; i++)
if(flow > edge[ Stack[i] ].cap)
{
flow = edge[Stack[i]].cap;
loc = i;
} for(ll i = 0; i < top; i++)
{
edge[ Stack[i] ].cap -= flow;
edge[Stack[i]^1].cap += flow;
}
ans += flow;
top = loc;
u = edge[Stack[top]].from;
}
for(ll i = cur[u]; i!=-1; cur[u] = i = edge[i].nex)//cur[u] 表示u所在能增广的边的下标
if(edge[i].cap && (sign[u] + 1 == sign[ edge[i].to ]))break;
if(cur[u] != -1)
{
Stack[top++] = cur[u];
u = edge[ cur[u] ].to;
}
else
{
if( top == 0 )break;
sign[u] = -1;
u = edge[ Stack[--top] ].from;
}
}
}
return ans;
}
void init(){memset(head,-1,sizeof head);edgenum = 0;} int n, m, k;
int a[500], b[500], suma, sumb;
int mp[505][505], dou[505][505]; //dou[0][i] i这列存在一个可增的点
int hehe(){
if(suma != sumb)return -1;
init();
int from = 0, to = n+m + 10;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
add(i, n+j, k);
for(int i = 1; i <= n; i++)
add(from, i, a[i]);
for(int i = 1; i <= m; i++)
add(n+i, to, b[i]);
int flow = Dinic(from, to);
if(flow != suma) return -1;
int tt = 1;
for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++, tt+=2)
mp[i][j] = edge[tt].cap;
memset(dou, 0, sizeof dou);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
for(int z = j+1; z <= m; z++)
{
bool v1=0,v2=0;
if(mp[i][j]!=k&&mp[i][z]!=0)
{
if(dou[z][j])return 0;
v1=1;
}
if(mp[i][j]!=0&&mp[i][z]!=k)
{
if(dou[j][z])return 0;
v2=1;
}
if(v1)dou[j][z]=1;
if(v2)dou[z][j]=1;
} }
return 1;
}
void input(){
suma = sumb = 0;
for(int i = 1; i <= n; i++)scanf("%d",&a[i]), suma += a[i];
for(int i = 1; i <= m; i++)scanf("%d",&b[i]), sumb += b[i];
}
int main(){
int u, v, i, j;
while(~scanf("%d %d %d",&n,&m,&k)) {
input();
int ans = hehe();
if(ans == -1)puts("Impossible");
else if(ans == 0)puts("Not Unique");
else
{
puts("Unique");
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
printf("%d%c",mp[i][j],j==m?'\n':' ');
}
}
return 0;
}
/*
2 3 8
13 16
3 11 15 2 4 8
15 16
3 11 15 2 3 4 8
15 16 10
4 12 18 6 3 4 8
15 16 10
4 13 18 6 3 5 8
16 16 11
4 13 18 6 2 3 4 1
1 3 4
3 2 1 2 */
HDU 4888 Redraw Beautiful Drawings 网络流 建图的更多相关文章
- hdu 4888 Redraw Beautiful Drawings 网络流
题目链接 一个n*m的方格, 里面有<=k的数, 给出每一行所有数的和, 每一列所有数的和, 问你能否还原这个图, 如果能, 是否唯一, 如果唯一, 输出还原后的图. 首先对行列建边, 源点向行 ...
- 【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】
传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因 ...
- hdu 4888 Redraw Beautiful Drawings 最大流
好难好难,将行列当成X和Y,源汇点连接各自的X,Y集,容量为行列的和,相当于从源点流向每一行,然后分配流量给每一列,最后流入汇点,这样执意要推断最后是否满流,就知道有没有解,而解就是每一行流向每一列多 ...
- HDU 4888 Redraw Beautiful Drawings(最大流+判最大流网络是否唯一)
Problem Description Alice and Bob are playing together. Alice is crazy about art and she has visited ...
- hdu 4888 Redraw Beautiful Drawings(最大流,判环)
pid=4888">http://acm.hdu.edu.cn/showproblem.php?pid=4888 加入一个源点与汇点,建图例如以下: 1. 源点 -> 每一行相应 ...
- HDU 4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)
题意:给定n*m个格子,每个格子能填0-k 的整数.然后给出每列之和和每行之和,问有没有解,有的话是不是唯一解,是唯一解输出方案. 思路:网络流,一共 n+m+2个点 源点 到行连流量为 所给的 ...
- HDU 4888 Redraw Beautiful Drawings
网络流. $s$向每一个$r[i]$连边,容量为$r[i]$. 每一个$r[i]$向每一个$c[j]$连边,容量为$k$. 每一个$c[j]$向$t$连边容量为$c[j]$. 跑最大流,中间每一条边上 ...
- hdu 4888 2014多校第三场1002 Redraw Beautiful Drawings 网络流
思路:一開始以为是高斯消元什么的.想让队友搞,结果队友说不好搞,可能是网络流.我恍然,思路立刻就有了. 我们建一个二部图.左边是行,右边是列,建个源点与行建边,容量是该行的和.列与新建的汇点建边.容量 ...
- Redraw Beautiful Drawings(hdu4888)网络流+最大流
Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
随机推荐
- OCR识别-python3.5版
刚接触,啥子都不会,按着教程走 需求:识别图片中的文字信息环境:windows系统 开发语言:python3.5 使用工具类:1.pyocr 2.PIL 3.tesseract-ocr 步骤: 1.p ...
- nodejs mysql 执行多条sql语句
执行多条查询语句 为了安全起见,默认情况下是不允许执行多条查询语句的.要使用多条查询语句的功能,就需要在创建数据库连接的时候打开这一功能: var connection = mysql.createC ...
- Tomcat8 启动中提示 org.apache.catalina.webresources.Cache.getResource Unable to add the resource
Tomcat8 启动过程中提示: org.apache.catalina.webresources.Cache.getResource Unable to add the resource at xx ...
- .NetCore中如何实现权限控制 基于Claim角色、策略、基于Claim功能点处理
.NetCore中如果实现权限控制的问题,当我们访问到一个Action操作的时候,我们需要进行权限控制 基于claims 角色控制 基于角色控制总觉得范围有点过大,而且控制起来感觉也不是太好,举一个例 ...
- 为K8S集群建立只读权限帐号
参考URL: https://www.jianshu.com/p/a1a0d64f1245 https://mritd.me/2018/03/20/use-rbac-to-control-kubect ...
- Nginx + PHP(php-fpm)遇到的502 Bad Gateway错误
我一个统计程序估计要跑1分多钟以上 查看了一个php-fpm 配置文件 [13-Oct-2013 12:06:07] WARNING: [pool www] child 7458, script '/ ...
- 【转】Git命令大全(非常齐全)
$ git init // 初始化一个Git仓库$ git status // 查看仓库的状态$ git add . // 将所有修改添加到暂存区$ git add * // Ant风格添 ...
- 【Java】 二叉树的遍历(递归与循环+层序遍历)
在[Java] 大话数据结构(9) 树(二叉树.线索二叉树)一文中,已经实现了采用递归方法的前.中.后序遍历,本文补充了采用循环的实现方法.以及层序遍历并进行了一个总结. 递归实现 /* * 前序遍历 ...
- ThinkPHP V5.0 正式版发布
ThinkPHP5.0版本是一个颠覆和重构版本,官方团队历时十月,倾注了大量的时间和精力,采用全新的架构思想,引入了更多的PHP新特性,优化了核心,减少了依赖,实现了真正的惰性加载,支持compose ...
- java轻松实现无锁队列
1.什么是无锁(Lock-Free)编程 当谈及 Lock-Free 编程时,我们常将其概念与 Mutex(互斥) 或 Lock(锁) 联系在一起,描述要在编程中尽量少使用这些锁结构,降低线程间互相阻 ...