Control(网络流-最小割-最大流)
judge:HDUOJ 4289
source:2012 ACM/ICPC Asia Regional Chengdu Online
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description
You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
http://acm.hdu.edu.cn/showproblem.php?pid=4289
Input
http://acm.hdu.edu.cn/showproblem.php?pid=4289
Output
http://acm.hdu.edu.cn/showproblem.php?pid=4289
Sample Input
5 6
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1
Sample Output
3
题意
如何用最小的花费堵死歹徒所有的路,即求最小割(最大流)
(感觉更像是板子们在比赛板子而不是我们……)
丢下我的御用板子:
代码
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
const int maxn = 810;
const int maxm = 640010;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f;
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
int S, T;
struct edge {
int to, cap, rev; //用于表示边的结构体(终点,容量,反向边)
edge() {}
edge(int t, int cap, int rev) :to(t), cap(cap), rev(rev) {}
};
vector <edge> _maxflow_G[maxm]; //图的邻接表表示
int _maxflow_dis[maxm]; //顶点到源点的距离标号
int _maxflow_cur[maxm]; //当前弧,在其之前的边已经没有用了
void _maxflow_init(){
for(int i = 0; i < maxn; i++) _maxflow_G[i].clear();
}
void _maxflow_addedge(int from, int to, int cap) {
_maxflow_G[from].push_back(edge(to, cap, _maxflow_G[to].size()));
_maxflow_G[to].push_back(edge(from, 0, _maxflow_G[from].size() - 1));
}
void _maxflow_bfs(int s) {//通过_maxflow_bfs计算从源点出发的距离标号
memset(_maxflow_dis, -1, sizeof(_maxflow_dis));
queue <int> q;
_maxflow_dis[s] = 0;
q.push(s);
while (!q.empty()) {
int v = q.front(); q.pop();
for (int i = 0; i < _maxflow_G[v].size(); i++) {
edge &e = _maxflow_G[v][i];
if (e.cap > 0 && _maxflow_dis[e.to] < 0) {
_maxflow_dis[e.to] = _maxflow_dis[v] + 1;
q.push(e.to);
}
}
}
}
int _maxflow_dfs(int v, int t, int f) {//通过_maxflow_dfs寻找增广路
if (v == t) return f;
for (int &i = _maxflow_cur[v]; i < _maxflow_G[v].size(); i++) {
edge &e = _maxflow_G[v][i];
if (e.cap > 0 && _maxflow_dis[v] < _maxflow_dis[e.to]) {
int d = _maxflow_dfs(e.to, t, min(f, e.cap));
if (d > 0) {
e.cap -= d;
_maxflow_G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int maxflow(int s, int t) {
int flow = 0;
for (;;) {
_maxflow_bfs(s); //计算层次图
if (_maxflow_dis[t] < 0) return flow; //找不到s-t路径
memset(_maxflow_cur, 0, sizeof(_maxflow_cur)); //初始化当前弧
int f;
while ((f = _maxflow_dfs(s, t, INF)) > 0) { //更新最大流
flow += f;
}
}
return flow;
}
int a[maxn];
int n, m;
int main() {
freopen("in.txt", "r", stdin);
while (scanf("%d%d", &n, &m) != EOF) {
scanf("%d%d", &S, &T);
_maxflow_init();
_rep(i, 1, n) {
int x;
scanf("%d", &x);
_maxflow_addedge(i, n + i, x);
}
_rep(i, 1, m) {
int u, v;
scanf("%d%d", &u, &v);
_maxflow_addedge(n + u, v, inf);
_maxflow_addedge(n + v, u, inf);
}
printf("%d\n", maxflow(S, n + T));
}
return 0;
}