HDU-3338 Kakuro Extension (maximum flow)

Source: Internet
Author: User

The main idea of the topic: to see a basic

Problem solving idea: the difficulty is the composition.
Sets a super source point and all the rows and connections, and the capacity for that row and-the line and is added by several numbers to get
Sets a super sink, and all columns are concatenated, and the capacity is the sum of the columns-and the columns are added by a few numbers
Then there's the white space and the "Row and" and "column and" relationships.
Whitespace is connected to the line and, the weight value is 8
The blank is connected to the column's columns, and the weight value is 8.
Why is 8, not 9, because the traffic may also be 0, but 0 is not filled, so the capacity is set to 8, the last value of the time to add 1 can

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace STD;#define N 100010#define INF 0x3f3f3f3fstructEdge {intFrom, to, cap, flow; Edge () {} Edge (intFromintTo,intCapintFlow): From, to, Cap (CAP), flow (flow) {}};structISAP {intP[n], Num[n], cur[n], d[n];intT, S, N, M;BOOLVis[n]; vector<int>G[n]; vector<Edge>EdgesvoidInitintN) { This->n = n; for(inti =0; I <= N;            i++) {g[i].clear ();        D[i] = INF;    } edges.clear (); }voidAddedge (intFromintTo,intCAP) {Edges.push_back (Edge (from, to, Cap,0)); Edges.push_back (Edge (to, from,0,0));        m = Edges.size (); G[from].push_back (M-2); G[to].push_back (M-1); }BOOLBFS () {memset(Vis,0,sizeof(VIS)); Queue<int>Q; D[T] =0; VIS[T] =1; Q.push (t); while(! Q.empty ()) {intU = Q.front (); Q.pop (); for(inti =0; I < g[u].size (); i++) {Edge &e = edges[g[u][i] ^1];if(!vis[e.from] && e.cap > E.flow) {Vis[e.from] =true; D[e.from] = D[u] +1;                Q.push (E.from); }            }        }returnVis[s]; }intAugment () {intU = T, flow = INF; while(U! = s)            {Edge &e = edges[p[u]];            flow = min (flow, e.cap-e.flow);        u = edges[p[u]].from; } u = t; while(U! = s)            {Edges[p[u]].flow + = flow; Edges[p[u] ^1].flow-= flow;        u = edges[p[u]].from; }returnFlow }intMaxflow (intSintT) { This->s = s; This->t = t;intFlow =0; BFS ();if(D[s] > N)return 0;memset(Num,0,sizeof(num));memset(cur,0,sizeof(cur)); for(inti =0; I < n; i++)if(D[i] < INF) num[d[i]]++;intU = s; while(D[s] <= N) {if(U = = t)                {Flow + = Augment ();            U = s; }BOOLOK =false; for(inti = Cur[u]; I < g[u].size (); i++) {Edge &e = edges[g[u][i]];if(E.cap > E.flow && d[u] = = D[e.to] +1) {OK =true;                     P[e.to] = G[u][i];                    Cur[u] = i; u = e.to; Break; }            }if(!ok) {intMin = n; for(inti =0; I < g[u].size (); i++) {Edge &e = edges[g[u][i]];if(E.cap > E.flow)                min = min (min, d[e.to]); }if(--num[d[u]] = =0) Break; Num[d[u] = Min +1]++; Cur[u] =0;if(U! = s) u = edges[p[u]].from; }        }returnFlow }};isap ISAP;#define MstructNode {intRow, Col, Val; Node () {} node (intRowintColintval): Row (Row), col (Col), Val (val) {}}row[n], col[n];intN, M;intEMPTY[M][M];Char Map[M];voidSolve () {intCnt_row =0, Cnt_col =0, Cnt_empty =0;memset(Empty,0,sizeof(empty)); for(inti =1; I <= N; i++) for(intj =1; J <= M; J + +) {scanf('%s ',Map);if(strcmp(Map,".......") ==0) Empty[i][j] = ++cnt_empty;Else if(Map[3] ==' \ \ ') {intTif(Map[0] !=' X ') {T = (Map[0] -' 0 ') * -+ (Map[1] -' 0 ') *Ten+ (Map[2] -' 0 ');                Col[++cnt_col] = Node (i, J, T); }if(Map[4] !=' X ') {T = (Map[4] -' 0 ') * -+ (Map[5] -' 0 ') *Ten+ (Map[6] -' 0 ');               Row[++cnt_row] = Node (i, J, T); }                }        }ints =0, T = cnt_empty + cnt_row + cnt_col +1; Isap.init (t); for(inti =1; I <= Cnt_row; i++) {intx = Row[i].row;intCNT =0; for(inty = Row[i].col +1; Y <= m; y++) {if(Empty[x][y])                {cnt++; Isap. Addedge (i, Cnt_row + empty[x][y],8); }Else                 Break; } ISAP.    Addedge (S, I, row[i].val-cnt); } for(inti =1; I <= Cnt_col; i++) {inty = Col[i].col;intCNT =0; for(intx = Col[i].row +1; x <= N; X + +) {if(Empty[x][y])                {cnt++; Isap. Addedge (Cnt_row + empty[x][y], Cnt_row + cnt_empty + I,8); }Else                  Break; } ISAP.    Addedge (Cnt_row + cnt_empty + I, T, col[i].val-cnt); } ISAP. Maxflow (S, t); for(inti =1; I <= N; i++) { for(intj =1; J <= M; J + +) {if(Empty[i][j]) {intAns =0; for(intK =0; K < Isap. G[EMPTY[I][J] + cnt_row].size (); k++) {Edge &e = Isap.edges[isap. G[EMPTY[I][J] + cnt_row][k]];if(E.to > Cnt_row + cnt_empty) {ans + = E.flow; Break; }                }printf("%d", ans +1); }Else                printf("_ "); }printf("\ n"); }}intMain () { while(scanf("%d%d", &n, &m)! = EOF) {solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU-3338 Kakuro Extension (maximum flow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.