poj2711-Leapin' Lizards(最大流)

Leapin' Lizards

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1792 Accepted: 704
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.

题意:蜥蜴站在高度大于0柱子上,柱子只能承受和高度相等的数量的蜥蜴,求最多有几只蜥蜴挑不出边界。

思路:最大流。把有蜥蜴的柱子和源点连起来权值为一,把柱子之间距离小于等于m的连起来权值为柱子的高度,把可以一步跳出边界的于汇点连起来权值为柱子的高度。


#include<map>
#include<set>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f
#define ll long long
#define maxn 2000
#define maxm 200000
using namespace std;
struct node1{
    int x,y,high,f;
}pill[maxn];
string s[maxn];
struct Edge{
int to,next;int cap,flow;
}edge[maxm];
int tol,n,m;
int head[maxn];
int gap[maxn],dep[maxn],pre[maxn],cur[maxn];
bool judge(node1 p){int x=p.x,y=p.y;
  for(int i=0;i<=s[0].size()+1;i++)if(sqrt((x-0)*(x-0)+(y-i)*(y-i))<=m*1.0){return true;}
  for(int i=0;i<=n+1;i++)if(sqrt((x-i)*(x-i)+(y-0)*(y-0))<=m*1.0){return true;}
  for(int i=0;i<=s[0].size()+1;i++)if(sqrt((x-n-1)*(x-n-1)+(y-i)*(y-i))<=m*1.0){return true;}
  for(int i=0;i<=n+1;i++)if(sqrt((y-s[0].size()-1)*(y-s[0].size()-1)+(x-i)*(x-i))<=m*1.0){return true;}
  return false;
}
void init(){tol=0;memset(head,-1,sizeof(head));memset(edge,0,sizeof(edge));}
void addedge(int u,int v,int w,int rw){
    edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];edge[tol].flow=0;head[u]=tol++;
    edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];edge[tol].flow=0;head[v]=tol++;
}
int sap(int s,int t,int N){
    memset(gap,0,sizeof(gap));memset(dep,0,sizeof(dep));memset(pre,0,sizeof(pre));
    memcpy(cur,head,sizeof(head));
    int u=s;pre[u]=-1;gap[0]=N;int ans=0;
    while(dep[s]<N){
        if(u==t){
            int minn=inf;for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
            if(minn>edge[i].cap-edge[i].flow)
                minn=edge[i].cap-edge[i].flow;
            for(int i=pre[u];i!=-1;i=pre[edge[i^1].to]){
                edge[i].flow+=minn;edge[i^1].flow-=minn;
            }u=s;ans+=minn;continue;
        }
        bool flag=false;int v;
        for(int i=cur[u];i!=-1;i=edge[i].next){
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow && dep[v]+1==dep[u]){
                flag=true;cur[u]=pre[v]=i;break;
            }
        }if(flag){u=v;continue;}
        int minn=N;
        for(int i=head[u];i!=-1;i=edge[i].next)
        if(edge[i].cap-edge[i].flow && dep[edge[i].to]<minn){
            minn=dep[edge[i].to];cur[u]=i;
            }
        gap[dep[u]]--;if(!gap[dep[u]])return ans;
        dep[u]=minn+1;gap[dep[u]]++;if(u!=s)u=edge[pre[u]^1].to;
        }
        return ans;
}
int main(){
    int test;scanf("%d",&test);int tes=0;
    while(test--){init();for(int i=0;i<maxn;i++)s[i].clear();
    scanf("%d%d",&n,&m);int l=0,k=0;memset(pill,0,sizeof(pill));
    for(int i=0;i<2*n;i++)cin>>s[i];
    for(int i=0;i<n;i++){
    for(int j=0;j<s[i].size();j++){if(s[i][j]>'0'){
        pill[l].x=i+1;pill[l].y=j+1;pill[l].high=s[i][j]-'0';
    if(s[i+n][j]=='L')pill[l].f=1;else pill[l].f=0;
    l++;} }
        }int s=0,t=l+l+1;
        for(int i=0;i<l;i++){for(int j=0;j<l;j++){
        double dis=sqrt((pill[i].x-pill[j].x)*(pill[i].x-pill[j].x)+(pill[i].y-pill[j].y)*(pill[i].y-pill[j].y));
        if(dis<=m*1.0)addedge(j+1+l,i+1,pill[j].high,0);}
        }for(int i=0;i<l;i++){if(judge(pill[i])){addedge(i+l+1,t,pill[i].high,0);}
        }
        for(int i=0;i<l;i++){addedge(i+1,i+1+l,pill[i].high,0);if(pill[i].f==1){k++;addedge(s,i+1,1,0);}}
        int ans=sap(s,t,t+1);ans=k-ans;printf("Case #%d: ",++tes);
        if(ans==0)cout<<"no lizard was left behind."<<endl;
        else if(ans==1)cout<<"1 lizard was left behind."<<endl;
        else printf("%d lizards were left behind.\n",ans);
    }
}



posted @ 2018-01-19 17:05  _大美  阅读(131)  评论(0编辑  收藏  举报