luoguP4313 文理分科
luoguP4313 文理分科
复习完之后做了道典型题目。
这道题条件有点多
我们逐个分析
如果没有\(sameart\)或者\(samescience\)的限制,就是一个裸的最大权闭合子图的问题了
但是再考虑有的话(其实还是一个最大权闭合子图)
很明显我们还是可以按照套路分成两个集合.
然后先按照权值分别划分\(S\),\(T\)集合
然后再向他要求的学生连一条\(\infty\)
放一张奇丑无比的图

感性理解一下就好了
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cctype>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 2e5 + 3;
const int M = 2e6 + 3;
const int INF = 2e9;
int cur[N],head[N],high[N];
struct edge{
int to;
int nxt;
int flow;
}e[M];
int n,m,tot = 1,s,t;
int ans = 0;
int dx[5] = {0,1,-1,0,0},dy[5] = {0,0,0,1,-1};
inline void add(int x,int y,int z){
e[++tot].to = y;
e[tot].flow = z;
e[tot].nxt = head[x];
head[x] = tot;
if(z < INF) ans += z;
}
inline int read(){
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline bool bfs(){
queue <int> q;
for(int i = 0;i <= t;++i) high[i] = 0;
q.push(s);high[s] = 1;
while(!q.empty()){
int k = q.front();q.pop();
for(int i = head[k];i;i = e[i].nxt){
int y = e[i].to;
if(!high[y] && e[i].flow > 0)
q.push(y),high[y] = high[k] + 1;
}
}
return high[t] != 0;
}
inline int dfs(int x,int dis){
if(x == t) return dis;
for(int &i = cur[x];i;i = e[i].nxt){
int y = e[i].to;
if(high[y] == high[x] + 1 && e[i].flow > 0){
int flow = dfs(y,min(dis,e[i].flow));
if(flow > 0){
e[i].flow -= flow;
e[i ^ 1].flow += flow;
return flow;
}
}
}
return 0;
}
inline int dinic(){
int res = 0;
while(bfs()){
for(int i = 0;i <= t;++i) cur[i] = head[i];
while(int now = dfs(s,INF)) res += now;
}
return res;
}
int main(){
n = read(),m = read();
s = 3 * n * m + 1,t = s + 1;
for(int i = 0;i < n;++i)
for(int j = 0;j < m;++j){
int x = read();
add(s,i * m + j,x);
add(i * m + j,s,0);
}
for(int i = 0;i < n;++i)
for(int j = 0;j < m;++j){
int x = read();
add(i * m + j,t,x);
add(t,i * m + j,0);
}
for(int i = 0;i < n;++i)
for(int j = 0;j < m;++j){
int x = i * m + j + n * m,y = x + n * m;
for(int k = 0;k < 5;++k){
int xx = i + dx[k],yy = j + dy[k];
if(xx < 0 || xx >= n || yy < 0 || yy >= m) continue;
add(x,xx * m + yy,INF);add(xx * m + yy,x,0);
add(xx * m + yy,y,INF);add(y,xx * m + yy,0);
// add(x,xx * n + yy,0);add(xx * n + yy,x,INF);
// add(xx * n + yy,y,0);add(y,xx * n + yy,INF);
}
}
for(int i = 0;i < n;++i)
for(int j = 0;j < m;++j){
int x = read();
add(s,i * m + j + n * m,x);
add(i * m + j + n * m,s,0);
}
for(int i = 0;i < n;++i)
for(int j = 0;j < m;++j){
int x = read();
add(i * m + j + 2 * n * m,t,x);
add(t,i * m + j + 2 * n * m,0);
}
// cout << ans << endl;
ans -= dinic();
printf("%d\n",ans);
return 0;
}
luoguP4313 文理分科的更多相关文章
- BZOJ3894/LuoguP4313 文理分科 (最小割)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- BZOJ 3894: 文理分科 [最小割]
3894: 文理分科 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 674 Solved: 392[Submit][Status][Discuss] ...
- Bzoj3894 文理分科
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 667 Solved: 389 Description 文理分科是一件很纠结的事情!(虽然看到这个题 ...
- bzoj 3894: 文理分科
Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行 描述,每个格子代表一个同学的座位. ...
- BZOJ_3894_文理分科&&BZOJ_2127_happiness_最小割
BZOJ_3894_文理分科_最小割 Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进 ...
- BZOJ3894文理分科——最小割
题目描述 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行 描述,每个格子代表一个同学的座位.每位同学必须从 ...
- 【BZOJ3894】文理分科
最小割劲啊 原题: 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行 描述,每个格子代表一个同学的座位.每位 ...
- 文理分科 BZOJ3894 & happiness BZOJ2127
分析: 最小割(一开始我没看出来...后来经过提点,大致理解...),不选则割的思想. 我们先这样考虑,将和选理相关的和S相连,与选文相关的和T相连,如果没有第二问,那么建图就是简单的S连cnt,cn ...
- 【BZOJ3894】文理分科(最小割)
[BZOJ3894]文理分科(最小割) 题面 BZOJ Description 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过) 小P所在的班级要进行文理分科.他的班级可以用一个 ...
随机推荐
- a configuration error occured during startup.please verify the preference field with the prompt:
Window->Preferences->MyEclipse Enterprice Workbench->Servers->Tomcat->选择你的Tomcat(比如To ...
- Ubuntu 如何编译安装第三方库
在工程应用中都会用到第三方库,标准库是在我们安装IDE环境或系统自带已经编译好的库,我们是可以直接调用的,而第三方库需要我们自己下载,编译和安装后才能使用,这里我们说的是Ubuntu如何使用cmake ...
- LeetCode115 Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. (Hard) A subse ...
- 巨蟒python全栈开发-第11阶段 ansible_project7
今日大纲 1.发布详情页面 2.前端页面获取分支信息 3.前端界面获取commit信息与tag信息 4.获取线上最新版本 5.发布之实现nginx下线 6.发布之实现server发布 7.前端页面按钮 ...
- 阿里云亮相2019联通合作伙伴大会,边缘计算等3款云产品助力5G时代产业数字化转型
4月23日,2019中国联通合作伙伴大会在上海正式开幕,本次大会以“合作不设限,共筑新生态”为主题,涉及5G.边缘计算.云计算.物联网.新媒体.人工智能.互联网化等各领域超过600家合作伙伴与3万名各 ...
- header发送Cookie
Cookie传达给客户端的原理 平时执行setcookie('key1', 'value1');这样的代码时,浏览器就会收到cookie并保存,但我们并不能从echo出去的内容中看到cookie内容 ...
- 06多次查询某区间内topk问题
题目描述:给定一个数组,需要多次查找不同区间内的,第k大或者第k小的元素. 考虑题目是多次查找,如果采用只对查询区间内的元素进行排序的思路,然后输出第k大的数的策略,那 ...
- 多版本python共存,安装三方库到指定python版本
多版本python安装过程略过不提提供完美解决python启动和各版本pip问题: python3下pip安装命令如下: py -3 -m pip install xxxxxx python2下pip ...
- supersockets服务器配置热更新
Keywords: 配置,热更新 此功能能够允许你在不重启服务器的前提下更新服务器实例的配置. (仅限1.6.5及其以上版本)
- 用CSS画平行四边形
<div class="jx">我是一个平行四边形</div> <style> .jx{ //定义div的高度宽度 transform:skew ...