题目链接:Control
题目大意:给出s和t点,求在图中找出最少的点集,使s和t不联通。
解题思路:拆点,把每个点变成一条边,权值为此点的花费,不同点之间若相连,则权值为正无穷。最后跑一遍最大流就能得到解了。
代码如下:
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const int maxn = 2e4 + 15;
struct Edge {
int to, cap, next;
};
int ecnt, st, ed, n, m;
Edge es[maxn * 10];
int cur[505], dep[505], head[505], cost[505];
class Dinic {
public:
void init(){
memset(head, -1, sizeof head);
ecnt = 0;
}
void add_edge(int u, int v, int cap) {
es[ecnt].to = v;
es[ecnt].next = head[u];
es[ecnt].cap = cap;
head[u] = ecnt++;
}
void add_double(int u, int v, int w1, int w2 = 0) {
add_edge(u, v, w1);
add_edge(v, u, w2);
}
bool BFS() {
queue<int> q; q.push(st);
memset(dep, 0x3f, sizeof dep); dep[st] = 0;
while(q.size() && dep[ed] == inf) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = es[i].next) {
Edge& e = es[i];
if(e.cap > 0 && dep[e.to] == inf) {
dep[e.to] = dep[u] + 1;
q.push(e.to);
}
}
}
return dep[ed] < inf;
}
int DFS(int u, int maxflow) {
if(u == ed) return maxflow;
int res = 0;
for(int i = cur[u]; ~i; i = es[i].next) {
Edge& e = es[i];
if(dep[e.to] == dep[u] + 1 && e.cap > 0) {
int flow = DFS(e.to, min(maxflow, e.cap));
cur[u] = i;
res += flow; maxflow -= flow;
es[i].cap -= flow;
es[i ^ 1].cap += flow;
if(!maxflow) return res;
}
}
dep[u] = inf;
return res;
}
int MaxFlow(){
int ans = 0;
while(BFS()) {
for(int i = st; i <= ed; i++) cur[i] = head[i];
ans += DFS(st, inf);
}
return ans;
}
};
inline int read() {
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = 0;
while(isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
int main(){
#ifdef NEKO
freopen("Nya.txt", "r", stdin);
#endif
Dinic dic;
while(scanf("%d%d", &n, &m) != EOF) {
int s, t;
s = read(); t = read();
st = 0; ed = 2 * n + 1;
dic.init();
dic.add_double(st, s, inf);
dic.add_double(t + n, ed, inf);
for(int i = 1; i <= n; i++) {
cost[i] = read();
dic.add_double(i, i + n, cost[i]);
}
for(int i = 1; i <= m; i++) {
int u, v; u = read(); v = read();
dic.add_double(u + n, v, inf);
dic.add_double(v + n, u, inf);
}
cout << dic.MaxFlow() << endl;
}
return 0;
}