2012年10月4日 星期四

uva 563 - Crimewave



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




Miguel A. Revilla
1998-03-10

network flow, 為確保每點只有一能通過一次,每一點拆為兩點,中間串一條流量為1的edge
#include<stdio.h>
#include<cstring>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i,n) REP(i, 0, n)
#define DBG 0
#define INF int(1e9)
#define MAXN 5100
#define MAXM 35000
using namespace std;

int N,M,B,LD,T,cases=1,e=0;
int head[MAXN],v[MAXM],next[MAXM],cap[MAXM],
q[MAXN],p[MAXN],pe[MAXN],dis[MAXN],dir[2][2],u[MAXN],work[MAXM];
int bks[MAXN][2];
bool trav[MAXN];
void addedge(int a,int b,int K){
if(DBG)printf("addedge(%d): %d %d\n",e,a,b);
v[e]=b,cap[e]=K,
next[e]=head[a],head[a]=e++;
v[e]=a,cap[e]=0,
next[e]=head[b],head[b]=e++;
}
int BFS(){
int a,b,c,t1,t2,i,rear=0;
memset(dis,-1,sizeof(dis));
q[0]=0;
dis[rear++]=0;
for(i=0;i<rear;i++){
if(DBG)printf("BFS %d(%d %d)\n",b,(b/2-1)%N+1,(b/2-1)/N+1);
for(a=head[q[i]];a>=0;a=next[a]){
if(dis[v[a]]<0&&cap[a]){
if(DBG)printf("c %d\n",c);
dis[v[a]]=dis[q[i]]+1;
if(v[a]==1){
if(DBG)printf("reach t: %d\n",dis[1]);
return dis[v[a]];
}
q[rear++]=v[a];
}
}
}
return INF;
}
void dinic(){
int a,b,c;
while(BFS()<INF){
int c=0;
memset(trav,0,sizeof(trav));
memcpy(work,head,sizeof(work));
while(1){
if(DBG)printf("DFS c %d\n",c);
for(a=work[c];a>=0;a=next[a]){
b=v[a];
if(cap[a]&&!trav[b]&&dis[b]==dis[c]+1)break;
}
work[c]=a;
if(a>=0){
if(b==1){
if(DBG)printf("reach t\n");
LD--;
p[b]=c,pe[b]=a;
for(b=1;b!=0;b=p[b])cap[pe[b]]-=1,cap[pe[b]^1]+=1;
c=0; //back to root
}
else{
trav[b]=1;
p[b]=c,pe[b]=a,c=b;
}
}
else{
if(c==0)break;
c=p[c];
}
}
}
}
void ans(){
LD=B;
dinic();
if(LD)printf("not possible\n");
else printf("possible\n");
}
int getNo(int x,int y){return x+y*N+1;}
bool valid(int a,int b){return a>=0&&a<N&&b>=0&&b<M;}
void addNodes(){
int a,b,c,d;
rep(j,M)rep(i,N)c=getNo(i,j),addedge(2*c,2*c^1,1);
if(DBG)printf("add nei\n");
rep(i,N)rep(j,M){ //neighbor and self
c=getNo(i,j);
rep(k,2){
a=i+dir[k][0],b=j+dir[k][1];
if(valid(a,b))d=getNo(a,b),addedge(2*c^1,d*2,1),addedge(2*d^1,2*c,1);
}
}
rep(i,B){
if(DBG)printf("bnk %d %d\n",bks[i][0]+1,bks[i][1]+1);
c=getNo(bks[i][0],bks[i][1]);
addedge(0,2*c,1);
}
int snk=1;
rep(i,N){
c=getNo(i,0),d=getNo(i,M-1);
addedge(2*c^1,snk,1),addedge(2*d^1,snk,1);
}
rep(i,M){
c=getNo(0,i),d=getNo(N-1,i);
addedge(2*c^1,snk,1),addedge(2*d^1,snk,1);
}
}
void init(){
dir[0][0]=1,dir[1][1]=1;
}
int main(){
int a,b,n;
scanf("%d",&n);
init();
rep(i,n)
while(scanf("%d%d%d",&N,&M,&B)==3){
memset(head,-1,sizeof(head)),e=0;
rep(i,B)scanf("%d%d",&bks[i][0],&bks[i][1]),bks[i][0]--,bks[i][1]--;
addNodes();
ans();
}
}

沒有留言:

張貼留言