zoukankan      html  css  js  c++  java
  • HDU 2732 Leapin' Lizards(最大流)

    Leapin' Lizards




    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.
     
      1 #include<cstdio>
      2 #include<iostream>
      3 #include<string>
      4 #include<cstring>
      5 #include<queue>
      6 #include<vector>
      7 #include<algorithm>
      8 using namespace std;
      9 
     10 struct Edge
     11 {
     12     int from,to,cap,flow;
     13 };
     14 const int maxn=1005;
     15 const int inf=0x3f3f3f;
     16 
     17 struct dinic
     18 {
     19     int n,m,s,t;
     20     vector<Edge>edges;
     21     vector<int>G[maxn];
     22     bool vis[maxn];
     23     int d[maxn];
     24     int cur[maxn];
     25 
     26     void init(int num)
     27     {
     28         for(int i=0;i<num;i++)G[i].clear();
     29         edges.clear();
     30     }
     31 
     32     void addedge(int from,int to,int cap)
     33     {
     34         edges.push_back((Edge){from,to,cap,0});
     35         edges.push_back((Edge){to,from,0,0});
     36         int m=edges.size();
     37         G[from].push_back(m-2);
     38         G[to].push_back(m-1);
     39     }
     40 
     41     bool bfs()
     42     {
     43         memset(vis,0,sizeof(vis));
     44         queue<int>q;
     45         q.push(s);
     46         d[s]=0;
     47         vis[s]=1;
     48         while(!q.empty())
     49         {
     50             int x=q.front();q.pop();
     51             for(int i=0;i<G[x].size();i++)
     52             {
     53                 Edge& e=edges[G[x][i]];
     54                 if(!vis[e.to]&&e.cap>e.flow)
     55                 {
     56                     vis[e.to]=1;
     57                     d[e.to]=d[x]+1;
     58                     q.push(e.to);
     59                 }
     60             }
     61         }
     62         return vis[t];
     63     }
     64 
     65     int dfs(int x,int a)
     66     {
     67         if(x==t||a==0)return a;
     68         int flow=0,f;
     69         for(int& i=cur[x];i<G[x].size();i++)
     70         {
     71             Edge& e=edges[G[x][i]];
     72             if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
     73             {
     74                 e.flow+=f;
     75                 edges[G[x][i]^1].flow-=f;
     76                 flow+=f;
     77                 a-=f;
     78                 if(a==0)break;
     79             }
     80         }
     81         return flow;
     82     }
     83 
     84     int maxflow(int s,int t)
     85     {
     86         this->s=s,this->t=t;
     87         int flow=0;
     88         while(bfs())
     89         {
     90             memset(cur,0,sizeof(cur));
     91             flow+=dfs(s,inf);
     92         }
     93         return flow;
     94     }
     95 }dc;
     96 
     97 int main()
     98 {
     99     int T;
    100     scanf("%d",&T);
    101     for(int kase=1;kase<=T;kase++)
    102     {
    103         int m,n,d,sum=0;
    104         scanf("%d%d",&n,&d);
    105         for(int i=0;i<n;i++)
    106         {
    107             string s;
    108             cin>>s;
    109             if(i==0)m=s.size(),dc.init(2*n*m+2);
    110             for(int j=0;j<m;j++)
    111             {
    112                 int id=i*m+j+1;
    113                 if(s[j]-'0'>0)
    114                 {
    115                     dc.addedge(id,id+m*n,s[j]-'0');
    116                     if(i<d||i+d>=n||j<d||j+d>=m)
    117                         dc.addedge(id+n*m,2*n*m+1,inf);
    118                     else
    119                     {
    120                         for(int k=0;k<n;k++)
    121                             for(int l=0;l<m;l++)
    122                             {
    123                                 int id2=k*m+l+1;
    124                                 if(id==id2)continue;
    125                                 if(abs(i-k)+abs(j-l)<=d)
    126                                     dc.addedge(id+n*m,id2,inf);
    127                             }
    128                     }
    129                 }
    130             }
    131         }
    132         for(int i=0;i<n;i++)
    133         {
    134             string s;
    135             cin>>s;
    136             for(int j=0;j<s.size();j++)
    137                 if(s[j]=='L')
    138                 {
    139                     sum++;
    140                     dc.addedge(0,i*m+j+1,1);
    141                 }
    142         }
    143         int ans=sum-dc.maxflow(0,2*m*n+1);
    144         if(ans==0) printf("Case #%d: no lizard was left behind.
    ",kase);
    145         else if(ans==1) printf("Case #%d: 1 lizard was left behind.
    ",kase);
    146         else printf("Case #%d: %d lizards were left behind.
    ",kase,ans);
    147     }
    148     return 0;
    149 }
  • 相关阅读:
    区块链中的随机数 nonce
    SaaS(软件即服务)、PaaS(平台即服务)、IaaS(基础架构即服务)、BaaS(区块链即服务)
    程序插桩简介
    侧链技术
    闪电/雷电网络
    Ubuntu16.04安装/升级openssl到1.1版本
    Ubuntu16.04升级Python3及其pip3并切换为默认版本
    Python——/usr/bin/env: ‘python(3) ’: No such file or directory
    TCP通信功能 (agent功能)
    gin框架web操作数据库
  • 原文地址:https://www.cnblogs.com/homura/p/4907501.html
Copyright © 2011-2022 走看看