HDU - 4289 Control(拆点+网络流||最小割)
ControlTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4578 Accepted Submission(s): 1890 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.
Input There are several test cases.
Output For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
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
Source 2012 ACM/ICPC Asia Regional Chengdu Online
|
意思就不多讲了,这道题其实最难的地方是在建立边得判断上,看起来是双向边,但是在拆点之后其实是单向了,而且每一个城市边要加两条(a后b前,b后a前)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 100010;
const int maxm = 1000100;
struct *** {
int v, w, ne;
}ed[maxm];
int n, m, cnt;
int head[maxn], dis[maxn], cur[maxn];
void init() {
cnt = 0;
memset(head, -1, sizeof(head));
}
void add(int u, int v, int w) {
ed[cnt].v = v; ed[cnt].w = w;
ed[cnt].ne = head[u]; head[u] = cnt++;
ed[cnt].v = u; ed[cnt].w = 0;
ed[cnt].ne = head[v]; head[v] = cnt++;
}
int bfs(int st, int en) {
queue<int>q;
memset(dis, 0, sizeof(dis));
dis[st] = 1;
q.push(st);
while (!q.empty()) {
int u = q.front(); q.pop();
if (u == en)return 1;
for (int s = head[u]; ~s; s = ed[s].ne) {
int v = ed[s].v;
if (dis[v] == 0 && ed[s].w > 0) {
dis[v] = dis[u] + 1; q.push(v);
}
}
}
return dis[en] != 0;
}
int dfs(int st, int en, int flow) {
int ret = flow, a;
if (st == en || flow == 0)return flow;
for (int &s = cur[st]; ~s; s = ed[s].ne) {
int v = ed[s].v;
if (dis[v] == dis[st] + 1 && (a = dfs(v, en, min(ret, ed[s].w)))) {
ed[s].w -= a;
ed[s ^ 1].w += a;
ret -= a;
if (!ret)break;
}
}
if (ret == flow)dis[st] = 0;
return flow - ret;
}
int dinic(int st, int en) {
int ans = 0;
while (bfs(st, en)) {
for (int s = 1; s <= n; s++)
cur[s] = head[s];
ans += dfs(st, en, inf);
}
return ans;
}
int main() {
while (~scanf("%d%d", &n, &m)) {
init();
int st, en;
scanf("%d%d", &st, &en);
for (int s = 1; s <= n; s++) {
int t; scanf("%d", &t);
add(2 * s - 1, 2 * s, t);
}
for (int s = 1; s <= m; s++){
int a, b;
scanf("%d%d", &a, &b);
add(2 * a, 2 * b - 1, inf);
add(2 * b, 2 * a - 1, inf);
}
n = 2 * n;
int ans = dinic(2 * st-1, 2 * en);
cout << ans << endl;
}
}