hdu 2732(最大流)

Leapin' Lizards

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


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.
 

Source
Mid-Central USA 2005
 

Recommend
zty
 

分析:这题很明显的最大流模型,不过输出结果比较恶心。。。注意答案为1的情况啊,wa了两次。。。

构图:

     将每个格子分为两个xa,xb,对于格子有人的,在源点与该点xa之间连一条容量为1的有向边,格子能跳出边界的,在该格子xb与汇点之间连一条容量为1的有向边,在xa与xb之间连一条容量为该格子数字的有向边,能互相到达的格子,在xbi与xaj之间连一条容量无穷的有向边

答案为总人数-最大流

代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int mm=66666;
const int mn=810;
const int oo=1000000000;
const int dx[4]={1,-1,1,-1};
const int dy[4]={1,1,-1,-1};
int node,src,dest,edge;
int reach[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
int mark[22][22];
char mapa[22][22],mapb[22][22];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<node; ++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c)
{
    reach[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    reach[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0; i<node; ++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=next[i])
            if(flow[i]&&dis[v=reach[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp; i>=0; i=next[i])
        if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; ++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
int main()
{
    int i,j,k,l,f,x,y,n,m,t,d,num,tot,cas=0,ans;
    bool can;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&d);
        for(i=0; i<n; ++i)scanf("%s",mapa[i]);
        for(i=0; i<n; ++i)scanf("%s",mapb[i]);
        m=strlen(mapa[0]);
        for(tot=i=0; i<n; ++i)
            for(j=0; j<m; ++j)
                if(mapa[i][j]>'0')mark[i][j]=++tot;
        prepare(tot*2+2,0,tot*2+1);
        for(num=i=0; i<n; ++i)
            for(j=0; j<m; ++j)
                if(mapa[i][j]>'0')
                {
                    addedge(mark[i][j],mark[i][j]+tot,mapa[i][j]-'0');
                    if(mapb[i][j]=='L')addedge(src,mark[i][j],1),++num;
                    can=0;
                    for(k=0;k<=d;++k)
                        for(l=0;l<=d;++l)
                            if(k+l>0&&k+l<=d)
                                for(f=0;f<4;++f)
                                {
                                    x=i+dx[f]*k;
                                    y=j+dy[f]*l;
                                    if(x>=0&&x<n&&y>=0&&y<m)
                                    {
                                        if(mapa[x][y]>'0')addedge(mark[i][j]+tot,mark[x][y],oo);
                                    }
                                    else can=1;
                                }
                    if(can)addedge(mark[i][j]+tot,dest,oo);
                }
        ans=num-Dinic_flow();
        if(ans>1)printf("Case #%d: %d lizards were left behind.\n",++cas,ans);
        else if(ans)printf("Case #%d: %d lizard was left behind.\n",++cas,ans);
        else printf("Case #%d: no lizard was left behind.\n",++cas);
    }
    return 0;
}


你可能感兴趣的:(report,Integer,OO,input,each,distance)