HDU 2732 Leapin' Lizards 最大流拆点+建图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tawn0000/article/details/82597626

                                           Leapin' Lizards

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


 

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

略~

Sample Output

Case #1: 2 lizards were left behind.

看题一小时,建图十分钟,A题一小时~~

一道拆点的模板题,只是题意不是很好理解,(ps.原谅我英语差)

题目意思是,有一个n*m 个的地图 有一些格子上有一柱子,有一些柱子上有蜥蜴,蜥蜴可以跳到任何与之所在格子的距离小于d的柱子上,当蜥蜴跳出地图后,才算得救,问你最多可以就下多少蜥蜴。

注意柱子每被一个蜥蜴跳过以后会下沉,有一个上限。

把每个柱子所在的格子都拆成两点1、2,连一条1->2权值为该上限的边,从2连一条到其他格子的边,INF边(如果超出地图,终点则为t)。

从0连一条到各个蜥蜴所在格子的1点的边,权值为1.

从0 到 t 跑最大流!

注意,起点为零

所以对于 格子 (0,0) ,编号应该为1,所以 (i,j) 标号为 i*m+j+1;

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 100;
const int maxm = 1000000 + 100;
typedef pair<int,int> P;
int n,d,m;
int l[maxn];//记录层数
int h[maxn];//链式前向星
int cur[maxn];
int tot = 0;
struct edge
{
 int to;
 int c;
 int next;
 edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {}
}es[maxm*2];//记录边 注意是2倍

void add_edge(int u, int v, int c)
{
   es[tot] = edge(v,c,h[u]);
   h[u] = tot++;
}

bool bfs(int s, int t)
{
  memset(l,0,sizeof(l));
  l[s] = 1;
  queue <int> q;
  q.push(s);
  while(!q.empty())
  {
   int u = q.front();
   q.pop();
   //cout << u <<  " " <<l[u] << endl;
   if(u == t)  return true;
   for(int i = h[u]; i != -1; i = es[i].next)
       {
        int v = es[i].to;
        if(!l[v] && es[i].c) {l[v] = l[u] + 1; q.push(v);}
       }
  }
  return false;
}

int dfs(int x, int t, int mf)
{

   if(x == t) return mf;
   int ret = 0;
   for(int &i = cur[x]; i != -1; i = es[i].next)
   {
    //  cout << x << " " << l[es[i].to]  << " "<< "mf = " << mf << endl;
     if(es[i].c && l[x] == l[es[i].to] - 1)
     {
      // cout << es[i].c << " "<< l[es[i].to] << endl;
       int f = dfs(es[i].to,t,min(es[i].c,mf - ret));
       es[i].c -= f;
       es[i^1].c += f;
       ret += f;
       if(ret == mf) return ret;
     }
   }
   return ret;
}

int dinic(int s, int t)
{
 int ans = 0;
 while(bfs(s,t))  {
   for(int i =
      0; i <= t; i++) cur[i] = h[i];
   int f = dfs(s,t,INF);
   ans += f;
   //if(f == 0) return ans;
   //cout << f << endl;
 }
 return ans;
}
int main()
{
  int T;
  scanf("%d",&T);
  int TT = T;
   while(T--)
   {
     memset(h,-1,sizeof(h));
     scanf("%d%d",&n,&d);
     int t = 1000;
     int res = 0;
     for(int i = 0; i < n; i++)
       {
         string str;
         cin >> str;
         m = str.size();
         for(int j = 0; j < m; j++)
          {
            if(str[j] != '0')
            {
              add_edge(i*m+j+1,i*m+j+1+400,str[j]-'0');
              add_edge(i*m+j+1+400,i*m+j+1,0);
              for(int k1 = -d; k1 <= d; k1++)
                for(int k2 = -d; k2 <= d; k2++)
                {
                  int ii = i + k1;
                  int jj = j + k2;
                  if(ii == i && jj == j) continue;
                  if((i-ii)*(i-ii) + (j-jj)*(j-jj) > d*d) continue;
                  if(ii < 0 || ii >= n || jj < 0 || jj >= m)
                   {
                    add_edge(i*m+j+1+400,t,INF);
                    add_edge(t,i*m+j+1+400,0);
                   }
                  else
                  {
                    add_edge(i*m+j+1+400,ii*m+jj+1,INF);
                    add_edge(ii*m+jj+1,i*m+j+1+400,0);
                  }
                }
            }
          }
        }

    for(int i = 0; i < n; i++)
    {
      string str;
      cin >> str;
      for(int j = 0; j < m; j++)
      {
        if(str[j] == 'L')
        {
          res++;
          add_edge(0,i*m+j+1,1);
          add_edge(i*m+j+1,0,0);
        }
      }
    }
   int ans = dinic(0,t);
   //cout << res << ans << endl;
   printf("Case #%d: ",TT-T);
   if(ans == res) printf("no lizard was left behind.\n");
   else if(res-ans == 1) printf("1 lizard was left behind.\n");
   else printf("%d lizards were left behind.\n",res-ans);
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/Tawn0000/article/details/82597626