「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个实数.他想算算这个数列的平均数和方差. 输入输出 ...
随机推荐
- Python网页开发神器fac 0.2.9、fuc 0.1.29新版本更新内容介绍
fac项目地址:https://github.com/CNFeffery/feffery-antd-components fuc项目地址:https://github.com/CNFeffery/fe ...
- P1751 贪吃虫 题解
题意: 题目传送门 在一棵 n 个结点的树上,有 k 个贪吃虫去吃食物. 每个贪吃虫都走到达食物的唯一路径. 当一条贪吃虫通向食物的道路上有另一条贪吃虫,则较远的那只停止移动. 多条贪吃虫要进入同一节 ...
- Linux系统基础知识与自学方法
Linux系统基础知识与自学方法 大部分非计算机相关的朋友也经常使用电脑,所以我们频繁接触的是Windows系统.关于这个系统的评价不一,一部分人觉得简洁快捷,一部分人觉得问题(病毒.弹窗)多多,总之 ...
- 【.NET深呼吸】用代码写WPF控件模板
这一次咱们来探究一下怎么用纯代码写 WPF 模板.模板有个共同基类 FrameworkTemplate,数据模板.控件模板等是从此类派生的,因此,该类已定义了一些通用成员. 用代码构建模板,重要的成员 ...
- Mysql数据库体系化详细笔记(b站韩顺平)
Mysql数据库 一.数据库 使用命令行窗口连接MYSQL数据库 mysql服务启动,在cmd输入net start mysql 1.mysql -h主机名-Р端口-u用户名-p密码 2.登录前,保证 ...
- ShardingSphere5入门到实战
ShardingSphere5入门到实战 第01章 高性能架构模式 互联网业务兴起之后,海量用户加上海量数据的特点,单个数据库服务器已经难以满足业务需要,必须考虑数据库集群的方式来提升性能.高性能数据 ...
- Unity的IGenerateNativePluginsForAssemblies:深入解析与实用案例
Unity IGenerateNativePluginsForAssemblies Unity是一款非常流行的游戏引擎,它支持多种平台,包括Windows.Mac.Linux.Android.iOS等 ...
- 一文了解 io.Copy 函数
1. 引言 io.Copy 函数是一个非常好用的函数,能够非常方便得将数据进行拷贝.本文我们将从io.Copy 函数的基本定义出发,讲述其基本使用和实现原理,以及一些注意事项,基于此完成对io.Cop ...
- 【Python】Locust持续优化:InfluxDB与Grafana实现数据持久化与可视化分析
前言 在进行性能测试时,我们需要对测试结果进行监控和分析,以便于及时发现问题并进行优化. Locust在内存中维护了一个时间序列数据结构,用于存储每个事件的统计信息. 这个数据结构允许我们在Chart ...
- 最为常用的Laravel操作(2)-路由
基本路由 // 接收一个 URI 和一个闭包 Route::get('hello', function () { return 'Hello, Laravel'; }); // 支持的路由方法 Rou ...