标签:printf can enc 最大流最小割 rom inline nic 输出 line
文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过)
小P所在的班级要进行文理分科。他的班级可以用一个n*m的矩阵进行描述,每个格子代表一个同学的座位。每位同学必须从文科和理科中选择一科。同学们在选择科目的时候会获得一个满意值。满意值按如下的方式得到:
小P想知道,大家应该如何选择,才能使所有人的满意值之和最大。请告诉他这个最大值。
第一行为两个正整数:n,m
接下来n行m个整数,表示art[i][j];
接下来n行m个整数.表示science[i][j];
接下来n行m个整数,表示same_art[i][j];
接下来n行m个整数,表示same_science[i][j];
输出为一个整数,表示最大的满意值之和
3 4
13 2 4 13
7 13 8 12
18 17 0 5
8 13 15 4
11 3 8 11
11 18 6 5
1 2 3 4
4 2 3 2
3 1 0 4
3 2 3 2
0 2 2 1
0 2 4 4
152
1表示选择文科,0表示选择理科,方案如下:
1 0 0 1
0 1 0 0
1 0 0 0
N,M<=100,读入数据均<=500
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int inf = 0x7ffffff;
const int maxn = 1e5 + 10;
const int maxm = 120;
struct node {
int to, can;
node *nxt, *rev;
node(int to = 0, int can = 0, node *nxt = NULL): to(to), can(can), nxt(nxt) { rev = NULL; }
};
node *head[maxn], *cur[maxn];
int dep[maxn];
int wen[maxm][maxm], li[maxm][maxm], ww[maxm][maxm], ll[maxm][maxm];
int n, m, s, t, tot;
bool bfs() {
for(int i = s; i <= t; i++) dep[i] = 0, cur[i] = head[i];
std::queue<int> q;
dep[s] = 1;
q.push(s);
while(!q.empty()) {
int tp = q.front(); q.pop();
for(node *i = head[tp]; i; i = i->nxt)
if(!dep[i->to] && i->can)
dep[i->to] = dep[tp] + 1, q.push(i->to);
}
return dep[t];
}
int dfs(int x, int change) {
if(x == t || !change) return change;
int flow = 0, ls;
for(node *&i = cur[x]; i; i = i->nxt) {
if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(change, i->can)))) {
flow += ls;
change -= ls;
i->can -= ls;
i->rev->can += ls;
if(!change) break;
}
}
return flow;
}
int id(int x, int y) { return (x - 1) * m + y; }
void add(int from, int to, int can) {
head[from] = new node(to, can, head[from]);
}
void link(int from, int to, int can) {
add(from, to, can), add(to, from, 0);
(head[from]->rev = head[to])->rev = head[from];
}
int dinic() {
int flow = 0;
while(bfs()) flow += dfs(s, inf);
return flow;
}
int main() {
n = in(), m = in();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
wen[i][j] = in();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
li[i][j] = in();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
ww[i][j] = in();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
ll[i][j] = in();
int tot = 0;
s = 0, t = 3 * n * m + 1;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
tot += wen[i][j];
link(s, id(i, j), wen[i][j]);
tot += li[i][j];
link(id(i, j), t, li[i][j]);
}
int now = n * m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
now++;
link(s, now, ww[i][j]), tot += ww[i][j];
link(now, id(i, j), inf);
if(i != 1) link(now, id(i - 1, j), inf);
if(i != n) link(now, id(i + 1, j), inf);
if(j != 1) link(now, id(i, j - 1), inf);
if(j != m) link(now, id(i, j + 1), inf);
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
now++;
link(now, t, ll[i][j]), tot += ll[i][j];
link(id(i, j), now, inf);
if(i != 1) link(id(i - 1, j), now, inf);
if(i != n) link(id(i + 1, j), now, inf);
if(j != 1) link(id(i, j - 1), now, inf);
if(j != m) link(id(i, j + 1), now, inf);
}
printf("%d\n", tot - dinic());
return 0;
}
标签:printf can enc 最大流最小割 rom inline nic 输出 line
原文地址:https://www.cnblogs.com/olinr/p/10538742.html