Relatively easy to see it is a minimal cut.
I use minimum cut + demolition point.
Title means: a gang of thieves think T city stealing, they are currently in the S city.
Now the police want this gang of thieves to be pulled over.
They can only intercept thief passing city, not in the S or T.
In each city to intercept certain costs.
Ask how much cost to a minimum certain the thief to intercept them.
Split for each point (x-> x''), and the costs required by the capacity between the two points in the city interception.
Start and end after the demolition point, between two points capacity inf, because they can not intercept.
In side the two cities, a, b, connected to a'' -> b, b'' -> a.
Find the smallest cut is the least cost.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MV 402
#define ME 80001
#define INF 0x7fffffff
using namespace std;
struct e{
int v, va, n;
}e[ME];
int e_c;
int h[MV], c[MV], p[MV], d[MV], g[MV];
void insert_arc(int u, int v, int w)
{
e[e_c].v = v; e[e_c].va = w;
e[e_c].n = h[u]; h[u] = e_c ++;
swap(u, v);
e[e_c].v = v; e[e_c].va = 0;
e[e_c].n = h[u]; h[u] = e_c ++;
}
void bfs(int de)
{
int u, v;
queue<int> q;
memset(g, 0, sizeof(g));
memset(d, -1, sizeof(d));
d[de] = 0; g[0] = 1;
q.push(de);
while( !q.empty() ) {
u = q.front(); q.pop();
for(int i = h[u]; i != -1; i = e[i].n) {
v = e[i].v;
if( e[i].va || d[v] != -1 ) continue;
d[v] = d[u]+1; q.push(v); g[d[v]] ++;
}
}
}
int i_sap(int s, int de, int VN)
{
int u(s), v, min_d, m_f(0), p_f(INF);
bfs(s); p[u] = u;
for(int i = 0; i <= VN; i ++)
c[i] = h[i];
while( d[s] < VN ) {
loop:
for(int &i = c[u]; i != -1; i = e[i].n) {
v = e[i].v;
if( !e[i].va || d[u] != d[v]+1 ) continue;
p[v] = u; p_f = min(p_f, e[i].va);
u = v;
if( v == de ) {
for(u = p[u]; v != s; v = u, u = p[u]) {
e[c[u]].va -= p_f; e[c[u]^1].va += p_f;
}
m_f += p_f; p_f = INF;
}
goto loop;
}
min_d = VN;
for(int i = h[u]; i != -1; i = e[i].n) {
v = e[i].v;
if( e[i].va && min_d > d[v] ) {
c[u] = i; min_d = d[v];
}
}
g[d[u]] --;
if( !g[d[u]] ) break;
d[u] = min_d+1;
g[d[u]] ++; u = p[u];
}
return m_f;
}
int main(int argc, char const *argv[])
{
int ver, arc, s, de, v, f, t, ss, ee;
while( ~scanf("%d %d %d %d", &ver, &arc, &s, &de) ) {
e_c = 0; memset(h, -1, sizeof(h));
ss = 0; ee = 2*ver+1;
insert_arc(ss, s, INF); insert_arc(de+ver, ee, INF);
for(int i = 1; i <= ver; i ++) {
scanf("%d", &v);
insert_arc(i, i+ver, v);
}
for(int i = 0; i < arc; i ++) {
scanf("%d %d", &f, &t);
insert_arc(f+ver, t, INF);
insert_arc(t+ver, f, INF);
}
printf("%d\n", i_sap(ss, ee, ee));
}
return 0;
}