关闭

UVA 563 - Crimewave(最大流)

901人阅读 评论(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 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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1355178次
    • 积分:28721
    • 等级:
    • 排名:第192名
    • 原创:1481篇
    • 转载:8篇
    • 译文:0篇
    • 评论:343条
    文章分类
    最新评论