uva 563 - Crimewave(分点+最大流)

来源:互联网 发布:鼎尖软件 编辑:程序博客网 时间:2024/04/29 19:53

  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 numbera 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 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 


26 6 104 13 24 25 23 44 45 43 64 65 65 5 53 22 33 34 33 4

Sample Output 

possiblenot possible




题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=504

分析:这题很明显的分点,然后最大流判断是否存在解就行,由于好几个月没做网络流了,居然因为id计算错误,调了4个多小时。。。最后试探性的写了个id函数才过的= =

将每个交叉路口分为两个i和i',i到i‘连上容量为1 的有向边,表示这个交叉路口只能通过一次,然后i与j相连的话,i'到j,j'到i都连上容量为1的有向边,表示可以走的路,虚构一个源,与所有银行连上一条容量为1 的有向边,虚构一个汇点,连接所有城市边缘的交叉点到汇点。。。然后判断最大流是否等于银行数就行

代码:

#include<cstdio>#include<iostream>using namespace std;const int mm= 55555;const int oo=1000000000;int dx[]= {0,-1,0,1};int dy[]= {-1,0,1,0};int src,dest,node,edge;int ver[mm],flow[mm],next[mm];int head[mm],work[mm],q[mm],dis[mm];int n,m,p,t;void prepare(int _src,int _dest,int _node){    src=_src,dest=_dest,node=_node;    for(int i=0; i<node; ++i)head[i]=-1;    edge=0;}void addedge(int u,int v,int c1,int c2){    ver[edge]=v,flow[edge]=c1,next[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=c2,next[edge]=head[v],head[v]=edge++;}int 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=ver[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=ver[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,delta,ret=0;    while(Dinic_bfs())    {        for(i=0; i<node; ++i)work[i]=head[i];        while(delta=Dinic_dfs(src,oo))ret+=delta;    }    return ret;}int id(int i,int j,int k){    return n*m*k+(i-1)*m+j;}int main(){    int i,j,k,x,y;    scanf("%d",&p);    while(p--)    {        scanf("%d%d%d",&n,&m,&t);        prepare(0,n*m*2+1,n*m*2+2);        for(i=1; i<=n; ++i)            for(j=1; j<=m; ++j)            {                addedge(id(i,j,0),id(i,j,1),1,0);                for(k=0; k<4; ++k)                {                    x=i+dx[k];                    y=j+dy[k];                    if(x<1||x>n||y<1||y>m)continue;                    addedge(id(i,j,1),id(x,y,0),1,0);                }            }        for(k=0; k<t; ++k)        {            scanf("%d%d",&i,&j);            addedge(src,id(i,j,0),1,0);        }        for(i=1; i<=n; ++i)        {            addedge(id(i,1,1),dest,1,0);            if(m>1)addedge(id(i,m,1),dest,1,0);        }        for(i=2; i<m; ++i)        {            addedge(id(1,i,1),dest,1,0);            if(n>1)addedge(id(n,i,1),dest,1,0);        }        if(Dinic_flow()==t)puts("possible");        else puts("not possible");    }    return 0;}


原创粉丝点击