Crimewave (Uva 563 最大流拆点)

Crimewave (Uva 563 最大流拆点)

分类:ACM网络流

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

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.

InputThe 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 numbera of avenues (

),followed by the numberb ()of banks to be robbed.Then b lines follow, each containing the location of a bank in the formof two numbersx (the number of the street) and y (the number of the avenue). Evidentlyand.OutputThe 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, thisline should contain the word:possible. If this is not possible,the line should contain the words not possible.Sample Input26 6 104 13 24 25 23 44 45 43 64 65 65 5 53 22 33 34 33 4Sample Outputpossiblenot possible

Miguel A. Revilla1998-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 pfprintf#define DBGpf("Hi\n")typedef long long ll;using namespace std;#define INF 0x3f3f3f3f#define mod 1000000009const 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_JUDGEfreopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);#endifint 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);elseaddedge((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");elsepf("not possible\n");}return 0;}

那里面非常漂亮,个个观景区都能看到奇形怪状的岩石。

Crimewave (Uva 563 最大流拆点)

相关文章:

你感兴趣的文章:

标签云: