HDU3338Kakuro Extension(最大流+边的流量)
来源:互联网 发布:java身份证校验 编辑:程序博客网 时间:2024/06/05 07:45
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2883
Kakuro Extension
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1986 Accepted Submission(s): 692
Special Judge
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
.......— "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.
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
_ _ _ _ _ __ _ 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这九个数字,每一回中可以重复。全黑色的格为空,有数字的格,左下角的表示列的和,右上角的表示行的和,则可以得到第二个图。
分析:
一个数等于若干个数的和,可以看做一条入流分解为若干条出流,入流量等于总的出流量。每个格子(i,j)可以由行和列两个坐标确定,所以可以建立行的点和列的点,代表i行的点向代表j列的点连接一条边(容量范围【1,9】)就是代表(i,j)这个格子。具有行总和的黑色格子,可以表示为流入行点的总流量,具有列总和的黑色格子,可以表示该列流出流量的和。某些行(列)可能有多个约束总和,我们可以将每个总和都看做单独一行(列),所以实际的行数和列数并不一定等于原图的。还要记录每个格子对应那一条边,最后流过那条边的总流量就是要填的数字。由于流量有上下限限制,可以给每个数都减掉1,对应的和也对应减去几,则填出来的数字范围为0—8, 就可以用单纯的网络流搞定了。求出来后再加上1就可以了,这样就没有下界需要处理了。
建图:
一共有四类点:
1. 构造源点S,汇点T
2. 有行和的格子,此类节点设为A
3. 空白格,设为B
4. 有列和的格子,设为C
则可以建边:
1. <S, A> 容量和行和
2. <A, B> 容量为8
3. <B, C> 容量为8
4. <C, T> 容量为列和
该题中对边需要求解实际流量的边进行记录,求出最大流后,则可以得到实际流量。
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<set>#include<map>using namespace std;#define PI acos(-1.0)#define eps 1e-8#define ll long long#define MEM(a, b) memset(a, b, sizeof(a))#define pb push_back#define MII map<int,int>::iterator#define MLL map<LL,LL>::iterator#define pii pair<int,int>#define SI set<int>::iterator#define SL set<LL>::iterator#define dug printf("bug-------bug-------bug\n")const int maxn = 10005;const int inf = 0x3f3f3f3f;struct Edge{ int to, cap, nxt; Edge(){} Edge(int t, int c, int nx):to(t), cap(c), nxt(nx){}};int head[maxn], tol;Edge edge[100*maxn];int n, m;int S, T;struct Val{ int fir, sec; int emp;};Val mp[105][105];int r[105][105], c[105][105], bel[105][105];int row[maxn], col[maxn];bool vis[maxn];void AddEdge(int u, int v, int cap){ edge[tol] = Edge(v, cap, head[u]); head[u] = tol++; edge[tol] = Edge(u, 0, head[v]); head[v] = tol++;}Val turn(char s[]){ Val ret; if(s[0] == 'X' || s[0] == '.') ret.fir = -1; else { int tmp = 0; for(int i = 0; i < 3; i++) tmp = tmp * 10 + s[i] - '0'; ret.fir = tmp; } if(s[4] == 'X' || s[4] == '.') ret.sec = -1; else { int tmp = 0; for(int i = 0; i < 3; i++) tmp = tmp * 10 + s[i+4] - '0'; ret.sec = tmp; } ret.emp = (s[0] == '.' && s[4] == '.'); return ret;}void init(){ tol = 0; memset(head, -1, sizeof(head)); char s[8]; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { scanf("%s", s); mp[i][j] = turn(s); } S = 0, T = n*m + 1; int rowc = 1, colc = 1; int cnt = 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { Val cur = mp[i][j]; if(cur.emp) continue; if(cur.sec != -1) { int ed = j + 1; while(ed <= m && mp[i][ed].emp) r[i][ed] = rowc, ed++; row[rowc] = cnt++; AddEdge(0, row[rowc++], cur.sec - (ed - j - 1)); } if(cur.fir != -1) { int ed = i + 1; while(ed <= n && mp[ed][j].emp) c[ed][j] = colc, ed++; col[colc] = cnt++; AddEdge(col[colc++], T, cur.fir - (ed - i - 1)); } } memset(bel, -1, sizeof(bel)); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(mp[i][j].emp) { AddEdge(row[r[i][j]], col[c[i][j]], 8); bel[i][j] = tol - 2; }}int d[maxn];bool BFS(){ queue<int> q; memset(vis, false, sizeof(vis)); vis[S] = true; d[S] = 0; q.push(S); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to, c = edge[i].cap; if(!vis[v] && c > 0) { vis[v] = true; d[v] = d[u] + 1; q.push(v); } } } return vis[T];}int cur[maxn];int dfs(int u, int T, int a){ if(u == T || a == 0) return a; int flow = 0, f; for(int &i = cur[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to, c = edge[i].cap, r = i^1; if(d[v] == d[u] + 1 && c > 0 && (f = dfs(v, T, min(c, a))) > 0) { edge[i].cap -= f; edge[r].cap += f; flow += f; a -= f; if(a == 0) break; } } return flow;}int dinic(int S, int T){ int ret = 0; while(BFS()) { for(int i = 0; i <= T; i++) cur[i] = head[i]; int f = inf; //while((f = dfs(S, T, f)) > 0) f = dfs(S, T, f); ret += f; } return ret;}int ans[maxn][maxn];int main(){ while(cin >> n >> m) { init(); int t = dinic(S, T); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { if(bel[i][j] == -1) { ans[i][j] = 0; continue; } ans[i][j] = 9 - edge[bel[i][j]].cap; } for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { if(!ans[i][j]) putchar('_'); else printf("%d", ans[i][j]); putchar(j == m ? '\n':' '); } } return 0;}
- HDU3338Kakuro Extension(最大流+边的流量)
- HDU3338Kakuro Extension(最大流,ISAP)建图是关键
- hdoj 3338 Kakuro Extension 【经典最大流+输出流量】
- 【POJ3281】【最大流】【加边限制流量】
- 【最大流】【HDU3338】【Kakuro Extension】
- HDU3338 Kakuro Extension(最大流)
- 流量公司的最大利润
- poj 2135 有流量限制的最小费用最大流
- 最大流最小割问题的流量计算问题
- hdu 3338 Kakuro Extension(最大流)
- hdu 3338 Kakuro Extension(最大流)
- hdu 3338 Kakuro Extension(最大流)
- 最大流-HDU-3338-Kakuro Extension
- Uva 11248 求只修改一条边的流量使得最大流>c 的所有可行边
- poj 3281 网络流最大流量
- ACDream 1171 最大费用流,流量不一定最大
- hdu3338 神奇网络流(最大流) Kakuro Extension
- 网络流_hdu3338Kakuro Extension (神奇最大流 Dinic)
- ActiveMQ初实践之【一】——入门实例
- dpdk-hugepage源码分析
- PHP防止SQL注入方法
- 深度学习检测方法梳理
- Spring aop事务异常回滚(rollbackFor)机制和源码分析
- HDU3338Kakuro Extension(最大流+边的流量)
- Ubuntu14.04+CUDA8.0+GTX1080+caffe配置教程
- POJ 3096 Surprising Strings 可能会
- LeetCode:9.Palindrome Number[Easy]
- IOS之UITabBarViewController用法
- Ubuntu14下repo的下载及Android源码下载
- 找工作之路2
- subid和slotid
- iFun Factory的 游戏服务器