At the start of the matrix game, we have an N x M matrix. Each grid has some balls.
The grid in (i,j) (0 ≤ i < N, 0 ≤ j < M) has Aij balls.
In each operation you can remove one ball from a grid or add one ball into a grid.
The goal of this game is to make each of the rows has the same number of balls and each of the columns has the same number of balls.
What is the minumun operations you should use?

输入描述:
The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
The first line of each test case contains two integers N and M (1 ≤ N,M ≤ 20).
The next N lines describe Aij, each line contains M integers. (0 ≤ Aij ≤ 20).

输出描述:
For each test case, print the case number and the answer.

输入
2
2 3
4 8 5
2 4 6
3 3
1 5 2
3 5 4
2 3 4

输出
Case 1: 7
Case 2: 7

说明
for the first example, the number of balls after operations is
4 6 5
6 4 5

题意

给你一个N*M的矩阵,有两个操作,每次对1个数+1或-1,问最少操作多少次使得每行和相同,每列和相同

题解

设每行X,每列Y,可知X*N=Y*M,每一行X/N,每一列Y/M,并且lcm(N,M)|X,0<=X<=20*n*m

然后可以枚举每一个X+=lcm(N,M),再用网络流判断

建图按行列建,S-每行流量X/N,T-每行流量Y/M,行列按a[i][j]建

跑出来最大流maxflow

+1,说明图中总共需要增加x-maxflow

-1,说明图中总共需要减掉多出来的sum-maxflow

代码

 #include<bits/stdc++.h>
using namespace std; const int maxn=1e5+;
const int maxm=2e5+;
const int INF=0x3f3f3f3f; int TO[maxm],CAP[maxm],NEXT[maxm],tote;
int FIR[maxn],gap[maxn],cur[maxn],d[maxn],q[];
int n,m,S,T; void add(int u,int v,int cap)
{
TO[tote]=v;
CAP[tote]=cap;
NEXT[tote]=FIR[u];
FIR[u]=tote++; TO[tote]=u;
CAP[tote]=;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
void bfs()
{
memset(gap,,sizeof gap);
memset(d,,sizeof d);
++gap[d[T]=];
for(int i=;i<=n;++i)cur[i]=FIR[i];
int head=,tail=;
q[]=T;
while(head<=tail)
{
int u=q[head++];
for(int v=FIR[u];v!=-;v=NEXT[v])
if(!d[TO[v]])
++gap[d[TO[v]]=d[u]+],q[++tail]=TO[v];
}
}
int dfs(int u,int fl)
{
if(u==T)return fl;
int flow=;
for(int &v=cur[u];v!=-;v=NEXT[v])
if(CAP[v]&&d[u]==d[TO[v]]+)
{
int Min=dfs(TO[v],min(fl,CAP[v]));
flow+=Min,fl-=Min,CAP[v]-=Min,CAP[v^]+=Min;
if(!fl)return flow;
}
if(!(--gap[d[u]]))d[S]=n+;
++gap[++d[u]],cur[u]=FIR[u];
return flow;
}
int ISAP()
{
bfs();
int ret=;
while(d[S]<=n)ret+=dfs(S,INF);
return ret;
}
void init()
{
tote=;
memset(FIR,-,sizeof FIR);
}
int main()
{
int t,N,M,ca=;
int a[][];
scanf("%d",&t);
while(t--)
{
int sum=;
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++)
for(int j=;j<=M;j++)
scanf("%d",&a[i][j]),sum+=a[i][j];
int up=N/__gcd(N,M)*M;
int minn=1e9;
S=N+M+,T=S+,n=T;
for(int x=;x<=*N*M;x+=up)
{
init();
for(int i=;i<=N;i++)add(S,i,x/N);
for(int i=;i<=M;i++)add(N+i,T,x/M);
for(int i=;i<=N;i++)
for(int j=;j<=M;j++)
add(i,N+j,a[i][j]);
minn=min(minn,x+sum-ISAP()*);
}
printf("Case %d: %d\n",ca++,minn);
}
return ;
}

上海大都会赛 I Matrix Game(最大流)的更多相关文章

  1. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it

    链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...

  2. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...

  3. 2018 ACM 国际大学生程序设计竞赛上海大都会赛

    传送门:2018 ACM 国际大学生程序设计竞赛上海大都会赛 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛2018-08-05 12:00:00 至 2018-08-05 17:00:0 ...

  4. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  5. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  6. 2018 ICPC上海大都会赛重现赛 D Thinking-Bear magic (几何)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 D Thinking-Bear magic (几何) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  7. 牛客 Fruit Ninja 2018 ACM 上海大都会赛 (随机化算法)

    题目链接:Fruit Ninja 比赛链接:2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 题目描述 Fruit Ninja is a juicy action game enjoyed ...

  8. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-K-Matrix Multiplication(矩阵乘法)

    题目描述 In mathematics, matrix multiplication or matrix product is a binary operation that produces a m ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位dp)

    题目链接:https://ac.nowcoder.com/acm/contest/163/J 题目大意:给定一个数N,求区间[1,N]中满足可以整除它各个数位之和的数的个数.(1 ≤ N ≤ 1012 ...

随机推荐

  1. 机器学习入门-文本特征-使用LDA主题模型构造标签 1.LatentDirichletAllocation(LDA用于构建主题模型) 2.LDA.components(输出各个词向量的权重值)

    函数说明 1.LDA(n_topics, max_iters, random_state)  用于构建LDA主题模型,将文本分成不同的主题 参数说明:n_topics 表示分为多少个主题, max_i ...

  2. Spring boot 自定义starter

    以下配置来自尚硅谷.. 常用如何配置 @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigur ...

  3. js人形时钟

    https://blog.csdn.net/rsylqc/article/details/44808063 分享自:http://chabudai.org/blog/?p=59 在这个网站看到一个很有 ...

  4. Django权限auth模块详解

    转自:http://www.cnblogs.com/Finley/p/5575305.html 1,auth模块是Django提供的标准权限管理系统,可以提供用户身份认证,用户组和权限管理 2,aut ...

  5. ACM__01背包,完全背包,多重背包

    今天写题的时候碰到了一道完全背包题,可是没有看出来,乱写了一通,浪费了一个晚上,顺便复习一下背包的知识 01背包 每种物品只能选择一次或者不选,求背包容量内的最大价值 先给出状态转移方程: f[i][ ...

  6. docker之container

    转自:https://www.cnblogs.com/jsonhc/p/7760144.html 运行一个container的本身就是开启一个具有独立namespace的进程 进程有自己的网络,文件系 ...

  7. 最邻近算法(KNN)识别数字验证码

    应用场景   对于简单的数字型验证码的自动识别.前期已经完成的工作是通过切割将验证码图片切割成一个一个的单个数字的图片,并按照对应的数字表征类别进行分类(即哪些图片表示数字7,哪些表示8),将各种数字 ...

  8. MySQL缓存分类和配置

    读书笔记,待补充完善 MySQL缓存分类 InnoDB缓冲池 InnoDB日志文件和MyIsAM数据的操作系统缓存 MyIsAM键缓存 查询缓存 无法手工配置的缓存,二进制日志,表定义文件的操作系统缓 ...

  9. 10.mysql-触发器.md

    目录 定义 语法 定义 当操作了某张表时,希望同时触发一些动作/行为,可以使用触发器完成 语法 -- 需求: 当向员工表插入一条记录时,希望mysql自动同时往日志表插入数据 -- 创建触发器(添加) ...

  10. 1.Java基础概念.md

    类 •类是Java的最小程序单位,所有程序必须放到类里. •Java中只可以有一个public类,且源文件名称必须和public类名称一致(建议) •Java的main方法: public stati ...