hdu3338 网络流 建图

来源:互联网 发布:python获取命令行参数 编辑:程序博客网 时间:2024/06/03 17:36

If you solved problem like this, forget it.Because you need to use a completely different algorithm to solve the following one. 
Kakuro puzzle is played on a grid of "black" and "white" cells. Apart from the top row and leftmost column which are entirely black, the grid has some amount of white cells which form "runs" and some amount of black cells. "Run" is a vertical or horizontal maximal one-lined block of adjacent white cells. Each row and column of the puzzle can contain more than one "run". Every white cell belongs to exactly two runs — one horizontal and one vertical run. Each horizontal "run" always has a number in the black half-cell to its immediate left, and each vertical "run" always has a number in the black half-cell immediately above it. These numbers are located in "black" cells and are called "clues".The rules of the puzzle are simple: 

1.place a single digit from 1 to 9 in each "white" cell 
2.for all runs, the sum of all digits in a "run" must match the clue associated with the "run" 

Given the grid, your task is to find a solution for the puzzle. 
                
        Picture of the first sample input            Picture of the first sample output
Input
The first line of input contains two integers n and m (2 ≤ n,m ≤ 100) — the number of rows and columns correspondingly. Each of the next n lines contains descriptions of m cells. Each cell description is one of the following 7-character strings: 

.......— "white" cell; 
XXXXXXX— "black" cell with no clues; 
AAA\BBB— "black" cell with one or two clues. AAA is either a 3-digit clue for the corresponding vertical run, or XXX if there is no associated vertical run. BBB is either a 3-digit clue for the corresponding horizontal run, or XXX if there is no associated horizontal run. 
The first row and the first column of the grid will never have any white cells. The given grid will have at least one "white" cell.It is guaranteed that the given puzzle has at least one solution.
Output
Print n lines to the output with m cells in each line. For every "black" cell print '_' (underscore), for every "white" cell print the corresponding digit from the solution. Delimit cells with a single space, so that each row consists of 2m-1 characters.If there are many solutions, you may output any of them.
Sample Input
6 6XXXXXXX XXXXXXX 028\XXX 017\XXX 028\XXX XXXXXXXXXXXXXX 022\022 ....... ....... ....... 010\XXXXXX\034 ....... ....... ....... ....... .......XXX\014 ....... ....... 016\013 ....... .......XXX\022 ....... ....... ....... ....... XXXXXXXXXXXXXX XXX\016 ....... ....... XXXXXXX XXXXXXX5 8XXXXXXX 001\XXX 020\XXX 027\XXX 021\XXX 028\XXX 014\XXX 024\XXXXXX\035 ....... ....... ....... ....... ....... ....... .......XXXXXXX 007\034 ....... ....... ....... ....... ....... .......XXX\043 ....... ....... ....... ....... ....... ....... .......XXX\030 ....... ....... ....... ....... ....... ....... XXXXXXX
Sample Output
_ _ _ _ _ __ _ 5 8 9 __ 7 6 9 8 4_ 6 8 _ 7 6_ 9 2 7 4 __ _ 7 9 _ __ _ _ _ _ _ _ __ 1 9 9 1 1 8 6_ _ 1 7 7 9 1 9_ 1 3 9 9 9 3 9_ 6 7 2 4 9 2 _

原数谜是个很有趣的游戏,如图,每一行或每一列空白称为一个回,每一回都对应着一个整数sum,sum就是这回的和。这些空白格里只能填入1—9这九个数字,且在每一回中不能重复。全黑色的格为空,有数字的格,左下角的表示列的和,右上角的表示行的和,则可以得到下面这个图。

但这道题不是原来的数谜,这题与原数谜相比,少了一点规则,就是,每一回中出现的数字可以重复。给你一个n * m 的图,让你填充一下。

看了这题,没有多少思路,借鉴一下解题报告的思想,这题可以用网络流做。以空白格为节点,假设流是从左流入,从上流出的,流入的容量为行和,流出来容量为列和,其余容量不变。求满足的最大流。由于流量有上下限限制,可以给每个数都减掉1,则填出来的数字范围为0—8, 就可以用单纯的网络流搞定了。求出来再加上就可以了。

建图:

一共有四类点:

1. 构造源点S,汇点T

2. 有行和的格子,此类节点设为A

3. 空白格,设为B

4. 有列和的格子,设为C

则可以建边:

1. <S, A> 容量和行和

2. <A, B> 容量为8

3. <B, C> 容量为8

4. <C, T> 容量为列和

当然,反向边容量都置为0。

题解转自:http://www.cnblogs.com/ylfdrib/archive/2010/08/15/1799903.html


代码:

#include <bits/stdc++.h>using namespace std;const int INF  = 0x3f3f3f3f;const int maxn = 20008;const int Mod  = 1e9 + 7;#define ll       long long#define mem(x,y) memset(x,y,sizeof(x))#define IO       ios_base::sync_with_stdio(0), cin.tie(0);inline ll gcd(ll a, ll b) {return a % b == 0 ? b : gcd(b, a % b);}inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}inline ll quick_pow(ll x, int k) {ll ans = 1; while (k) { if (k & 1) ans = (ans * x) % Mod; x = x * x % Mod;  k >>= 1; } return ans;}int dep[maxn];struct Node {    int  v, w, re_id;    Node() {};    Node(int a, int b, int c) {        v = a, w = b, re_id = c;    }};vector<Node> node[maxn];void addEdge(int u, int v, int w) {    node[u].push_back(Node(v, w, node[v].size()));    node[v].push_back(Node(u, 0, node[u].size() - 1));}int bfs(int s, int t) {    queue<int> Q;    mem(dep, -1);    dep[s] = 0;    Q.push(s);    while (!Q.empty()) {        int u = Q.front();        Q.pop();        for (int i = 0; i < node[u].size(); i++) {            int v = node[u][i].v;            if (node[u][i].w > 0 && dep[v] == -1) {                dep[v] = dep[u] + 1;                Q.push(v);            }        }    }    return dep[t] != -1;}int  dfs(int s, int t, int f) {    if (s == t || f == 0) return f;    int sumf = 0;    for (int i = 0; i < node[s].size(); i++) {        int v = node[s][i].v;        if (node[s][i].w > 0 && dep[v] == dep[s] + 1) {            int tmp = dfs(v, t, min(f, node[s][i].w));            if (tmp > 0) {                node[s][i].w -= tmp;                node[v][node[s][i].re_id].w += tmp;                sumf += tmp, f -= tmp;            }        }    }    if (sumf == 0) dep[s] = -1;    return sumf;}int dinic(int s, int t) {    int ans = 0;    while (bfs(s, t))  ans += dfs(s, t, INF);    return ans;}char s[101][101][9];int ans[101][101] = {0};int main() {    int n, m, ss = 0, tt = 20002;    while (cin >> n >> m) {        mem(ans, 0);        for (int i = 0; i < maxn; i++)node[i].clear();        for (int i = 1; i <= n; i++)            for (int j = 1; j <= m; j++)                scanf("%s", s[i][j]);        for (int i = 1; i <= n; i++)            for (int j = 1; j <= m; j++) {                if (strcmp(s[i][j], "XXXXXXX") == 0) continue;                if (strcmp(s[i][j], ".......") == 0) continue;                int a1 = (i - 1) * m + j, a2 = (i - 1) * m + j + 10000;                if (s[i][j][0] != 'X') {                    int c = (s[i][j][0] - '0') * 100 + (s[i][j][1] - '0') * 10 + s[i][j][2] - '0';                    int num = 0;                    for (int k = i + 1; k <= n; k++) {                        if (strcmp(s[k][j], ".......") != 0) break;                        int b = (k - 1) * m + j;                        addEdge(b, a2, 8);                        num++;                    }                    addEdge(a2, tt, c - num);                }                if (s[i][j][4] != 'X') {                    int c = (s[i][j][4] - '0') * 100 + (s[i][j][5] - '0') * 10 + s[i][j][6] - '0';                    int num = 0;                    for (int k = j + 1; k <= m; k++) {                        if (strcmp(s[i][k], ".......") != 0) break;                        int b = (i - 1) * m + k;                        addEdge(a1, b, 8);                        num++;                    }                    addEdge(ss, a1, c - num);                }            }                    dinic(ss, tt);        for (int i = 1; i <= n; i++)            for (int j = 1; j <= m; j++) {                if (strcmp(s[i][j], "XXXXXXX") == 0) continue;                if (strcmp(s[i][j], ".......") == 0) continue;                int a1 = (i - 1) * m + j, a2 = (i - 1) * m + j + 10000;                if (s[i][j][4] != 'X') {                    for (int k = j + 1; k <= m; k++) {                        if (strcmp(s[i][k], ".......") != 0) break;                        ans[i][k] = 8 - node[a1][k - j - 1].w + 1;                    }                }            }        for (int i = 1; i <= n; i++) {            for (int j = 1; j <= m; j++) {                if (ans[i][j]) cout << ans[i][j];                else cout << "_";                printf("%c", j == m ? '\n' : ' ' );            }        }    }}