关闭

UVA - 563 Crimewave【最大流】

标签: acm网络流
236人阅读 评论(0) 收藏 举报
分类:

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

题意:街道相互垂直,每个交点有一家银行。许多强盗某一时刻抢劫不同的银行,离开网格的时候路线不能相交,问是否存在此路线

做法:看下面的代码吧……自己的错误是忘记了长方形的另外两条边==太不应该了

/*************
uva 563
2016-08-30 10:54:18
0	0
C++ 5.3.0
	2725
************/
#include <stdio.h>
#include<cstring>
#include <iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int oo=0x3f3f3f3f;
const int mm=900000;
const int mn=900000;
int node ,scr,dest,edge;
int ver[mm],flow[mm],Next[mm];
int head[mn],work[mn],dis[mn],q[mn];
void prepare(int _node,int _scr,int _dest)
{
    node=_node,scr=_scr,dest=_dest;
    for(int i=0; i<node; ++i)
        head[i]=-1;
    edge=0;
}
void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,Next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,Next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0; i<node; i++)
        dis[i]=-1;
    dis[q[r++]=scr]=0;
    for(l=0; l<r; ++l)
    {
        for(i=head[u=q[l]]; i>=0; i=Next[i])
        {
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)
                    return 1;
            }
        }
    }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)
        return exp;
    for(int &i=work[u],v,tmp; i>=0; i=Next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; i++)
            work[i]=head[i];
        while(delta=Dinic_dfs(scr,oo))
            ret+=delta;
    }
    return ret;
}
int n,m,t,Q;
int main()
{
  //  freopen("cin.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&Q);
        prepare(n*m*2+2,0,n*m*2+1);
        for(int i=1;i<=Q;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            addedge(0,a+(b-1)*n,1);
        }
        for(int i=1;i<=n*m;i++)addedge(i,i+n*m,1);
        //
        for(int i=1+n*m;i<=n+n*m;i++)addedge(i,dest,1);
        for(int i=2;i<=n;i++)addedge((m-1)*n+i+n*m,dest,1);
        for(int i=n+1+n*m;i<=n*m*2;i+=n)addedge(i,dest,1);
        for(int i=2;i<m;i++)addedge(i*n+n*m,dest,1);
       //
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                //(i,j)=>(i-1,j)  (i+1,j)  (i,j+1)  (i,j-1)
                if(i>1) addedge(i+(j-1)*n+n*m,i-1+(j-1)*n,1);
                if(i<n) addedge(i+(j-1)*n+n*m,i+1+(j-1)*n,1);
                if(j>1) addedge(i+(j-1)*n+n*m,i+(j-2)*n,1);
                if(j<m) addedge(i+(j-1)*n+n*m,i+(j)*n,1);
            }
        }
        //printf("%d\n",);
        if(Q==Dinic_flow())puts("possible");
        else puts("not possible");
    }
    return 0;
}


0
0
查看评论

uva563 - 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 rectan...
  • corncsd
  • corncsd
  • 2014-01-22 18:25
  • 343

UVA 563 Crimewave (最大流,拆点)

题意: 有若干罪犯抢银行,要求逃出地图时他们的路线不相交,求是否能达到上述要求。 分析: 路线不相交即每个点每条边只能用一次,即容量为1,点上的流量限制拆点即可。源点连向罪犯所在位置的入点,最外一圈的出点连向汇点,满流即可能。
  • u012965890
  • u012965890
  • 2014-09-04 22:12
  • 1018

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...
  • u011217342
  • u011217342
  • 2013-12-03 19:44
  • 939

UVa 563 - Crimewave 最大流

题目链接:
  • u013368721
  • u013368721
  • 2014-05-31 11:29
  • 889

uva 563 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 ...
  • llx523113241
  • llx523113241
  • 2015-07-18 19:54
  • 482

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...
  • fp_hzq
  • fp_hzq
  • 2012-04-18 14:13
  • 818

Crimewave (Uva 563 最大流拆点)

题意:b个银行被抢,罪犯制定逃跑路线,要求路上上的点和边不重合,问是否存在可行方案。图的边界就是表示逃出去了。 思路:拆点,容量为1,每个点向四周相邻的点连边,容量为1,判断是否满流。
  • u014422052
  • u014422052
  • 2015-08-01 09:28
  • 525

UVa 563 Crimewave / 最大流EK

这题有很多第一次 第一次用了数组表示的邻接表 以前都是用vector 第一次学了拆点 参考大神的 最大流建图是重点 题意看图 就是所有点要到边界 不能转弯超过一次 不能重叠 第一次不懂什么是拆点 看了下这个就懂了 说的蛮好的 http://www.cnblogs.com/scau20110726/a...
  • u011686226
  • u011686226
  • 2013-12-18 18:36
  • 1114

uva 563 Crimewave 最短路径

#include #include #include #include #include #include #include #include #include #include #include #define INF 100000000 using namespace s...
  • qq_24667639
  • qq_24667639
  • 2015-05-17 10:54
  • 392

UVa 563 Crimewave ( 用最大流判断,拆点)

这道题主要思想就是建图,拆点,判断 判断最大流是不是等于银行个数 拆点,每个点只能经过一次,有容量的点都拆成进点和出点,从进点进,从出点出 建图,将所有的点重新编号,bank和的进点和源点相连,容量为1;grid的四框上的点的拆出来的点和汇点相连,容量INF;每个点对应的进入点和出点容...
  • AClion
  • AClion
  • 2013-02-28 11:42
  • 560
    碎碎念
    周小姐,你还想毕业找不到工作吗????
    个人资料
    • 访问:282715次
    • 积分:8845
    • 等级:
    • 排名:第2584名
    • 原创:609篇
    • 转载:25篇
    • 译文:0篇
    • 评论:38条
    友情链接