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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值