解题报告 之 UVA563 Crimewave

解题报告 之 UVA563 Crimewave


Description

Download as PDF

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



Miguel A. Revilla
1998-03-10


题目大意:给你一个l*w的地图,现在从某些交叉点(银行)出发,走折线到达图边界点,要求任意一个点最多只能途径一次,问是否有满足要求的方案?只需判断possible或者not possible。


分析:明显赤裸裸的最大流。其实这个题点有2500个算多的了,而且还要拆点,不过好在边的数量比较固定,大概是大于4*2500一点。所以整体建图的思路是每个点先拆点限制流量,然后与它四周的点连接,负载为1,边界的点与超级汇点连接,负载为1,所有银行与超级源点相连,负载为1。注意连接时,起出点为拆点的出点,而终点为拆点的入点。然后跑最大流,如果最大流==银行数,则是possible。


上代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int MAXN = 6010;
const int MAXM = 200100;
const int INF = 0x3f3f3f3f;

struct Edge
{
	int to, cap, next;
};

Edge edge[MAXM];
int level[MAXN];
int head[MAXN];
int src, des, cnt;

void addedge( int from, int to, int cap )
{
	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].next = head[from];
	head[from] = cnt++;

	swap( from, to );

	edge[cnt].to = to;
	edge[cnt].cap = 0;
	edge[cnt].next = head[from];
	head[from] = cnt++;

}

int bfs()
{
	memset( level, -1, sizeof level );
	queue<int> q;
	while (!q.empty())
		q.pop();

	level[src] = 0;
	q.push( src );
	
	while (!q.empty())
	{
		int u = q.front();
		q.pop();

		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (edge[i].cap > 0 && level[v] == -1)
			{
				level[v] = level[u] + 1;
				q.push( v );
			}
		}
	}
	return level[des] != -1;
}

int dfs( int u, int f )
{
	if (u == des) return f;
	int tem;
	
	for (int i = head[u]; i != -1; i=edge[i].next)
	{
		int v = edge[i].to;
		if (edge[i].cap > 0 && level[v] == level[u] + 1)
		{
			tem = dfs( v, min( f, edge[i].cap ) );
			if (tem > 0)
			{
				edge[i].cap -= tem;
				edge[i^1].cap += tem;
				return tem;
			}
		}
	}
	level[u] = -1;
	return 0;
}

int Dinic()
{
	int ans = 0, tem;
	while (bfs())
	{
		while (tem = dfs( src, INF ))
		{
			ans += tem;
		}
	}
	return ans;
}

int main()
{
	int kase;
	cin >> kase;
	int n;
	src = 0;
	des = 5005;
	int l, w;
	while (kase--)
	{
		cin >> l >> w >> n;
		memset( head, -1, sizeof head );
		cnt = 0;

		for (int i = 0; i < l; i++)
		{
			for (int j = 0; j < w; j++)
			{
				int o = i*w + j + 1;
				addedge( o, o + 2500, 1 );
				bool bor = false;
				if (i>0) addedge( o + 2500, o - w, 1 ); else bor = true;
				if (i < l - 1)addedge( o + 2500, o + w, 1 ); else bor = true;
				if (j>0) addedge( o + 2500, o - 1, 1 ); else bor = true;
				if (j < w - 1) addedge( o + 2500, o + 1, 1 ); else bor = true;
				if (bor) addedge( o + 2500, des, 1 );
			}
		}

		for (int i = 1; i <= n; i++)
		{
			int x, y;
			cin >> x >> y;
			x--, y--;
			int o = x*w + y + 1;
			addedge( src, o, 1 );
		}

		if (Dinic() == n)
			cout << "possible" << endl;
		else
			cout << "not possible" << endl;
	}
	return 0;
}

最大流艹过半啦!继续加油!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值