HDU 2732 Leapin’ Lizards (最大流)

题意:一张网格图,有N个柱子,有些柱子上有蜥蜴,有的没有,每只蜥蜴一次最多跳d距离,一个柱子只能被跳有限次,问最后有几只蜥蜴跳不出来网格。

题解:最大流
拆点,将每个点拆成进来和出去两个。因为这里是每个点有流量,这个很关键。
对于每一个柱子 ( i , j ) , i d = ( i − 1 ) ∗ c o l + j (i,j),id = (i - 1) * col + j ijid=(i1)col+j
maze[id][id + row * col] = 能跳的次数,表示跳出去。
maze[id + row * col][T] = inf,表示跳出网格到汇点。
maze[id + row * col][id2] = inf,表示跳出来的点再跳到周围距离为d的点。

对于每一个蜥蜴 ( i , j ) , i d = ( i − 1 ) ∗ c o l + j (i,j),id = (i - 1) * col + j ijid=(i1)col+j
maze[0][id] = 1,表示从源点出发。

这里用的sap。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f;
/*
 * SAP 邻接矩阵形式
 * 点的编号从 0 开始
 * 增加个 flow 数组,保留原矩阵 maze, 可用于多次使用最大流
 */
const int MAXN = 1100;
int maze[MAXN][MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int flow[MAXN][MAXN];//存最大流的流量
int sap(int start, int end, int nodenum) {
    memset(cur, 0, sizeof(cur));
    memset(dis, 0, sizeof(dis));
    memset(gap, 0, sizeof(gap));
    memset(flow, 0, sizeof(flow));
    int u = pre[start] = start, maxflow = 0, aug = -1;
    gap[0] = nodenum;
    while (dis[start] < nodenum) {
    loop:
        for (int v = cur[u]; v < nodenum; v++)
            if (maze[u][v] - flow[u][v] && dis[u] == dis[v] + 1) {
                if (aug == -1 || aug > maze[u][v] - flow[u][v]) aug = maze[u][v] - flow[u][v];
                pre[v] = u;
                u = cur[u] = v;
                if (v == end) {
                    maxflow += aug;
                    for (u = pre[u]; v != start; v = u, u = pre[u]) {
                        flow[u][v] += aug;
                        flow[v][u] -= aug;
                    }
                    aug = -1;
                }
                goto loop;
            }
        int mindis = nodenum - 1;
        for (int v = 0; v < nodenum; v++)
            if (maze[u][v] - flow[u][v] && mindis > dis[v]) {
                cur[u] = v;
                mindis = dis[v];
            }
        if ((--gap[dis[u]]) == 0) break;
        gap[dis[u] = mindis + 1]++;
        u = pre[u];
    }
    return maxflow;
}
int t, row, col, d;
char pil[MAXN][22], liz[MAXN][22];
int main() {
    scanf("%d", &t);
    int cas = 0;
    while (t--) {
        memset(maze, 0, sizeof(maze));
        scanf("%d%d", &row, &d);
        for (int i = 1; i <= row; i++) {
            scanf("%s", pil[i] + 1);
            if(i == 1) col = strlen(pil[1] + 1);
            for (int j = 1; j <= col; j++) {
                if (pil[i][j] == '0') continue;
                int id = (i - 1) * col + j;
                maze[id][id + row * col] = pil[i][j] - '0';
                if (i <= d || i + d > row || j <= d || j + d > col) {
                    maze[id + row * col][2 * row * col + 1] = inf;
                }
                else {
                    for (int x = 1; x <= row; x++) {
                        for (int y = 1; y <= col; y++) {
                            if (abs(x - i) + abs(y - j) <= d) {
                                int id2 = (x - 1) * col + y;
                                if (id == id2) continue;
                                maze[id + row * col][id2] = inf;
                            }

                        }
                    }
                }
            }
        }      
        int sum = 0;
        for (int i = 1; i <= row; i++) {
            scanf("%s", liz[i] + 1);
            for (int j = 1; j <= col; j++) {
                if (liz[i][j] == 'L') {
                    sum++;
                    maze[0][(i - 1) * col + j] = 1;
                }
            }
        }
        int ans = sum - sap(0, 2 * row * col + 1, 2 * row * col + 2);
        if(ans == 0) printf("Case #%d: no lizard was left behind.\n", ++cas);
        else if(ans == 1) printf("Case #%d: %d lizard was left behind.\n", ++cas, ans);
        else printf("Case #%d: %d lizards were left behind.\n", ++cas, ans);
    }
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值