BZOJ3894文理分科——最小割
题目描述
输入
输出
样例输入
13 2 4 13
7 13 8 12
18 17 0 5
8 13 15 4
11 3 8 11
11 18 6 5
1 2 3 4
4 2 3 2
3 1 0 4
3 2 3 2
0 2 2 1
0 2 4 4
样例输出
提示
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
int head[40000];
int to[300000];
int next[300000];
int val[300000];
int d[40000];
int q[40000];
int back[40000];
int S,T;
int x;
int n,m;
int tot=1;
int ans;
int dx[6]={0,1,0,-1,0};
int dy[6]={1,0,-1,0,0};
void add(int x,int y,int v)
{
tot++;
next[tot]=back[x];
back[x]=tot;
to[tot]=y;
val[tot]=v;
tot++;
next[tot]=back[y];
back[y]=tot;
to[tot]=x;
val[tot]=0;
}
bool bfs(int S,int T)
{
int r=0;
int l=0;
memset(d,-1,sizeof(d));
q[r++]=T;
d[T]=2;
while(l<r)
{
int now=q[l];
for(int i=back[now];i;i=next[i])
{
if(d[to[i]]==-1&&val[i^1]!=0)
{
d[to[i]]=d[now]+1;
q[r++]=to[i];
}
}
l++;
}
if(d[S]==-1)
{
return false;
}
else
{
return true;
}
}
int dfs(int x,int flow)
{
if(x==T)
{
return flow;
}
int now_flow;
int used=0;
for(int &i=head[x];i;i=next[i])
{
if(d[to[i]]==d[x]-1&&val[i]!=0)
{
now_flow=dfs(to[i],min(flow-used,val[i]));
val[i]-=now_flow;
val[i^1]+=now_flow;
used+=now_flow;
if(now_flow==flow)
{
return flow;
}
}
}
if(used==0)
{
d[x]=-1;
}
return used;
}
int dinic()
{
int res=0;
while(bfs(S,T))
{
memcpy(head,back,sizeof(back));
res+=dfs(S,0x3f3f3f3f);
}
return res;
}
int find(int x,int y)
{
return (x-1)*m+y;
}
int main()
{
scanf("%d%d",&n,&m);
S=n*m*3+1;
T=n*m*3+2;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&x);
add(S,find(i,j),x);
ans+=x;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&x);
add(find(i,j),T,x);
ans+=x;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&x);
ans+=x;
add(S,n*m+find(i,j),x);
for(int k=0;k<=4;k++)
{
int fx=i+dx[k],fy=j+dy[k];
if(fx>=1&&fx<=n&&fy>=1&&fy<=m)
{
add(n*m+find(i,j),find(fx,fy),INF);
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&x);
ans+=x;
add(2*n*m+find(i,j),T,x);
for(int k=0;k<=4;k++)
{
int fx=i+dx[k],fy=j+dy[k];
if(fx>=1&&fx<=n&&fy>=1&&fy<=m)
{
add(find(fx,fy),2*n*m+find(i,j),INF);
}
}
}
}
printf("%d",ans-dinic());
}
BZOJ3894文理分科——最小割的更多相关文章
- [BZOJ3894]文理分科(最小割)
(1) 对每个位置建一个点F1,S向这个点连art[i][j]的边,这个点向T连science[i][j]的边. (2) 对每个位置再建一个点F2,S向这个点连same_art[i][j]的边,这个点 ...
- 【BZOJ3894】文理分科 最小割
[BZOJ3894]文理分科 Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行描述,每个格 ...
- 【BZOJ3894】【Luogu3358】文理分科 - 最小割多选一模型
链接Click Here 这个题就是个板子的最小割多选一模型啦\(QwQ\),这里介绍一种通用的解法. 抛开组合收益不谈,这个题就是一个简单的最小割模型.我们只需要建出来这样一张图,在上面跑最小割,割 ...
- P4313 文理分科 最小割
$ \color{#0066ff}{ 题目描述 }$ 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行描述,每个格 ...
- BZOJ 3894: 文理分科 [最小割]
3894: 文理分科 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 674 Solved: 392[Submit][Status][Discuss] ...
- BZOJ 3894 Luogu P4313 文理分科 (最小割)
题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3894 (luogu) https://www.luogu.org/pro ...
- BZOJ3894/LuoguP4313 文理分科 (最小割)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- [bzoj3894]文理分科_网络流_最小割
文理分科 bzoj-3894 题目大意:题目链接. 注释:略. 想法: 这种题也是一种套路. 我们新建一个点表示收益点. 然后把所有的收益都加一起,求最小割表示代价即可. Code: #include ...
- [Bzoj3894]文理分科(最小割)
Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行描述,每个格子代表一个同学的座位.每位 ...
随机推荐
- Js获取当前页面URL各种参数
JS获取当前页面URL各种参数 一:Location Location 对象包含有关当前 URL 的信息. Location 对象是 Window 对象的一个部分,可通过 window.locatio ...
- eletron打包
https://www.cnblogs.com/BigJ/p/electron.html https://www.cnblogs.com/kakayang/p/9559777.html
- 开源后的.Net 如何选择使用
.NET是跨平台的开发栈.它有一个标准库,称为.NET Standard Library,其中包含了大量的APIs.这个标准库由各种.NET运行环境实现:.NET Framework..NET Co ...
- Dockerfile centos7_tomcat7.0.64_jdk7u80
FROM centos:7 MAINTAINER jiangzhehao WORKDIR /tmp RUN yum -y install net-tools ADD jdk-7u80-linux-x6 ...
- python三:循环语句练习--小白博客
# 打印0-10去掉5 count = - : count += : continue print(count) # 打印0-10的偶数 count = : print(count) count+= ...
- 使用insert ignore来避免向数据库重复插入数据
mysql中 insert ignore 的使用示例如下: INSERT IGNORE INTO `table_name` (`reportid`, `content`) VALUES (‘11111 ...
- 软件扒网站? 爬虫? F12查看源码? 查看网页源代码?浏览器sources? 区别和联系!
1.软件扒网站: 利用各类扒站网站,如仿站小工具8.0,可以按照规则将网站的未经浏览器简析的前端代码扒下来,并整理成css,js,html等文件夹,很方便.(当然看不到ajax等相关代码) 备注:如果 ...
- Echatrs 中PIE饼图中间位置怎么显示总数值?
title: { text: '总资产', subtext: '2000000.00', x: 'center', y: 'center' }图例:
- Java工具类——UUIDUtils
借用一下百度百科的解释,来看一下UUID是什么. UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Ope ...
- Pycharm中怎么给字典中的多个键-值对同时加上单引号
今天看了个爬虫视频,崔庆才讲师的免费视频, 里面一个批量给header加引号2s完成,这波操作让我眼前一亮. 最终还是发现了骚操作的背后手速是真的快. pycharm中按ctrl+r 勾选右上角的Re ...