首页 > 其他分享 >HDU - 3338 Kakuro Extension(最大流 行列模型)

HDU - 3338 Kakuro Extension(最大流 行列模型)

时间:2023-04-07 11:07:37浏览次数:48  
标签:HDU Kakuro num Extension int cap flow edges row


题目大意:看一下图基本就知道了

解题思路:难点是构图。。
设置一个超级源点和所有的行的和相连,容量为该行的和 - 该行和由几个数相加得到,因为这里将0看成了,1看成了2,依此类推
设置一个超级汇点,和所有列的和相连,容量为该列的和 - 该列和由几个数相加得到,和上面一样
接着就是空白部分和 “行和“和 “列和“的关系了
空白连向该行的行和,权值为8
空白连向该列的列和,权值也为8
为什么为8,而不是9,因为流量也可能为0,但是0是不能填的,所以将容量设为8,最后取值的时候加1即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define N 100010
#define INF 0x3f3f3f3f

struct Edge {
    int from, to, cap, flow;
    Edge() {}
    Edge(int from, int to, int cap, int flow): from(from), to(to), cap(cap), flow(flow) {}
};

struct ISAP {
    int p[N], num[N], cur[N], d[N];
    int t, s, n, m;
    bool vis[N];

    vector<int> G[N];
    vector<Edge> edges;

    void init(int n) {
        this->n = n;
        for (int i = 0; i <= n; i++) {
            G[i].clear();
            d[i] = INF;
        }
        edges.clear();
    }

    void AddEdge(int from, int to, int cap) {
        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);
    }

    bool BFS() {
        memset(vis, 0, sizeof(vis));

        queue<int> Q;
        d[t] = 0;
        vis[t] = 1;
        Q.push(t);

        while (!Q.empty()) {
            int u = Q.front();
            Q.pop();

            for (int i = 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);
                }
            }
        }
        return vis[s];
    }

    int Augment() {
        int u = 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;
        }
        return flow;
    }

    int Maxflow(int s, int t) {
        this->s = s; this->t = t;
        int flow = 0;
        BFS();
        if (d[s] > n)
            return 0;

        memset(num, 0, sizeof(num));
        memset(cur, 0, sizeof(cur));
        for (int i = 0; i < n; i++)
            if (d[i] < INF)
                num[d[i]]++;
        int u = s;

        while (d[s] <= n) {
            if (u == t) {
                flow += Augment();
                u = s;
            }
            bool ok = false;
            for (int i = 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) {
                int Min = n;
                for (int i = 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;
            }
        }
        return flow;
    }
};

ISAP isap;
#define M 110

struct Node {
    int row, col, val;
    Node() {}
    Node(int row, int col, int val): row(row), col(col), val(val){}
}row[N], col[N];

int n, m;
int empty[M][M];
char map[M];

void solve() {
    int cnt_row = 0, cnt_col = 0, cnt_empty = 0;
    memset(empty, 0, sizeof(empty));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            scanf("%s", map);
            if (strcmp(map, ".......") == 0)
                empty[i][j] = ++cnt_empty;
            else if(map[3] == '\\') {
                int t;
                if (map[0] != 'X') {
                    t = (map[0] - '0') * 100 + (map[1] - '0') * 10 + (map[2] - '0');
                    col[++cnt_col] = Node(i, j, t);
                }
                if (map[4] != 'X') {
                    t = (map[4] - '0') * 100 + (map[5] - '0') * 10 + (map[6] - '0');
                    row[++cnt_row] = Node(i, j, t);
               }    
            }
        }
    int s = 0, t = cnt_empty + cnt_row + cnt_col + 1;
    isap.init(t);

    for (int i = 1; i <= cnt_row; i++) {
        int x = row[i].row;
        int cnt = 0;
        for (int y = 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 (int i = 1; i <= cnt_col; i++) {
        int y = col[i].col;
        int cnt = 0;
        for (int x = 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 (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (empty[i][j]) {
                int ans = 0;
                for (int k = 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");
    }
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        solve();
    }
    return 0;
}


标签:HDU,Kakuro,num,Extension,int,cap,flow,edges,row
From: https://blog.51cto.com/u_10970600/6175565

相关文章

  • HDU - 3081 Marriage Match II(二分图最大匹配 + 并查集)
    题目大意:有n个男生n个女生,现在要求将所有的男女配对,所有的男女都配对的话,算该轮游戏结束了。接着另一轮游戏开始,还是男女配对,但是配对的男女不能是之前配对过的。问这游戏能玩多少轮男女能配对的条件如下1.男女未曾吵架过。2.如果两个女的是朋友,那么另一个女的男朋友可以和......
  • HDU - 3715 Go Deeper (二分 + 2-SAT)
    题目大意:给出一个递归函数,问这个递归函数最多能递归几层解题思路:二分枚举递归层数,然后依此建边如果给出的c[i]为0时,那么x[a[i]]和x[b[i]]中的其中一个要为真,连边即为!a[i]->b[i],!b[i]->a[i]如果给出的c[i]为1时,那么x[a[i]]和x[b[i]]两个要么都为真,要么都为假,连边即为a[i]->b[i......
  • HDU - 1317 XYZZY (floyd + 最长路)
    题目大意:有一种游戏,游戏里面有N个房间,每个房间有相应的能量值,走入该房间就可以得到相应的能量值现在你要从房间1出发,走到房间N,如果中途能量耗尽了,就表示输了,反之,则为赢解题思路:首先得判断一下能不能到达N,这可以用Floyd去判断如果能直接走到N的话,就算赢,否则判断一下,看是否有正环......
  • HDU 5479 Scaena Felix(DP)
    题目大意:给出一系列的由’(‘和’)’组成的字符串,现有一种操作,可以将括号的方向变反,但需要花费1现在问,需要花费多少的代价才能使该字符串的任意一个连续子串都不存在”()”的配对解题思路:用dp[i][0]表示第i个位置变成’(‘的代价,dp[i][1]表示第i个位置变成’)’的代价如果当前......
  • hdu-5306(区间最值+线段树)
    hduGorgeousSequenceHDU-5306题意:给定一个长度为n的区间,做m次操作,三种操作对于序列[L,R]区间中的每个ai,用min(ai,x)替换。打印序列[L,R]区间的最大值打印序列[L,R]区间和因为区间和与区间最值无关,所以无法直接用简单的标记处理。区间最值与区间和如何扯上关系呢?我......
  • HDU 2196 Computer(树形DP) 入门模板
    ComputerTimeLimit:1000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):34313    AcceptedSubmission(s):5345 ProblemDescriptionAschoolboughtthefirstcomputersometimeago(sothiscomputer'sidis1).D......
  • HDU动态规划题解目录
    ProblemA:MaxSum(HDU1003)   点击这里ProblemB:CommonSubsequence(HDU1159)    点击这里ProblemC:SuperJumping!Jumping!Jumping!(HDU1087)    点击这里ProblemD:HumbleNumbers(HDU1058)   点击这里ProblemE:MonkeyandBanana(HDU1069)    点击这里ProblemF:......
  • hdu - 4578(线段树)
    题目:Yuanfangispuzzledwiththequestionbelow:Therearenintegers,a1,a2,…,an.Theinitialvaluesofthemare0.Therearefourkindsofoperations.Operation1:Addctoeachnumberbetweenaxandayinclusive.Inotherwords,dotransformationak&......
  • HDU 3328 Flipper 栈的应用
    FlipperTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):521    AcceptedSubmission(s):334ProblemDescriptionLittleBobbyRoberts(sonofBigBob,ofProblemG)playsthissolitairememory......
  • Firefox Tree Style Tab extension & hide native tabs
    地址栏空白处右键CustomizeToolbar...,勾选Titlebar安装TreeStyleTab扩展在地址栏输入about:config搜索toolkit.legacyUserProfileCustomizations.stylesheets改为true在地址栏输入about:support搜索ProfileDirectory,OpenDirectory打开路径新建chrome文件夹,文件夹下新建......