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

畫船聽雨發表於2014-08-17

題目意思是有一些蜥蜴在一個迷宮裡面,求這些蜥蜴還有多少是無論如何都逃不出來的。題目只給定一個行數n,一個最遠能夠跳躍的距離d。每隻蜥蜴有一個初始的位置,題目保證這些位置都有一些柱子,但是它每離開一根柱子,柱子的高度就會降低1m,問最多能有多少隻跳不出去。

將每個柱子在的點進行拆點,把每一個點拆完之後連一條容量為所在點柱子高度的邊。從原點連一條容量為1的邊,然後找到每個可以直接跳出的點,將這些點與匯點 相連容量為無窮。每個柱子與它可以到達的點的容量也為無窮。


Leapin' Lizards

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


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.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

using namespace std;

const int maxn = 1100;

int cnt;
int n, m;
int cur[maxn], head[maxn];
int dis[maxn], gap[maxn];
int aug[maxn], pre[maxn];
int num[maxn];

struct node
{
    int v, w;
    int next;
} f[2010000];

void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w)
{
    f[cnt].v = v;
    f[cnt].w = w;
    f[cnt].next = head[u];
    head[u] = cnt++;

    f[cnt].v = u;
    f[cnt].w = 0;
    f[cnt].next = head[v];
    head[v] = cnt++;
}

int SAP(int s, int e, int n)
{
    int max_flow = 0, v, u = s;
    int id, mindis;
    aug[s] = INF;
    pre[s] = -1;
    memset(dis, 0, sizeof(dis));
    memset(gap, 0, sizeof(gap));
    gap[0] = n;
    for (int i = 0; i <= n; ++i)  cur[i] = head[i];/// 初始化當前弧為第一條弧
    while (dis[s] < n)
    {
        bool flag = false;
        if (u == e)
        {
            max_flow += aug[e];
            for (v = pre[e]; v != -1; v = pre[v]) /// 路徑回溯更新殘留網路
            {
                id = cur[v];
                f[id].w -= aug[e];
                f[id^1].w += aug[e];
                aug[v] -= aug[e]; /// 修改可增廣量,以後會用到
                if (f[id].w == 0) u = v; /// 不回退到源點,僅回退到容量為0的弧的弧尾
            }
        }
        for (id = cur[u]; id != -1; id = f[id].next)/// 從當前弧開始查詢允許弧
        {
            v = f[id].v;
            if (f[id].w > 0 && dis[u] == dis[v] + 1) /// 找到允許弧
            {
                flag = true;
                pre[v] = u;
                cur[u] = id;
                aug[v] = min(aug[u], f[id].w);
                u = v;
                break;
            }
        }
        if (flag == false)
        {
            if (--gap[dis[u]] == 0) break; ///gap優化,層次樹出現斷層則結束演算法
            mindis = n;
            cur[u] = head[u];
            for (id = head[u]; id != -1; id = f[id].next)
            {
                v = f[id].v;
                if (f[id].w > 0 && dis[v] < mindis)
                {
                    mindis = dis[v];
                    cur[u] = id; /// 修改標號的同時修改當前弧
                }
            }
            dis[u] = mindis + 1;
            gap[dis[u]]++;
            if (u != s) u = pre[u]; /// 回溯繼續尋找允許弧
        }
    }
    return max_flow;
}

char map1[maxn][maxn], map2[maxn][maxn];
int vis[maxn][maxn];

double dist(int x1, int y1, int x2, int y2)
{
    double a = x1, b = y1, c = x2, d = y2;
    return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}
int main()
{
    int Case = 1;
    int d;
    int K;
    cin >>K;
    while(K--)
    {
        scanf("%d %d",&n, &d);
        init();
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++) cin >>map1[i];
        for(int j = 0; j < n; j++) cin >>map2[j];
        int len = strlen(map1[0]);
        int k = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < len; j++) if(map1[i][j]-'0' > 0) vis[i][j] = ++k;
        int S = 0;
        int T = 2*k+1;
        int en = T+1;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < len; j++)
            {
                if(map1[i][j]-'0' > 0)
                {
                    add(vis[i][j], vis[i][j]+k, map1[i][j]-'0');
                    for(int ii = 0; ii < n; ii++)
                    {
                        for(int jj = 0; jj < len; jj++)
                        {
                            if(i == ii && j == jj) continue;
                            double s = dist(i, j, ii, jj);
                            if(vis[ii][jj] && (double)d >= s) add(vis[i][j]+k, vis[ii][jj], INF-10);
                        }
                    }
                }
            }
        }
        int kk = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < len; j++)
            {
                if(map2[i][j] == 'L')
                {
                    kk++;
                    add(S, vis[i][j], 1);
                }
            }
        }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < len; j++)
                if(map1[i][j]-'0' > 0) if(i+1<=d || j+1<=d || n-i<=d || len-j<=d) add(vis[i][j]+k, T, INF-10);
        int ans = SAP(S, T, en);
        cout<<"Case #"<<Case++<<": ";
        if(kk-ans == 0) cout<<"no lizard was left behind."<<endl;
        else if(kk-ans == 1) cout<<"1 lizard was left behind."<<endl;
        else cout<<kk-ans<<" lizards were left behind."<<endl;
    }
    return 0;
}


相關文章