Crimewave - UVa 563 dinic网络流

76 篇文章 0 订阅

Crimewave

Nieuw Knollendam is a very modern town. This becomes clear already when looking at the layout of its map, which is just a rectangular grid of streets and avenues. Being an important trade centre, Nieuw Knollendam also has a lot of banks. Almost on every crossing a bank is found (although there are never two banks at the same crossing). Unfortunately this has attracted a lot of criminals. Bank hold-ups are quite common, and often on one day several banks are robbed. This has grown into a problem, not only to the banks, but to the criminals as well. After robbing a bank the robber tries to leave the town as soon as possible, most of the times chased at high speed by the police. Sometimes two running criminals pass the same crossing, causing several risks: collisions, crowds of police at one place and a larger risk to be caught.


To prevent these unpleasant situations the robbers agreed to consult together. Every Saturday night they meet and make a schedule for the week to come: who is going to rob which bank on which day? For every day they try to plan the get-away routes, such that no two routes use the same crossing. Sometimes they do not succeed in planning the routes according to this condition, although they believe that such a planning should exist.


Given a grid of $(s \times a)$ and the crossings where the banks to be robbed are located, find out whether or not it is possible to plan a get-away route from every robbed bank to the city-bounds, without using a crossing more than once.

Input 

The first line of the input contains the number of problems  p  to be solved.

  • The first line of every problem contains the number s of streets ( $1 \le s \le 50$), followed by the number aof avenues ( $1 \le a \le 50$), followed by the number b ($b \ge 1$) of banks to be robbed.

  • Then b lines follow, each containing the location of a bank in the form of two numbers x (the number of the street) and y (the number of the avenue). Evidently $1 \le x \le s$ and $1 \le y \le a$.

Output 

The output file consists of  p  lines. Each line contains the text  possible  or  not possible . If it is possible to plan non-crossing get-away routes, this line should contain the word:  possible . If this is not possible, the line should contain the words  not possible .

Sample Input 

2
6 6 10
4 1
3 2
4 2
5 2
3 4
4 4
5 4
3 6
4 6
5 6
5 5 5
3 2
2 3
3 3
4 3
3 4

Sample Output 

possible
not possible



题意:从给定的点流到边上的点,要求所有点只能经过一次,问是否可以全部留出。

思路:拆点,将每个点变成两个点A1和A2,A1流到A2的量为1,A2流到四周的B1上,流量也为1。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int first[7010],next[30010],u[30010],v[30010],flow[30010],d[7010];
int s,a,b,e,INF=1e9;
queue<int> qu;
void add(int x,int y,int f)
{
    u[e]=x;
    v[e]=y;
    flow[e]=f;
    next[e]=first[x];
    first[x]=e;
    e++;
}
void init()
{
    int i,j,k,z,newz,x,y;
    e=0;
    scanf("%d%d%d",&s,&a,&b);
    memset(first,-1,sizeof(first));
    memset(flow,0,sizeof(flow));
    for(i=1;i<=a;i++)
    {
        z=a+i;
        add(2*z^1,1,1);
        add(1,2*z^1,0);
        z=s*a+i;
        add(2*z^1,1,1);
        add(1,2*z^1,0);
    }
    for(i=1;i<=s;i++)
    {
        z=i*a+1;
        add(2*z^1,1,1);
        add(1,2*z^1,0);
        z=i*a+a;
        add(2*z^1,1,1);
        add(1,2*z^1,0);
    }
    for(i=1;i<=s;i++)
       for(j=1;j<=a;j++)
       {
           z=i*a+j;
           add(2*z,2*z^1,1);
           add(2*z^1,2*z,0);
           if(i+1<=s)
           {
               newz=z+a;
               add(2*z^1,2*newz,1);
               add(2*newz,2*z^1,0);
               add(2*newz^1,2*z,1);
               add(2*z,2*newz^1,0);
           }
           if(j+1<=a)
           {
               newz=z+1;
               add(2*z^1,2*newz,1);
               add(2*newz,2*z^1,0);
               add(2*newz^1,2*z,1);
               add(2*z,2*newz^1,0);
           }
       }
    for(i=1;i<=b;i++)
    {
        scanf("%d%d",&x,&y);
        z=x*a+y;
        add(0,2*z,1);
        add(2*z,0,0);
    }
}
int bfs()
{
    int i,j;
    memset(d,-1,sizeof(d));
    while(!qu.empty())
      qu.pop();
    qu.push(0);
    d[0]=0;
    while(!qu.empty())
    {
        i=qu.front();
        qu.pop();
        for(j=first[i];j!=-1;j=next[j])
           if(flow[j]>0 && d[v[j]]==-1)
           {
               d[v[j]]=d[i]+1;
               if(v[j]==1)
                 return 1;
               qu.push(v[j]);
           }
    }
    return 0;
}
int dfs(int cur,int f)
{
    if(cur==1 || f==0)
      return f;
    int ans=0,i,j,k,t;
    for(j=first[cur];j!=-1;j=next[j])
       if(flow[j]>0 && d[v[j]]==d[cur]+1)
       {
           k=dfs(v[j],min(flow[j],f));
           flow[j]-=k;
           flow[j^1]+=k;
           f-=k;
           ans+=k;
           if(f==0)
             break;
       }
    d[cur]=-1;
    return ans;
}
int dinic()
{
    int ans=0;
    while(bfs())
        ans+=dfs(0,INF);
    return ans;
}
int main()
{
    int T,t,i,j,k,ans;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        init();
        ans=dinic();
        if(ans==b)
          printf("possible\n");
        else
          printf("not possible\n");
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值