「luogu - P4313」文理分科 Mincut
Pretty nice practice for the min-cut trick.
Starting out we eliminate the constraint that if five students in a edge-connected component alternatively choose exactly the same learning branch they get an extra contribution to the answer, and easily we can work it out with the following way to build a network and run a max-flow algorithm:
- Connect the source with each student and set the capacities of arcs as the number of euphorias the student would get if he or she chooses Arts.
- Connect each student with the sink and set capacities of arcs as the number of euphorias the student would get if he or she chooses Science.
Considering how to deal with the extra contributions, we simply shrink the four neighboring students and the student himself or herself into one point and connect the source with this taken the extra contributions (for choosing Arts) as the capacities. Additionally, connect the point with the four neighboring students taken \(+\infty\) as the capacities. So do we process ones who choose Science.
Just as the picture via @jun头吉吉 goes.

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
template<typename Kap> struct Net {
const int n;
struct Arc {
int to; size_t rev; Kap cap,flow;
};
vector<vector<Arc>> e;
vector<int> iter,lev;
queue<int,list<int>> q;
Net(int n):n(n),e(n),iter(n),lev(n) {}
void line(int one,int ano,Kap ca) {
one--; ano--;
assert(0<=one && one<n && 0<=ano && ano<n);
e[one].push_back((Arc){ano,e[ano].size()+(one==ano),ca,0});
e[ano].push_back((Arc){one,e[one].size()-1,0,0});
}
Kap dinitz(int s,int t) { return dinitz(s-1,t-1,numeric_limits<Kap>::max()); }
bool Getlayer(int s,int t) {
lev.assign(n,0); q.push(s); lev[s]=1;
while(q.size()) {
int now=q.front(); q.pop();
for(int i=0; i<int(e[now].size()); ++i) {
int y=e[now][i].to;
if(!lev[y] && e[now][i].cap>e[now][i].flow) lev[y]=lev[now]+1,q.push(y);
}
}
return lev[t];
}
Kap Augment(int now,Kap up,int t) {
if(now==t) return up;
Kap rlow=0;
for(int& i=iter[now]; i<int(e[now].size()); ++i) {
if(up==rlow) break;
int y=e[now][i].to;
if(lev[y]==lev[now]+1 && e[now][i].cap>e[now][i].flow) {
Kap f=Augment(y,min(up-rlow,e[now][i].cap-e[now][i].flow),t);
e[now][i].flow+=f; e[y][e[now][i].rev].flow-=f; rlow+=f;
}
}
if(up==rlow) lev[now]=0;
return rlow;
}
Kap dinitz(int s,int t,const Kap INF) {
assert(0<=s && s<n && 0<=t && t<n);
Kap res=0;
while(Getlayer(s,t)) iter.assign(n,0),res+=Augment(s,INF,t);
return res;
}
};
int n,m,a[200][200],b[200][200],exta[200][200],extb[200][200],S,T,sum;
int valid(int x,int y) { return x>=1 && x<=n && y>=1 && y<=m; }
int getID(int x,int y) { return (x-1)*m+y; }
int artify(int x) { return x+n*m; }
int sciencify(int x) { return x+n*m*2; }
signed main() {
scanf("%d %d",&n,&m);
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j) scanf("%d",&a[i][j]),sum+=a[i][j];
}
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j) scanf("%d",&b[i][j]),sum+=b[i][j];
}
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j) scanf("%d",&exta[i][j]),sum+=exta[i][j];
}
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j) scanf("%d",&extb[i][j]),sum+=extb[i][j];
}
Net<int> net(n*m*3+2); S=n*m*3+1; T=n*m*3+2;
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j) {
net.line(S,getID(i,j),a[i][j]);
net.line(getID(i,j),T,b[i][j]);
net.line(S,artify(getID(i,j)),exta[i][j]);
net.line(artify(getID(i,j)),getID(i,j),INF);
net.line(getID(i,j),sciencify(getID(i,j)),INF);
net.line(sciencify(getID(i,j)),T,extb[i][j]);
if(valid(i+1,j)) {
net.line(artify(getID(i,j)),getID(i+1,j),INF);
net.line(getID(i+1,j),sciencify(getID(i,j)),INF);
}
if(valid(i,j+1)) {
net.line(artify(getID(i,j)),getID(i,j+1),INF);
net.line(getID(i,j+1),sciencify(getID(i,j)),INF);
}
if(valid(i-1,j)) {
net.line(artify(getID(i,j)),getID(i-1,j),INF);
net.line(getID(i-1,j),sciencify(getID(i,j)),INF);
}
if(valid(i,j-1)) {
net.line(artify(getID(i,j)),getID(i,j-1),INF);
net.line(getID(i,j-1),sciencify(getID(i,j)),INF);
}
}
}
printf("%d\n",sum-net.dinitz(S,T));
return 0;
}
「luogu - P4313」文理分科 Mincut的更多相关文章
- Solution -「BZOJ3894」文理分科
Sol. 说实话,对于一个初学者,这道题很难看出是一道网络流-最小割.对于一个熟练者,这是比较套路的一种模型. 最小割,可以看做是在一个图中删掉最小的边权和使得源点.汇点不连通.或者换一个角度,可以看 ...
- 「 Luogu P1231 」 教辅的组成
题目大意 有 $\text{N1}$ 本书 $\text{N2}$本练习册 $\text{N3}$本答案,一本书只能和一本练习册和一本答案配对.给你一些书和练习册,书和答案的可能的配对关系.问你最多可 ...
- 「Luogu 1525」关押罪犯
更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description \(S\)城现有两座监狱,一共关押着\(N\)名罪犯,编号分别为\(1 - N\) ...
- 「Luogu 2367」语文成绩
更好的阅读体验 Portal Portal1: Luogu Description 语文老师总是写错成绩,所以当她修改成绩的时候,总是累得不行.她总是要一遍遍地给某些同学增加分数,又要注意最低分是多少 ...
- 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party
更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...
- 「Luogu 1349」广义斐波那契数列
更好的阅读体验 Portal Portal1: Luogu Description 广义的斐波那契数列是指形如\(an=p \times a_{n-1}+q \times a_{n-2}\)的数列.今 ...
- 「Luogu 3792」由乃与大母神原型和偶像崇拜
更好的阅读体验 Portal Portal1: Luogu Description 给你一个序列\(a\) 每次两个操作: 修改\(x\)位置的值为\(y\): 查询区间\([l, r]\)是否可以重 ...
- 「Luogu P3866」[TJOI2009]战争游戏 解题报告
题面 好难表述啊~ 在n*m的矩阵上,有一些大兵(为0),一些空地(一个正整数),障碍物(-1),现在摧毁一些空地,使所有大兵不能走出矩阵去(代价为表示空地的整数),求最小代价 思路: 网络流最小割 ...
- 「Luogu P2201」数列编辑器 解题报告
数列编辑器,在线IDE 本期的主题是洛谷的在线IDE 小学生?!小学生虐我
- 「Luogu 1471」 方差
题目背景 滚粗了的HansBug在收拾旧数学书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本数学书里面发现了一个神奇的数列,包含N个实数.他想算算这个数列的平均数和方差. 输入输出 ...
随机推荐
- 图扑虚拟现实 VR 智慧办公室可视化
前言 "虚拟现实"是来自英文"Virtual Reality",简称 VR 技术,其是通过利用计算机仿真系统模拟外界环境,主要模拟对象有环境.技能.传感设备和感 ...
- 3、数据库:Oracle部署 - 系统部署系列文章
Oracle数据库的安装,以前写过一篇,这次将新版的安装再记录一次,让读者能够有所了解,笔者也能够记录下最新版的安装过程. 一.数据库下载: Oracle最新版目前在官网是19c,从下面这个链接进去下 ...
- OSPF路由控制
实验拓扑 实验需求 公司A使用OSPF路由协议实现公司设备全网互通,后来公司A扩张兼并了公司B,要求将公司B采用的IS-IS路由协议与公司A的OSPF协议互相引入,使得相应部门可以实现互通. Rout ...
- .netcore中的虚拟文件EmbeddedFile
以前一直比较好奇像swagger,cap,skywalking等组件是如何实现引用一个dll即可在网页上展示界面的,难道这么多html,js,css等都是硬编码写死在代码文件中的?后面接触apb里面也 ...
- 【Java技术专题】「攻破技术盲区」带你攻破你很可能存在的Java技术盲点之动态性技术原理指南(反射技术专题)
@ 目录 带你攻破你很可能存在的Java技术盲点之动态性技术原理指南 编程语言的类型 静态类型语言 动态类型语言 技术核心方向 反射API 反射案例介绍 反射功能操作 获取构造器 长度可变的参数 - ...
- 如何取消Blazor Server烦人的重新连接?
如何取消Blazor Server烦人的重新连接? 相信很多Blazor的用户在开发内部系统上基本上都选择速度更快,加载更快的Blazor Server模式. 但是Blazor Server由于是Si ...
- 使用Git进行代码版本控制和协作:代码共享、协作和版本管理
目录 1. 引言 2. 技术原理及概念 3. 实现步骤与流程 使用 Git 进行代码版本控制和协作:代码共享.协作和版本管理 Git 是一个开源的分布式版本控制系统,由 Linux 内核开发组创建.G ...
- Python潮流周刊#9:如何在本地部署开源大语言模型?
你好,我是猫哥.这里每周分享优质的 Python 及通用技术内容,部分为英文,已在小标题注明.(标题取自其中一则分享,不代表全部内容都是该主题,特此声明.) 首发于我的博客:https://pytho ...
- 移动端APP组件化架构实践
前言 对于中大型移动端APP开发来讲,组件化是一种常用的项目架构方式.个人最近几年在工作项目中也一直使用组件化的方式来开发,在这过程中也积累了一些经验和思考.主要是来自在日常开发中使用组件化开发遇到的 ...
- CMOS信噪比与感光面积的关系
前言 一般情况下,相同分辨率的sensor,如果感光面积越大,则其单位像素的感光面积也越大,成像质量也会越好.即相同分辨率品质相当的sensor,2/3"的传感器成像质量一般情况就要优于1/ ...