您的位置:首页 > 理论基础 > 计算机网络

(beginer) 网络流(拆点) UVA 563 Crimewave

2014-03-03 16:16 381 查看


  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

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 (

), followed by the number
a of avenues (

), followed by the number
b (

) 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


and


.

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

题意:有一个网格图,网格上面有一些点有银行,你要设计b条从银行逃出边界的路线,要求这些我路线不会经过一个交叉点两次及以上,问可不可能。



思路:我们把网格图的交叉点作为点,边自然就作为边,但是由于一个交叉点只能经过一次,及进来一次,出去一次,所以我们把交叉点拆成两个点,从外面进来的都指向一个点,从这个点出去都是从另外一个点出去。这样就能满足一个交叉点只能经过一次了。然后建好图后我们就网络流之,看最大流是不是b,是的话就可能,不是的话,就不可能。



代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
const int inf = 1e8;
const int maxn = 4*50 * 50 + 5;
#define LL long long

struct Edge
{
int u, v;
int cap, flow;
Edge(int u, int v, int cap, int flow)
:u(u), v(v), cap(cap), flow(flow) { }
};

vector<Edge> edges;
vector<int> G[maxn];
void add(int u, int v, int cap)
{
edges.push_back(Edge(u, v, cap, 0));
edges.push_back(Edge(v, u, 0, 0));
int m = edges.size();
G[u].push_back(m - 2);
G[v].push_back(m - 1);
}

struct ISAP
{
int cur[maxn], d[maxn], p[maxn], num[maxn];
int n, s, t;
void init(int n)
{
this->n = n;
}
int Augment()
{
int x = t, a = inf;
while (x != s)
{
Edge &e = edges[p[x]];
a = min(a, e.cap - e.flow);
x = e.u;
}
x = t;
while (x != s)
{
edges[p[x]].flow += a;
edges[p[x] ^ 1].flow -= a;
x = edges[p[x]].u;
}
return a;
}

void bfs()
{
queue<int> q;
q.push(t);
for (int i = 0; i < n; ++i) d[i] = inf;
d[t] = 0;
while (q.size())
{
int x = q.front(); q.pop();
for (int i = 0; i < G[x].size(); ++i)
{
Edge & e = edges[G[x][i]];
if (e.cap>0 || d[e.v] <= d[x] + 1) continue;
d[e.v] = d[x] + 1;
q.push(e.v);
}
}
}

int maxflow(int s, int t)
{
this->s = s, this->t = t;
int flow = 0;
bfs();
memset(num, 0, sizeof(num));
for (int i = 0; i < n; ++i)
if(d[i]!=inf) num[d[i]]++;
int x = s;
memset(cur, 0, sizeof(cur));
while (d[s] < n)
{
if (x == t)
{
flow += Augment();
x = s;
}
int ok = 0;
for (int i = cur[x]; i < G[x].size(); ++i)
{
Edge&e = edges[G[x][i]];
if (e.cap>e.flow&&d[x] == d[e.v] + 1)
{
ok = true;
p[e.v] = G[x][i];
cur[x] = i;
x = e.v;
break;
}
}
if (!ok)
{
int m = n - 1;
for (int i = 0; i < G[x].size(); ++i)
{
Edge&e = edges[G[x][i]];
if (e.cap>e.flow) m = min(m, d[e.v]);
}
if (--num[d[x]] == 0) break;
num[d[x] = m + 1]++;
cur[x] = 0;
if (x != s) x = edges[p[x]].u;
}
}
return flow;
}
}solver;

int n, m, b;
const int Move[2][4] = { { -1, 0, 1, 0 }, { 0, 1, 0, -1 } };
inline bool inRange(int r, int c) { return 0 <= r&&r < n && 0 <= c&&c < m; }

inline int getx(int r, int c) { return 2*(r*m + c); }
void input()
{
for (int i = 0; i < maxn; ++i) G[i].clear();
edges.clear();
scanf("%d", &b);
for (int i = 0; i < b; ++i)
{
int r, c;
scanf("%d%d", &r, &c);
--r,--c;
int x = getx(r, c)+1;
add(x, 2*n*m + 1, 1);
}
for (int i = 0; i < n; ++i)
{
int x = getx(i, 0);
add(2*n*m, x, 1);
x = getx(i, m - 1);
add(2*n*m, x, 1);
}
for (int i = 1; i < m - 1; ++i)
{
int x = getx(0, i);
add(2*n*m, x, 1);
x = getx(n - 1, i);
add(2*n*m, x, 1);
}
for (int r = 0; r < n;++r)
for (int c = 0; c < m; ++c)
add(getx(r, c), getx(r, c) + 1, 1);
for (int r = 0; r < n; ++r)
for (int c = 0; c < m; ++c)
for (int k = 0; k < 4; ++k)
{
int rr = r + Move[0][k];
int cc = c + Move[1][k];
if (!inRange(rr, cc)) continue;
int x = getx(r, c)+1;
int y = getx(rr, cc);
add(x, y, 1);
}
}

void solve()
{
solver.init(2*n*m + 2);
int flow = solver.maxflow(2*n*m, 2*n*m + 1);
if (flow == b) puts("possible");
else puts("not possible");
}

int main()
{
int T; cin >> T;
while (T--)
{
scanf("%d%d", &n, &m);
input();
solve();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM algorithm uva 网络流