HDU 2732 Leapin' Lizards (拆点棋盘建图+最大流)

原创 2017年07月22日 00:41:25


Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2846    Accepted Submission(s): 1171


Problem Description
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room's floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below... Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there's a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.
 

Input
The input file will begin with a line containing a single integer representing the number of test cases, which is at most 25. Each test case will begin with a line containing a single positive integer n representing the number of rows in the map, followed by a single non-negative integer d representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit (0-3) in each position representing the number of jumps the pillar in that position will sustain before collapsing (0 means there is no pillar there). The second map will follow, with an 'L' for every position where a lizard is on the pillar and a '.' for every empty pillar. There will never be a lizard on a position where there is no pillar.Each input map is guaranteed to be a rectangle of size n x m, where 1 ≤ n ≤ 20 and 1 ≤ m ≤ 20. The leaping distance is
always 1 ≤ d ≤ 3.
 

Output
For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.
 

Sample Input
4 3 1 1111 1111 1111 LLLL LLLL LLLL 3 2 00000 01110 00000 ..... .LLL. ..... 3 1 00000 01110 00000 ..... .LLL. ..... 5 2 00000000 02000000 00321100 02000000 00000000 ........ ........ ..LLLL.. ........ ........
 

Sample Output
Case #1: 2 lizards were left behind. Case #2: no lizard was left behind. Case #3: 3 lizards were left behind. Case #4: 1 lizard was left behind.

题意思路转自:http://blog.csdn.net/u013480600/article/details/38964749

题意:

       给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最大跳跃距离是d,如果一只蜥蜴能跳出网格边缘,那么它就安全了.且每个网格有一个最大跳出次数x,即最多有x只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出网格.

分析:

       本题类似POJ3498:

http://blog.csdn.net/u013480600/article/details/38900315

       建图:

       源点S编号0,网格的每个格子分成两个点i和i+n*m(nm为网格的行和列数,其实i编号点是表示蜥蜴进来,i+n*m编号的点是表示蜥蜴出去).汇点t编号n*m*2+1.

       如果格子i上有蜥蜴,那么从s到i有边(s,i,1).

       如果格子i能承受x次跳出,那么有边(i,i+n*m,x)

       如果从格子i能直接跳出网格边界,那么有边(i+n*m,t,INF)

       如果从格子i不能直接跳出网格,那么从i到离i距离<=d的网格j有边(i+n*m,j,INF). 注意这里的距离是abs(行号之差)+abs(列号之差)

       最终我们求出的最大流就是能跳出网格的蜥蜴数.

       原题中提到:任意时刻每个柱子上最多只有1只蜥蜴在上面,那么我们上面的解法会不会与这个要求冲突呢?

       不会的,假设有k只蜥蜴能出去,那么一定存在一个符合上面要求的解,使得这k只蜥蜴按顺序出去,在任意时刻每个柱子上最多只有1只蜥蜴.(假设在某个时刻,蜥蜴j想出去,它跳到了柱子h上,但是柱子h上已经有蜥蜴了,那么这样就违反了上面的要求. 其实我们可以这么想,我们为什么不让柱子h上的蜥蜴先按照蜥蜴j以前的逃跑路线出去,然后再让蜥蜴j到柱子h上去替代之前的蜥蜴,那么这样既不违反规则,也得到了解)


这题从各方面暗示你: 我是网络流, 一个蜥蜴跳了一个柱子,这个柱子就下降一块, 这就相当于水流在水管里流动,占有一部分体积一样。。然后汇点连入所有要求的蜥蜴, 然后权值是1,代表只有一个蜥蜴,代表这块水管的水流容量是1, 这种题都要拆点, 权值就是这块方块的权值, 代表这段水管的容量, 然后可以到达目的的东西, 就跟汇点连在一起, 权值赋值为INF,让容量无限大,因为相连或者出去, 没有约束,所以应该容量没有约束...


注意 因为汇点是0,所以前向星的时候,一定要把k赋值成0,唉...说多了都是泪...

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 1000;
const int INF = 0x3f3f3f3f;
int head[maxn], cur[maxn], d[maxn], n, m, s, t, k, sum;
char maze[25][25], val[25][25];
struct node
{
    int v, w, next;
}edge[maxn*maxn];
void addEdge(int u, int v, int w)
{
    edge[k].v = v;
    edge[k].w = w;
    edge[k].next = head[u];
    head[u] = k++;
    edge[k].v = u;
    edge[k].w = 0;
    edge[k].next = head[v];
    head[v] = k++;

}
int bfs()
{
    memset(d, 0, sizeof(d));
    d[s] = 1;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();
        if(u == t) return 1;
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int to = edge[i].v, w = edge[i].w;
            if(w && d[to] == 0)
            {
                d[to] = d[u] + 1;
                if(to == t) return 1;
                q.push(to);
            }
        }
    }
    return 0;
}
int dfs(int u, int maxflow)
{
    if(u == t) return maxflow;
    int ret = 0;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int to = edge[i].v, w = edge[i].w;
        if(w && d[to] == d[u]+1)
        {
            int f = dfs(to, min(maxflow-ret, w));
            edge[i].w -= f;
            edge[i^1].w += f;
            ret += f;
            if(ret == maxflow) return ret;
        }
    }
    return ret;
}
int Dinic()
{
    int ans = 0;
    while(bfs() == 1)
    {
//        memcpy(cur, head, sizeof(head));
        ans += dfs(s, INF);
    }
    return ans;
}
int main()
{
    int T, ca = 1;
    scanf("%d", &T);
    while(T--)
    {
        k = 0; sum = 0;
        memset(head, -1, sizeof(head));
        int step;
        scanf("%d%d", &n, &step);
        for(int i = 1; i <= n; i++)
            scanf(" %s", val[i]);
        for(int i = 1; i <= n; i++)
            scanf(" %s", maze[i]);
        m = strlen(val[1]);
        s = 0; t = n*m*2+1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                int in = (i-1)*m+j+1, out = (i-1)*m+j+1+n*m;
                if(maze[i][j] == 'L')
                {
                    addEdge(s, in, 1);
                    sum++;
                }
                if(val[i][j] == '0') continue;
                addEdge(in, out, val[i][j]-'0');
                if(i+step > n || i-step < 1 || j+step >= m || j-step < 0)
                    addEdge(out, t, INF);
                else
                {
                    for(int x = 1; x <= n; x++)
                        for(int y = 0; y < m; y++)
                            {
                                if(val[x][y] == '0') continue;
                                if(x == i && y == j) continue;
                                if(abs(x-i) + abs(y-j) <= step)
                                    addEdge(out, (x-1)*m+y+1, INF);
                            }
                }
            }
        }
        int ans = Dinic();
//        cout << ans << endl;
        if(sum-ans == 0)
            printf("Case #%d: no lizard was left behind.\n", ca++);
        else if(sum-ans == 1)
            printf("Case #%d: 1 lizard was left behind.\n", ca++, sum-ans);
        else
            printf("Case #%d: %d lizards were left behind.\n", ca++, sum-ans);
    }
    return 0;
}


版权声明:ACM小白成长之路,我想做那种所有人都能看懂的博客,想让我的博客成为acm新手的最好伙伴~让我的博客就像大神ACdreamer那样可以给别人带去知识~

相关文章推荐

hdu 2732 Leapin' Lizards【Edmond-Karp最大流+Bfs建图】

Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)...

HDU2732Leapin' Lizards(最大流SAP,建图---折点法)

Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)...

HDU 2732 Leapin' Lizards(拆点法+最大流)

该题是一道比较简单拆点+最大流的题目,因为每个柱子都有一定的寿命,很容易将其对应成流量,那么处理结点容量的一般方法当然是拆点法 。该题反而对边的容量没有要求,为保险起见可以设成无穷大。   该题的思路...

hdu 2732 / poj 2711 Leapin' Lizards //拆点最大流

 Leapin' LizardsTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1166 Accepted: 447Descript...
  • hqd_acm
  • hqd_acm
  • 2011年04月29日 19:47
  • 1455

hdu 2732 Leapin' Lizards 【图论-网络流-最大流】

Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

HDU 2732 Leapin' Lizards (最大流)

题目链接 题目大意:有一些蜥蜴在一些迷宫里,迷宫的行数为n列数没给,每个蜥蜴每次可以跳跃的最大距离为d,现在给出两个矩阵,第一个描述的是每个位置最多可以被走几次,第二个描述的是哪里有蜥蜴,问最少有几...
  • HHH_go_
  • HHH_go_
  • 2016年12月14日 17:21
  • 273

Leapin' Lizards (hdu 2732 最大流)

题意:给一个n行的图(不知道有多少列),每个点的数字表示能承受的最大跳跃次数,接下来又是n行,‘L’表示蜥蜴所在地方,给出蜥蜴能跳跃的最大距离d,每次蜥蜴跳离的那根柱子的承受力会减一,若为零了该点的柱...

HDU2732 Leapin' Lizards(最大流)

建图比较恶心的网络流模型。 有一个n*m(m没有直接给出)的网状迷宫图,还有一堆蜥蜴要从这个迷宫逃跑。蜥蜴站在一个柱子上面,每次跳跃的最大值是d(1≤d≤31\leq d\leq3),而且蜥蜴起跳离...
  • cqbzwja
  • cqbzwja
  • 2016年03月13日 12:45
  • 138

hdu 2732 Leapin' Lizards(最大流)

题意:给出一个n×m的方格,每个方格有可能有柱子,柱子上有可能有蜥蜴,每个柱子有个长度,每个蜥蜴每次能跳的最远的长度为d,每个柱子蜥蜴从这个柱子跳出去会长度会下降。下降到0蜥蜴就不能跳到上面。问最后有...
  • qian99
  • qian99
  • 2014年03月05日 19:40
  • 538

最大流-HDU-2732-Leapin' Lizards

Leapin’ Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 2732 Leapin' Lizards (拆点棋盘建图+最大流)
举报原因:
原因补充:

(最多只允许输入30个字)