题目:
http://acm.hdu.edu.cn/showproblem.php?pid=4289
题意:
恐怖份子通过高速公路要从起点城市到目标城市运送武器,高速公路只能在城市中进出,如果所经过的某个城市有特工,恐怖份子就会被抓获,但是与某个城市的特工保持联络需要一些花费,现在挑选一些城市,恐怖份子至少会经过其中一个(也就是会被抓),并且使花费最小,输出最小的花费
思路:
其实就是求最小割。把每个城市拆点连边,容量为它的花费,把任意两个相连的城市的边容量设为无穷大,这样最小割就是答案
总结:
好久没切网络流了,手生了,这种1A题都给哇了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;
const int N = ;
const int INF = ;
struct edge
{
int to, cap, next;
} g[N*N*];
int cnt, head[N], level[N], gap[N], cur[N], pre[N];
int ss, tt, nv;
void add_edge(int v, int u, int cap)
{
g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
g[cnt].to = v, g[cnt].cap = , g[cnt].next = head[u], head[u] = cnt++;
}
int sap(int s, int t)
{
memset(level, , sizeof level);
memset(gap, , sizeof gap);
memcpy(cur, head, sizeof head);
gap[] = nv;
int v = pre[s] = s, flow = , aug = INF;
while(level[s] < nv)
{
bool flag = false;
for(int &i = cur[v]; i != -; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > && level[v] == level[u] + )
{
flag = true;
pre[u] = v;
v = u;
aug = min(aug, g[i].cap);
if(v == t)
{
flow += aug;
while(v != s)
{
v = pre[v];
g[cur[v]].cap -= aug;
g[cur[v]^].cap += aug;
}
aug = INF;
}
break;
}
}
if(flag) continue;
int minlevel = nv;
for(int i = head[v]; i != -; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > && minlevel > level[u])
minlevel = level[u], cur[v] = i;
}
if(--gap[level[v]] == ) break;
level[v] = minlevel + ;
gap[level[v]]++;
v = pre[v];
}
return flow;
}
int main()
{
int n, m;
while(~ scanf("%d%d", &n, &m))
{
cnt = ;
memset(head, -, sizeof head);
int a, b, c, d;
scanf("%d%d", &a, &b);
for(int i = ; i <= n; i++)
{
scanf("%d", &c);
add_edge(i, i + n, c);
}
for(int i = ; i <= m; i++)
{
scanf("%d%d", &c, &d);
add_edge(c + n, d, INF);
add_edge(d + n, c, INF);
}
ss = a, tt = b + n;
nv = * n;
printf("%d\n", sap(ss, tt));
}
return ;
}