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 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 (
), followed by the number a of avenues (
), followed by the number b (
) 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
and
.
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

//
题目大意:给定一个n*m的矩形城市,这个城市有k个银行,每个银行都有不同的坐标。现在抢劫犯想抢劫了这些银行后抛出城市边界,但是要保证每个点最多经过一次,求这种方法是否可行。
解题思路:网络流。建立一个源点s和一个汇点t。将n*m个点的每个点A拆成两个点A1和A2且A1向A2连一条边。如果点A处是银行那么s与A1连一条边,如果B与A相邻,那么A2与B1连一条边,对于所有的边界点C,C2与汇点t连一条边,所有的边的容量都是1,最后做网络流,如果最大流量等于k就可行,否则不可行。
附代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 50010
#define M 500010
#define INF 0x3f3f3f3f
int row,col,K;
int n,m,e,s,t;
int head[N],dep[N],top,stk[N],cur[N];
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
struct node
{
int u,v,next,val;
}edge[M];
bool is_ok(int x, int y)
{
if(x >= 1 && x <= row && y >= 1 && y <= col) return true;
return false;
}
void addedge(int u, int v, int c)
{
edge[e].u = u; edge[e].v = v; edge[e].val = c;
edge[e].next = head[u]; head[u] = e++;
edge[e].u = v; edge[e].v = u; edge[e].val = 0;
edge[e].next = head[v]; head[v] = e++;
}
bool flow()
{
int i,j,k,f,r,Min;
int res = 0;
while(1)
{
memset(dep,-1,sizeof(dep));
f = 0; r = 1; dep[s] = 0; stk[0] = s;
while(f != r)
{
for(i = stk[f++], j = head[i]; j != -1; j = edge[j].next)
if(edge[j].val && -1 == dep[k=edge[j].v])
{
dep[k] = dep[i] + 1;
stk[r++] = k;
if(k == t) {f = r; break;}
}
}
if(-1 == dep[t]) break;
memcpy(cur,head,sizeof(head));
i = s; top = 0;
while(1)
{
if(i == t)
{
Min = INF;
for(j = 0; j < top; j++)
if(edge[stk[j]].val < Min)
{
Min = edge[stk[j]].val;
f = j;
}
for(j = 0; j < top; j++)
{
edge[stk[j]].val -= Min;
edge[stk[j]^1].val += Min;
}
res += Min; i = edge[stk[top=f]].u;
}
for(j = cur[i]; j != -1; j = cur[i] = edge[j].next)
if(edge[j].val && dep[i] + 1 == dep[edge[j].v]) break;
if(cur[i] != -1)
{
stk[top++] = cur[i];
i = edge[cur[i]].v;
}
else
{
if(0 == top) break;
dep[i] = -1; i = edge[stk[--top]].u;
}
}
}
if(res == K) return true;
return false;
}
int main()
{
int i,j,k;
int cases,a,b,cnt,x,y;
scanf("%d",&cases);
while(cases--)
{
scanf("%d%d%d",&row,&col,&K);
cnt = row*col; e = 0;
s = 0; n = cnt*2+2; t = cnt*2+1;
memset(head,-1,sizeof(head));
for(i = 1; i <= K; i++)
{
scanf("%d%d",&a,&b);
addedge(s,(a-1)*col+b,1);
}
for(i = 1; i <= col; i++)
{
addedge(i+cnt,t,1);
addedge((row-1)*col+i+cnt,t,1);
}
for(i = 2; i < row; i++)
{
addedge((i-1)*col+1+cnt,t,1);
addedge((i-1)*col+col+cnt,t,1);
}
for(i = 1; i <= row; i++)
for(j = 1; j <= col; j++)
{
a = (i-1)*col + j + cnt;
addedge(a-cnt,a,1);
for(k = 0; k < 4; k++)
{
x = i + dir[k][0]; y = j + dir[k][1];
if(is_ok(x,y))
{
b = (x-1)*col+y;
addedge(a,b,1);
}
}
}
if(flow()) printf("possible\n");
else printf("not possible\n");
}
return 0;
}