UVA 563 - Crimewave(最大流)

原创 2013年12月03日 19:44:06

  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 

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个银行,现在去抢,要求所有抢的路线不能交叉,并且都能逃回边界。

思路:最大流,银行和源点连接,边界和汇点连接,所有点和周围4个点连接,容量都为1,所以要拆点,点数有2500最多,所以要用邻接表

代码:

#include <stdio.h>
#include <string.h>
#include <queue>
#define min(a,b) (a)<(b)?(a):(b)
#define INF 0x3f3f3f3f
using namespace std;
const int N = 5050;

int T, S, A, B, x, y, g[N][N], a[N], p[N], f[N][N];
queue<int>q;

void init() {
    scanf("%d%d%d", &S, &A, &B);
    int n = A * A;
    int t = 2 * n + 1;
    for (int i = 1; i <= n; i ++)
	g[i][i + n] = INF;
    for (int i = 1; i <= A; i ++) {
	g[i + n][t] = 1;
	g[n - i + 1 + n][t] = 1;
	g[1 + (i - 1) * A + n][t] = 1;
	g[i * A + n][t] = 1;
    }
    for (int i = 2; i <= A; i ++)
	for (int j = 2; j <= A; j ++) {
	    g[(i - 1) * A + j + n][(i - 1) * A + j - 1] = 1;
	    g[(i - 1) * A + j + n][(i - 1) * A + j + 1] = 1;
	    g[(i - 1) * A + j + n][(i - 2) * A + j] = 1;
	    g[(i - 1) * A + j + n][i * A + j] = 1;
	}
    for (int i = 0; i < B; i ++) {
	scanf("%d%d", &x, &y);
	g[(x - 1) * A + y][(x - 1) * A + y + n] = 1;
	g[0][(x - 1) * A + y] = 1;
    }
}

void solve() {
    init();
    int s = 0, t = 2 * A + 1, ans = 0;
    memset(f, 0, sizeof(s));
    while (1) {
	memset(a, 0, sizeof(a));
	a[s] = INF;
	q.push(s);
	while (!q.empty()) {
	    int u =q.front(); q.pop();
	    for (int v = 1; v <= t; v ++) {
		if (!a[v] && g[u][v] > f[u][v]) {
		    p[v] = u; q.push(v);
		    a[v] = min(a[u], g[u][v] - f[u][v]);
		}
	    }
	}
	if (!a[t]) break;
	for (int u = t; u != s; u = p[u]) {
	    f[p[u]][u] += a[t];
	    f[u][p[u]] -= a[t];
	}
	ans += a[t];
    }
    printf("%d\n", ans);
    if (ans < B)
	printf("not possible\n");
    else
	printf("possible\n");
}
int main() {
    scanf("%d", &T);
    while (T --) {
	solve();
    }
    return 0;
}


uva 563 - Crimewave(网络流最大流)

题目链接:uva 563 - Crimewave 题目大意:在一个r*c的城市中,有n个银行,有一个抢匪想抢劫这n家银行,每次抢劫后需逃离城市(移动到坐标外),但是又不能走先前走过的位置,给...
  • u011328934
  • u011328934
  • 2013年11月22日 14:48
  • 1302

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

这道题主要思想就是建图,拆点,判断 判断最大流是不是等于银行个数 拆点,每个点只能经过一次,有容量的点都拆成进点和出点,从进点进,从出点出 建图,将所有的点重新编号,bank和的进点和源...
  • AClion
  • AClion
  • 2013年02月28日 11:42
  • 554

uva 563(最大流)

题意 题解:
  • u013392752
  • u013392752
  • 2014年11月17日 21:37
  • 463

UVa 563 Crimewave (网络流构图+最大流+挺好的+双向边)

#include #include #include #include #include #include using namespace std; typedef long long l...
  • Slow_Wakler
  • Slow_Wakler
  • 2015年08月08日 21:47
  • 1021

UVa 10480 Sabotage ( 最小割最大流定理)

这是一道很典型的最小割最大流定理,通过这道题,我再一次学习了最小割的定义 最小割,就是在所有割中,容量之和最小的割,这就是我的理解,而最小割的值就是最大流的值,因为很容易想到,从源点s到汇点t的最大...
  • AClion
  • AClion
  • 2013年02月28日 10:21
  • 1287

UVa 10594 - Data Flow(无向图的最小费用最大流问题)

题目链接:UVa 10594 - Data Flow 这道题是无向图的最小费用最大流问题,看清楚是无向图的。这么说无向图和有向图的费用流问题有什么区别呢?主要是反向边的问题。首先我们说一下最大流问题中...
  • fobdddf
  • fobdddf
  • 2014年02月21日 13:50
  • 5758

Uva 563 网络流

题目链接:点击打开链接 题意:给定s*a的方格点,有b个坐标是有且仅有一个人的。 每个点只能被经过一次 能不能让所有人都移动到矩阵边缘。 拆点一下,建图还是挺明显的。。 太卡了提交半天没结果,贴一...
  • qq574857122
  • qq574857122
  • 2014年05月26日 20:45
  • 876

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

Crimewave  Nieuw Knollendam is a very modern town. This becomes clear already when looking at ...
  • corncsd
  • corncsd
  • 2014年01月22日 18:25
  • 337

UVA 753(最大流匹配)

s - >往M个设备流1, 然后每个设备往对应插口号流1,然后插口号之间的K个转换边流无穷大(代表该装备能被利用无穷遍),然后插排号流1到对应的N个插排,然后每个插排流1到汇点。 //#pragma...
  • playwfun
  • playwfun
  • 2015年09月10日 19:39
  • 231

uva 10806(最大流最小费)

题意: 题解: #include #include const int N = 105; const int M = 10000; const int INF = 0x3f3f3f3f; int ...
  • u013392752
  • u013392752
  • 2014年11月15日 01:05
  • 593
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA 563 - Crimewave(最大流)
举报原因:
原因补充:

(最多只允许输入30个字)