bzoj 3698 XWW的难题
内容
Description
XWW是个影响力很大的人,他有很多的追随者。这些追随者都想要加入XWW教成为XWW的教徒。但是这并不容易,需要通过XWW的考核。
XWW给你出了这么一个难题:XWW给你一个NN的正实数矩阵A,满足XWW性。
称一个NN的矩阵满足XWW性当且仅当:(1)A[N][N]=0;(2)矩阵中每行的最后一个元素等于该行前N-1个数的和;(3)矩阵中每列的最后一个元素等于该列前N-1个数的和。
现在你要给A中的数进行取整操作(可以是上取整或者下取整),使得最后的A矩阵仍然满足XWW性。同时XWW还要求A中的元素之和尽量大。
Input
第一行一个整数N,N ≤ 100。
接下来N行每行包含N个绝对值小于等于1000的实数,最多一位小数。
Output
输出一行,即取整后A矩阵的元素之和的最大值。无解输出No。
Sample Input
4
3.1 6.8 7.3 17.2
9.6 2.4 0.7 12.7
3.6 1.2 6.5 11.3
16.3 10.4 14.5 0
Sample Output
129
HINT
【数据规模与约定】
有10组数据,n的大小分别为10,20,30…100。
【样例说明】
样例中取整后满足XWW性的和最大的矩阵为:
3 7 8 18
10 3 0 13
4 1 7 12
17 11 15 0
题解
每一行每一列建一个点
对于每一行i,s->i,[a(i,n),a(i,n)+1]
对于每一列j,j->t,[a(n,j),a(n,j)+1]
对于每一个点(i,j),i->j,[a(i,j),a(i,j)+1]
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define N 100010
using namespace std;
const int inf = 0x3f3f3f3f;
int n, m, S, T, s, t, l, r, d[N];
struct flows {
struct node {
int to, next, flow;
} e[N];
int tot, st[N], dis[N], cur[N];
void init() {
tot = -1, memset(e, -1, sizeof e),
memset(st, -1, sizeof st);
memset(d, 0, sizeof d);
}
void add(int x, int y, int z) {
e[++tot].to = y;
e[tot].flow = z;
e[tot].next = st[x];
st[x] = tot;
}
void add_edge(int x, int y, int z) {
// printf("add %d %d %d\n", x, y, z);
add(x, y, z), add(y, x, 0);
}
queue <int> que;
int bfs(int S, int T) {
memcpy(cur, st, sizeof st);
memset(dis, 0, sizeof dis);
while (!que.empty()) que.pop();
que.push(S);
dis[S] = 1;
while(!que.empty()) {
int now = que.front();
que.pop();
for (int i = st[now]; ~i; i = e[i].next)
if (e[i].flow && !dis[e[i].to]) {
dis[e[i].to] = dis[now] + 1;
if (e[i].to == T) return 1;
que.push(e[i].to);
}
}
return 0;
}
int finds(int now, int T, int flow) {
if (now == T)
return flow;
int f;
for (int i = cur[now]; ~i; i = e[i].next) {
cur[now] = i;
if (e[i].flow && dis[e[i].to] == dis[now] + 1 &&
(f = finds(e[i].to, T, min(flow, e[i].flow)))) {
e[i].flow -= f;
e[i^1].flow += f;
return f;
}
}
return 0;
}
int dinic(int S, int T) {
int flow = 0, x;
while(bfs(S, T)) {
while(x = finds(S, T, inf)) {
flow += x;
}
}
return flow;
}
void del(int x) {
for (int i = st[x]; ~i; i = e[i].next)
e[i].flow = e[i ^ 1].flow = 0;
}
}flow;
int a[110][110], flag[110][110];
double x;
main() {
int in = 0, out = 0;
scanf("%d", &n);
flow.init();
s = n + n + 1, t = s + 1, S = t + 1, T = S + 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
scanf("%lf", &x), a[i][j] = (int)x;
int fx = floor(x), cx = ceil(x);
if (fx == cx)
flag[i][j] = 1;
}
for (int i = 1; i <= n; ++i) {
d[s] -= a[i][n], d[i] += a[i][n];
d[t] += a[n][i], d[i + n] -= a[n][i];
if (!flag[i][n])
flow.add_edge(s, i, 1);
if (!flag[n][i])
flow.add_edge(i, t, 1);
}
for (int i = 1; i < n; ++i)
for (int j = 1; j < n; ++j) {
d[i] -= a[i][j], d[j + n] += a[i][j];
if (!flag[i][j])
flow.add_edge(i, j + n, 1);
}
for (int i = 1; i <= t; ++i)
if (d[i] > 0)
flow.add_edge(S, i, d[i]), in += d[i];
else if (d[i] < 0)
flow.add_edge(i, T, -d[i]), out -= d[i];
flow.add_edge(t, s, inf);
if (in != out) {
puts("No");
return 0;
}
int ans = flow.dinic(S, T);
if (ans != in) {
puts("No");
return 0;
}
// flow.e[flow.tot - 1].flow = flow.e[flow.tot].flow = 0;
ans = flow.dinic(s, t);
printf("%d\n", ans * 3);
}