uva563 - Crimewave 拆点+链接表的最大流

来源:互联网 发布:如何学好高中数学知乎 编辑:程序博客网 时间:2024/05/17 04:43

  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 numberb ($b \ge 1$) of banks to be robbed.
  • Then b lines follow, each containing the location of a bank in the form of two numbersx (the number of the street) andy (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 ornot 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

  S*A的网格,给你M个银行的坐标,问能否不经过同样的点,从每个银行走到网格外面。


  建图:把S*A个点都设成一个点,相互能到达的容量设为1。

  由于不能经过同样的点,也就是说每个点容量为1,这样就要拆点了,把每个点n拆成2个(可以设为n和n+S*A),cap[n][n+S*A]=1。如果有点u连接n则cap[u][n]=1,如果n连接v则cap[n+S*A][v]=1。总之在每个点有容量的时候一般都要拆点,从一个点进,另一个点出,两个点的边的容量为被拆点的容量。

  设起点为0,连接银行的点n,cap[0][n]=1。设终点为2*S*A+1,把边缘的点n连上,注意是cap[n+S*A][2*S*A+1]=1。


  这里面每个点最多和4个点连,如果不用链接表,每次取出一个点就把所有点遍历一遍的话应该会超时。

  我发现链接表如果用于有向图,如果u,v之间有边,那么不光要把u,v连接,还要把v,u连接。因为就算一开始v,u没边,但不能保证在残量图中没边。u到v一旦有流,v到u就能流回去。


#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<cmath>#include<queue>#include<map>#define INF 0x3f3f3f3f#define MAXN 5010using namespace std;int T,S,N,M,K;int flow[MAXN][MAXN],a[MAXN],p[MAXN];int move[4][2]={-1,0,1,0,0,1,0,-1};struct st{    int y,c;    st(){        c=0;    }};vector<st> V[MAXN];void add_edge(int u,int y){    st cur;    cur.y=y;    cur.c=1;    V[u].push_back(cur);}int Edmonds_Karp(){    queue<int> q;    int f=0,s=0,t=N*S*2+1;    memset(flow,0,sizeof(flow));    while(1){        memset(a,0,sizeof(a));        a[s]=INF;        q.push(s);        while(!q.empty()){            int u=q.front();            q.pop();            for(int i=0;i<V[u].size();i++){                int v=V[u][i].y,cap=V[u][i].c;                if(!a[v]&&flow[u][v]<cap){                    a[v]=min(a[u],cap-flow[u][v]);                    p[v]=u;                    q.push(v);                }            }        }        if(!a[t]) break;        for(int u=t;u!=s;u=p[u]){            flow[p[u]][u]+=a[t];            flow[u][p[u]]-=a[t];        }        f+=a[t];    }    return f;}int main(){    freopen("in.txt","r",stdin);    scanf("%d",&T);    while(T--){        scanf("%d%d%d",&N,&S,&M);        for(int i=0;i<=N*S*2+1;i++) V[i].clear();        int u,v,x,y;        for(int i=1;i<=N;i++)            for(int j=1;j<=S;j++){                v=(i-1)*S+j;                add_edge(v,v+N*S);                if(i==1||j==1||i==N||j==S) add_edge(v+N*S,N*S*2+1);                for(int k=0;k<4;k++){                    x=i+move[k][0],y=j+move[k][1];                    if(x>=1&&x<=N&&y>=1&&y<=S){                        int u=(x-1)*S+y;                        add_edge(v+N*S,u);                    }                }            }        for(int i=0;i<M;i++){            scanf("%d%d",&x,&y);            v=(x-1)*S+y;            add_edge(0,v);        }        if(Edmonds_Karp()<M) printf("not possible\n");        else printf("possible\n");    }    return 0;}


0 0