uva 563 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×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 × m的网点状街区,在这个n × m的街区上,有l家银行。有一伙劫匪要去抢银行,抢完以后要跑,他们逃跑的路线不能重叠,逃出n × m的街区边界才算是逃跑成功。给出街区大小和各银行位置,问是否可以全部成功逃脱。
解题思路:设置一个超级源点,连向所有的银行,再设置一个超级汇点,所有的边界连向这个超级汇点。中间所有街区的十字路口,都要进行拆点。本身拆成两个点(点a,拆成a -> a’),容量为1。还要和四周的点进行连接(点a, a’ ->aup, a’->aleft, a’->aright, a’->adown)。图建完以后,求最大流。

EK算法

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long ll;
const int OF = 2500;
const int INF = 0x3f3f3f3f;
const int N = 5005;
const int FIN = 5001;
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, m, l;

struct Edge{
    int from, to, cap, flow; 
};

vector<Edge> edges;
vector<int> G[N];

void init() {
    for (int i = 0; i < n * n; i++) G[i].clear();
    edges.clear();
}

void addEdge(int from, int to, int cap, int flow) {
    edges.push_back((Edge){from, to, cap, 0});
    edges.push_back((Edge){to, from, 0, 0});
    int m = edges.size();
    G[from].push_back(m - 2);
    G[to].push_back(m - 1);
} 

void input() {
    scanf("%d %d %d", &n, &m, &l);  
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (i == 1 || j == 1 || i == n || j == m) {
                addEdge((i - 1) * m + j + OF, FIN, 1, 0);   
            }
            addEdge((i - 1) * m + j, (i - 1) * m + j + OF, 1, 0);
            for (int k = 0; k < 4; k++) {
                int x = i + dir[k][0], y = j + dir[k][1];
                if (x <= 0 || x > n) continue;
                if (y <= 0 || y > m) continue;
                addEdge((i - 1) * m + j + OF, (x - 1) * m + y, 1, 0);
            }
        }   
    }
    int a, b;
    for (int i = 0; i < l; i++) {
        scanf("%d %d", &a, &b); 
        addEdge(0, (a - 1) * m + b, 1, 0);
    }
}

int EK() {
    int ans = 0;
    int a[N], pre[N];
    queue<int> Q;
    while (1) {
        memset(a, 0, sizeof(a));
        memset(pre, 0, sizeof(pre));
        a[0] = INF;
        Q.push(0);
        while (!Q.empty()) {
            int u = Q.front(); Q.pop(); 
            for (int i = 0; i < G[u].size(); i++) {
                Edge &e = edges[G[u][i]];
                if (!a[e.to] && e.cap > e.flow) {
                    a[e.to] = min(a[u], e.cap - e.flow);    
                    pre[e.to] = G[u][i];
                    Q.push(e.to);
                }
            }
        //  if (a[FIN]) break;
        }
        if (a[FIN] == 0) break;
        ans += a[FIN];
        for (int v = FIN; v != 0; v = edges[pre[v]].from) {
            edges[pre[v]].flow += a[FIN];   
            edges[pre[v]^1].flow -= a[FIN];
        }
    }
    return ans;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int ans;    
        init();
        input();
        ans = EK();
        if (ans == l) printf("possible\n");
        else printf("not possible\n");
    }
    return 0;
}

Dinic算法

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long ll;
const int OF = 2500;
const int INF = 0x3f3f3f3f;
const int N = 5005;
const int FIN = 5001;
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, m, l, s, t;

struct Edge{
    int from, to, cap, flow; 
};

vector<Edge> edges;
vector<int> G[N];

void init() {
    s = 0, t = FIN;
    for (int i = 0; i < FIN; i++) G[i].clear();
    edges.clear();
}

void addEdge(int from, int to, int cap, int flow) {
    edges.push_back((Edge){from, to, cap, 0});
    edges.push_back((Edge){to, from, 0, 0});
    int m = edges.size();
    G[from].push_back(m - 2);
    G[to].push_back(m - 1);
} 

void input() {
    scanf("%d %d %d", &n, &m, &l);  
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (i == 1 || j == 1 || i == n || j == m) {
                addEdge((i - 1) * m + j + OF, FIN, 1, 0);   
            }
            addEdge((i - 1) * m + j, (i - 1) * m + j + OF, 1, 0);
            for (int k = 0; k < 4; k++) {
                int x = i + dir[k][0], y = j + dir[k][1];
                if (x <= 0 || x > n) continue;
                if (y <= 0 || y > m) continue;
                addEdge((i - 1) * m + j + OF, (x - 1) * m + y, 1, 0);
            }
        }   
    }
    int a, b;
    for (int i = 0; i < l; i++) {
        scanf("%d %d", &a, &b); 
        addEdge(0, (a - 1) * m + b, 1, 0);
    }
}

int vis[N], d[N];
int BFS() {
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    Q.push(s);
    d[s] = 0;
    vis[s] = 1;
    while (!Q.empty()) {
        int u = Q.front(); Q.pop(); 
        for (int i = 0; i < G[u].size(); i++) {
            Edge &e = edges[G[u][i]];   
            if (!vis[e.to] && e.cap > e.flow) {
                vis[e.to] = 1;  
                d[e.to] = d[u] + 1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}

int cur[N];
int DFS(int u, int a) {
    if (u == t || a == 0) return a;
    int flow = 0, f; 
    for (int &i = cur[u]; i < G[u].size(); i++) {
        Edge &e = edges[G[u][i]];
        if (d[u] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
            e.flow += f;    
            edges[G[u][i]^1].flow -= f;
            flow += f;
            a -= f;
            if (a == 0) break;
        }
    }
    return flow;
}
int MF() {
    int ans = 0;
    while (BFS()) {
        memset(cur, 0, sizeof(cur));    
        ans += DFS(s, INF);
    }
    return ans;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int ans;    
        init();
        input();
        ans = MF();
        if (ans == l) printf("possible\n");
        else printf("not possible\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值