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浏览器插件分享

    大家好,我是卷了又没卷,薛定谔的卷的AI算法工程师「陈城南」~ 担任某大厂的算法工程师,带来最新的前沿AI知识和工具,欢迎大家交流~,后续我还会分享更多 AI 有趣工具和实用玩法,包括AI相关技术.C ...

  2. 前端学习C语言 - 数组和字节序

    数组 本篇主要介绍:一维二维数组.字符数组.数组名和初始化注意点以及字节序. 一维数组 初始化 有以下几种方式对数组初始化: // 定义一个有5个元素的数组,未初始化 int a[5]; // 定义一 ...

  3. Rust 过程宏 proc-macro 是个啥

    定义一个 procedural macro 新建一个 lib 类型的 crate: cargo new hello-macro --lib procedural macros 只能在 proc-mac ...

  4. 2023-07-04:给定一个数组A, 把它分成两个数组B和C 对于数组A每个i位置的数来说, A[i] = B[i] + C[i] 也就是一个数字分成两份,然后各自进入B和C 要求B[i], C[i

    2023-07-04:给定一个数组A, 把它分成两个数组B和C 对于数组A每个i位置的数来说, A[i] = B[i] + C[i] 也就是一个数字分成两份,然后各自进入B和C 要求B[i], C[i ...

  5. 特性介绍 | MySQL测试框架 MTR 系列教程(四):语法篇

    作者:卢文双 资深数据库内核研发 序言: 以前对 MySQL 测试框架 MTR 的使用,主要集中于 SQL 正确性验证.近期由于工作需要,深入了解了 MTR 的方方面面,发现 MTR 的能力不仅限于此 ...

  6. 搭建Vue脚手架(vue-cli)

    windows下环境安装前置环境 node.js安装 https://nodejs.org/en/download/ 安装成功后打开cmd 输入如下,如果能看到node和npm的版本号了,说明已经安装 ...

  7. SRS之StateThreads学习

    最近在看SRS的源码.SRS是基于协程开发的,底层使用了StateThreads.所以为了充分的理解SRS源码,需要先学习一下StateThreads.这里对StateThreads的学习做了一些总结 ...

  8. ZEGO全新语音聊天室方案,2小时复刻 Clubhouse

    真的火了! 新晋带货王马斯克在 Clubhouse"开房"之后,直接让 Clubhouse 爆火出圈,据说,Clubhouse 平台邀请码现在在ebay上已经卖到了快200刀一个. ...

  9. Hexo博客使用valine评论系统无效果及终极解决方案

    注意事项 有一些博主valine评论系统无效果,有一些原因: 1.很大程度是因为next的版本升级导致某些参数设置不同 2.valine评论是基于LeanCloud,还有一个文章阅读次数功能也是用Le ...

  10. 2023年icpc大学生程序设计竞赛-nhr

    icpc的省赛是在洛阳举办,第一次出省,还是两天,第一次离开郑州去别的城市比赛,心情更多的是激动,非常感谢老师给了这次机会,第一天20号,打完热身赛之后回寝室,和队友一起看了一下去年省赛的题,感觉还是 ...