Crimewave (Uva 563 最大流拆点)

46 篇文章 0 订阅

 Crimewave 

Nieuw Knollendam is a very modern town. This becomes clear already whenlooking at the layout of its map, which is just a rectangular grid of streetsand avenues. Being an important trade centre, Nieuw Knollendam also has a lotof 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 bankthe robber tries to leave the town as soon as possible, most of the timeschased at high speed by the police. Sometimes two running criminals pass thesame crossing, causing several risks: collisions, crowds of police atone place and a larger risk to be caught.


To prevent these unpleasant situations the robbers agreed to consulttogether. Every Saturday night they meet and make a schedule for the weekto come: who is going to rob which bank on which day? For every day they tryto plan the get-away routes,such that no two routes use the same crossing. Sometimes they do notsucceed in planning the routes according to this condition, although theybelieve 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 a of 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 formof 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, thisline 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



Miguel A. Revilla
1998-03-10



题意:b个银行被抢,罪犯制定逃跑路线,要求路上上的点和边不重合,问是否存在可行方案。图的边界就是表示逃出去了。

思路:拆点,容量为1,每个点向四周相邻的点连边,容量为1,判断是否满流。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 5500;
const int MAXM = 220010;
const int N = 1005;

int n,m,b;

struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];

int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}

//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
    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 start,int end,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u=start;
    pre[u]=-1;
    gap[0]=N;
    int ans=0;
    while (dep[start]<N)
    {
        if (u==end)
        {
            int Min=INF;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
                if (Min>edge[i].cap-edge[i].flow)
                    Min=edge[i].cap-edge[i].flow;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u=start;
            ans+=Min;
            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 Min=N;
        for (int i=head[u];i!=-1;i=edge[i].next)
            if (edge[i].cap-edge[i].flow && dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if (!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if (u!=start) u=edge[pre[u]^1].to;
    }
    return ans;
}

int dir[4][2]={1,0,0,1,-1,0,0,-1};

bool isok(int x,int y)
{
    if (x>=1&&x<=n&&y>=1&&y<=m) return true;
    return false;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j,t,u,v,k;
    sf(t);
    while (t--)
    {
        init();
        sfff(n,m,b);
        for (i=0;i<b;i++)
        {
            sff(u,v);
            addedge(0,(u-1)*m+v,1);
        }
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=m;j++)
            {
                addedge((i-1)*m+j,(i-1)*m+j+n*m,1);
                for (k=0;k<4;k++)
                {
                    int dx=i+dir[k][0];
                    int dy=j+dir[k][1];
                    if (isok(dx,dy))
                        addedge((i-1)*m+j+n*m,(dx-1)*m+dy,1);
                    else
                        addedge((i-1)*m+j+n*m,2*n*m+1,1);
                }
            }
        }
        int ans=sap(0,n*m*2+1,n*m*2+2);
//        printf("ans=%d\n",ans);
        if (ans==b)
            pf("possible\n");
        else
            pf("not possible\n");
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值