设为首页 - 加入收藏 焦点技术网
热搜:java
当前位置:首页 >

UVA 563 Crimewave (最大流,拆点)

2014-09-04 22:12:00.0 图论 网络流 ACM 网络流 图论 algorithm  
导读:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=504  Crimewave Nieuw Knollendam is a very modern town. This becomes clear already when looking at the lay...。。。

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

  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 

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

Sample Output 

possiblenot possible



Miguel A. Revilla 
1998-03-10

题意:

有若干罪犯抢银行,要求逃出地图时他们的路线不相交,求是否能达到上述要求。

分析:

路线不相交即每个点每条边只能用一次,即容量为1,点上的流量限制拆点即可。源点连向罪犯所在位置的入点,最外一圈的出点连向汇点,满流即可能。


/* * * Author : fcbruce * * Date : 2014-09-04 21:26:22  * */#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define sqr(x) ((x)*(x))#define LL long long#define itn int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#ifdef _WIN32 #define lld "%I64d"#else #define lld "%lld"#endif#define maxm 2333333#define maxn 8964using namespace std;int fir[maxn];int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm];int e_max;int iter[maxn],q[maxn],lv[maxn];void add_edge(int _u,int _v,int _w){    int e;    e=e_max++;    u[e]=_u;v[e]=_v;cap[e]=_w;    nex[e]=fir[u[e]];fir[u[e]]=e;    e=e_max++;    u[e]=_v;v[e]=_u;cap[e]=0;    nex[e]=fir[u[e]];fir[u[e]]=e;}void dinic_bfs(int s){    int f,r;    memset(lv,-1,sizeof lv);    q[f=r=0]=s;    lv[s]=0;    while(f<=r)    {        int x=q[f++];        for (int e=fir[x];~e;e=nex[e])        {            if (cap[e]>flow[e] && lv[v[e]]<0)            {                lv[v[e]]=lv[u[e]]+1;                q[++r]=v[e];            }        }    }}int dinic_dfs(int _u,int t,int _f){    if (_u==t)  return _f;    for (int &e=iter[_u];~e;e=nex[e])    {        if (cap[e]>flow[e] && lv[_u]0)            {                flow[e]+=_d;                flow[e^1]-=_d;                return _d;            }        }    }    return 0;}int max_flow(int s,int t){    memset(flow,0,sizeof flow);    int total_flow=0;    for (;;)    {        dinic_bfs(s);        if (lv[t]<0)    break;        memcpy(iter,fir,sizeof iter);        int _f;        while ((_f=dinic_dfs(s,t,INF))>0)            total_flow+=_f;    }    return total_flow;}int main(){ #ifdef FCBRUCE  freopen("/home/fcbruce/code/t","r",stdin); #endif // FCBRUCE  int T_T;  scanf( "%d",&T_T);  while (T_T--) {  int n,m,s=0,t=8963;  scanf( "%d%d",&n,&m);    e_max=0;  memset(fir,-1,sizeof fir);    for (int i=1;i<=n;i++)   for (int j=1;j<=m;j++)   {    add_edge(i*m+j+n*m,i*m+j,1);        if (i==n || j==m) continue;        add_edge(i*m+j,(i+1)*m+j+n*m,1);    add_edge(i*m+j,i*m+j+1+n*m,1);        add_edge((i+1)*m+j,i*m+j+n*m,1);    add_edge(i*m+j+1,i*m+j+n*m,1);   }     for (int i=1;i<=m;i++)  {   add_edge(1*m+i,t,1);   add_edge(n*m+i,t,1);  }    for (int i=2;i

(编辑: u012965890)

网友评论
相关文章