HDU 3338 Kakuro Extension
题意:完成如图的游戏,填充数字1-9
思路:网络流的行列模型,把每行每列连续的一段拆分出来建图即可,然后题目有限制一个下限1,所以 每行每列的容量减去相应的数字,然后建图建容量8就好,这样就默认原来容量已经有1了
代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXNODE = 10005;
const int MAXEDGE = 500005;
typedef int Type;
const Type INF = 0x3f3f3f3f;
struct Edge {
int u, v;
Type cap, flow;
Edge() {}
Edge(int u, int v, Type cap, Type flow) {
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
}
};
struct Dinic {
int n, m, s, t;
Edge edges[MAXEDGE];
int first[MAXNODE];
int next[MAXEDGE];
bool vis[MAXNODE];
Type d[MAXNODE];
int cur[MAXNODE];
vector<int> cut;
void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
}
void add_Edge(int u, int v, Type cap) {
edges[m] = Edge(u, v, cap, 0);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0);
next[m] = first[v];
first[v] = m++;
}
bool bfs() {
memset(vis, false, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = true;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = first[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (!vis[e.v] && e.cap > e.flow) {
vis[e.v] = true;
d[e.v] = d[u] + 1;
Q.push(e.v);
}
}
}
return vis[t];
}
Type dfs(int u, Type a) {
if (u == t || a == 0) return a;
Type flow = 0, f;
for (int &i = cur[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[i^1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
Type Maxflow(int s, int t) {
this->s = s; this->t = t;
Type flow = 0;
while (bfs()) {
for (int i = 0; i < n; i++)
cur[i] = first[i];
flow += dfs(s, INF);
}
return flow;
}
void MinCut() {
cut.clear();
for (int i = 0; i < m; i += 2) {
if (vis[edges[i].u] && !vis[edges[i].v])
cut.push_back(i);
}
}
} gao;
const int N = 105;
int n, m, down[N][N], right[N][N];
int tox[N][N], toy[N][N], xh[N * N], yh[N * N], xr[N * N], yr[N * N];
int xn, yn;
char str[N];
void build(int x, int y) {
scanf("%s", str);
if (str[0] == 'X') down[x][y] = -2;
else if (str[0] == '.') down[x][y] = -1;
else {
int num = (str[0] - '0') * 100 + (str[1] - '0') * 10 + str[2] - '0';
down[x][y] = num;
}
if (str[4] == 'X') right[x][y] = -2;
else if (str[4] == '.') right[x][y] = -1;
else {
int num = (str[4] - '0') * 100 + (str[5] - '0') * 10 + str[6] - '0';
right[x][y] = num;
}
}
int out[N][N];
int main() {
while (~scanf("%d%d", &n, &m)) {
xn = yn = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
build(i, j);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (right[i][j] >= 0)
xh[++xn] = right[i][j];
else if (right[i][j] == -1) {
xh[xn]--;
tox[i][j] = xn;
xr[xn] = i;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (down[j][i] >= 0)
yh[++yn] = down[j][i];
else if (down[j][i] == -1) {
yh[yn]--;
toy[j][i] = yn;
yr[yn] = i;
}
}
}
gao.init(xn + yn + 2);
for (int i = 1; i <= xn; i++) gao.add_Edge(0, i, xh[i]);
for (int i = 1; i <= yn; i++) gao.add_Edge(xn + i, xn + yn + 1, yh[i]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (right[i][j] == -1)
gao.add_Edge(tox[i][j], toy[i][j] + xn, 8);
}
}
gao.Maxflow(0, xn + yn + 1);
for (int i = 0; i < gao.m; i += 2) {
if (gao.edges[i].u == 0 || gao.edges[i].v == xn + yn + 1) continue;
out[xr[gao.edges[i].u]][yr[gao.edges[i].v - xn]] = gao.edges[i].flow;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (right[i][j] == -1) printf("%d", out[i][j] + 1);
else printf("_");
printf("%c", j == m - 1 ? '\n' : ' ');
}
printf("\n");
}
}
return 0;
}