K键盘里的青春K

我曾经失去的荣耀,我定加倍夺回

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


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;
}


阅读更多
版权声明:希望我的博客可以为别人带去知识与便利,让那些像我曾经一样迷茫的小伙伴不再迷茫~ https://blog.csdn.net/qq_34374664/article/details/75676076
个人分类: 网络流
所属专栏: ACM
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭