Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <queue>
- using namespace std;
- const int N = 5555;
- const int M = 222222;
- const int INF = 1000000000;
- int es[M], ev[M], next[M], first[N], c, n, m, d[N], s, t, ans, l[N], last[N], p, q, tmp;
- void add(int x, int y, int z) {
- next[++c] = first[x];
- first[x] = c;
- es[c] = y;
- ev[c] = z;
- }
- int be(int x) {
- if (x & 1) {
- return x + 1;
- }
- return x - 1;
- }
- void bfs() {
- queue<int> q;
- q.push(s);
- memset(l, 63, sizeof(l));
- l[s] = 0;
- while (!q.empty()) {
- int v = q.front();
- q.pop();
- for (int h = first[v]; h; h = next[h]) {
- if (l[es[h]] > l[v] + 1 && ev[h] > 0){
- l[es[h]] = l[v] + 1;
- q.push(es[h]);
- }
- }
- }
- }
- int dfs(int v, int f = INF) {
- if (v == t) {
- return f;
- }
- int res = 0;
- if (last[v] == -1) {
- last[v] = first[v];
- } else {
- last[v] = next[last[v]];
- }
- for (;last[v]; last[v] = next[last[v]]) {
- int h = last[v];
- if (ev[h] > 0 && l[es[h]] == l[v] + 1) {
- int ans = dfs(es[h], min(ev[h], f));
- ev[h] -= ans;
- ev[be(h)] += ans;
- res += ans;
- f -= ans;
- if (f == 0) break;
- }
- }
- return res;
- }
- int main(){
- cin >> n >> p >> q;
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= n; ++j) {
- cin >> tmp;
- if (tmp) {
- add(i, j, 1);
- add(j, i, 0);
- }
- }
- }
- s = n + 1;
- t = n + 2;
- while (p--) {
- cin >> tmp;
- add(s, tmp, INF);
- add(tmp, s, 0);
- }
- while (q--) {
- cin >> tmp;
- add(tmp, t, INF);
- add(t, tmp, 0);
- }
- int cur;
- n += 2;
- do {
- bfs();
- for (int i = 1; i <= n; i++) {
- last[i] = -1;
- }
- cur = dfs(s);
- ans += cur;
- } while (cur);
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement