link。

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的更多相关文章

  1. Solution -「BZOJ3894」文理分科

    Sol. 说实话,对于一个初学者,这道题很难看出是一道网络流-最小割.对于一个熟练者,这是比较套路的一种模型. 最小割,可以看做是在一个图中删掉最小的边权和使得源点.汇点不连通.或者换一个角度,可以看 ...

  2. 「 Luogu P1231 」 教辅的组成

    题目大意 有 $\text{N1}$ 本书 $\text{N2}$本练习册 $\text{N3}$本答案,一本书只能和一本练习册和一本答案配对.给你一些书和练习册,书和答案的可能的配对关系.问你最多可 ...

  3. 「Luogu 1525」关押罪犯

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description \(S\)城现有两座监狱,一共关押着\(N\)名罪犯,编号分别为\(1 - N\) ...

  4. 「Luogu 2367」语文成绩

    更好的阅读体验 Portal Portal1: Luogu Description 语文老师总是写错成绩,所以当她修改成绩的时候,总是累得不行.她总是要一遍遍地给某些同学增加分数,又要注意最低分是多少 ...

  5. 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party

    更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...

  6. 「Luogu 1349」广义斐波那契数列

    更好的阅读体验 Portal Portal1: Luogu Description 广义的斐波那契数列是指形如\(an=p \times a_{n-1}+q \times a_{n-2}\)的数列.今 ...

  7. 「Luogu 3792」由乃与大母神原型和偶像崇拜

    更好的阅读体验 Portal Portal1: Luogu Description 给你一个序列\(a\) 每次两个操作: 修改\(x\)位置的值为\(y\): 查询区间\([l, r]\)是否可以重 ...

  8. 「Luogu P3866」[TJOI2009]战争游戏 解题报告

    题面 好难表述啊~ 在n*m的矩阵上,有一些大兵(为0),一些空地(一个正整数),障碍物(-1),现在摧毁一些空地,使所有大兵不能走出矩阵去(代价为表示空地的整数),求最小代价 思路: 网络流最小割 ...

  9. 「Luogu P2201」数列编辑器 解题报告

    数列编辑器,在线IDE 本期的主题是洛谷的在线IDE 小学生?!小学生虐我

  10. 「Luogu 1471」 方差

    题目背景 滚粗了的HansBug在收拾旧数学书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本数学书里面发现了一个神奇的数列,包含N个实数.他想算算这个数列的平均数和方差. 输入输出 ...

随机推荐

  1. 效率神器,边看网页边问ChatGPT!神级ChatGPT插件(浏览器扩展)推荐!

    如果在看一个网页时,有些词不认识.句子不知道含义,怎么办? 憨憨版:不认识就算了呗,还能咋滴 进阶版:复制到 Google/Baidu 里问一问: AI达人版:复制到 ChatGPT/Claude 里 ...

  2. MySQL 中分区表

    MySQL 中的分区表 InnoDB 逻辑存储结构 表空间 (Tablespace) 段 (segment) 区 (extent) 页 (page) 行 (row) InnoDB 数据页结构 分区别表 ...

  3. 多线程知识:三个线程如何交替打印ABC循环100次

    本文博主给大家讲解一道网上非常经典的多线程面试题目.关于三个线程如何交替打印ABC循环100次的问题. 下文实现代码都基于Java代码在单个JVM内实现. 问题描述 给定三个线程,分别命名为A.B.C ...

  4. 【小小Demo】微信公众号如何接入微信机器人

    微信对话开放平台文档 官方文档 平台简介 微信对话开放平台开放了微信在对话领域积累多年的的智能对话技术,开发者及非开发者可简单.快速地搭建智能对话机器人(智能客服), 并接入公众号.小程序等,为业务赋 ...

  5. Centos静默安装Oracle11G

    环境准备 Oracle 11gR2 64位 Linux版安装包 linux.x64_11gR2_database_1of2.zip linux.x64_11gR2_database_2of2.zip ...

  6. mysql根据.frm和.ibd文件恢复数据表

    忠人之事受人之托 起因是因为一位朋友的数据库服务器被重装了,只剩下一个zbp_post.frm和zbp_post.ibd文件.咨询我能不能恢复,确实我只用过mysqldump这种工具导出数据 然后进行 ...

  7. Trackbar调色板

    我们将会建立一个简单的应用,显示我们指定的颜色.将会建立一个窗口,显示三个trackbar指定RGB三个颜色通道值.可以滑动trackbar来改变相应的颜色.默认情况下,初始颜色为黑色. cv2.ge ...

  8. 解决:vue-loader was used without the corresponding plugin.

    原因 webpack经常出现版本不兼容问题,vue-loader在15以前的版本打包时候会自动生成VueLoaderPlugin,但是现在需要手动去wepack.config.js文件中去加入,如下图 ...

  9. CF1601 题解

    偶然看这一场的题目,忽然很有感觉,于是写了一下 A 题面 考虑每一位可以单独分开考虑 考虑单独的一位,每次要选 \(m\) 个位置,可能产生贡献的位置就是这位为 1 的数,设数量为 \(x\),则 \ ...

  10. cesium中限制地图浏览范围

    https://blog.csdn.net/qq_42740164/article/details/119375782?ops_request_misc=%257B%2522request%255Fi ...