uva 563 Crimewave(最大流)
2015-07-18 19:54
429 查看
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)(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.
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
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)(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.
[code]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算法
[code]#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 ; 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 , pre ; 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算法
[code]#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 ; 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 , d ; 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 ; 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; }
相关文章推荐
- 基于PaaS人事部门间平台多重身份的技术解决方案
- 纯css改变radio样式
- 用CI框架向数据库中实现简单的增删改查
- NYOJ 77
- Dell poweredge r210进BIOS修改磁盘控制器(SATA Controller)接口模式
- Mac使用pyenv来进行python版本管理
- Android Studio 那些事|Class Editor Tab页卡不显示问题
- 利用UML进行建模
- 正则表达式基本定义及应用
- PyQt 分离UI主线程与工作线程
- 【bzoj3687】 简单题 bitset
- 左外连接、右外连接、内连接、全连接
- Unity3D使用腾讯广点通广告
- 算法之递推思想
- MJRefresh下拉刷新框架使用
- [MAC使用] rz sz 命令
- java线程同步
- [HNOI2001][BZOJ1221] 软件开发
- 根据flat.vmdk文件恢复磁盘(完善版)
- 字符串创建和存储机制2