1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include<bits/stdc++.h>
#define INF 2147483647 #define ll long long #define get(i, j) ((i - 1) * n + j)
int inp(){ char c = getchar(); int neg = 1; while(c < '0' || c > '9'){ if(c == '-') neg = -1; c = getchar(); } int sum = 0; while(c >= '0' && c <= '9'){ sum = ((sum << 3) + (sum << 1)) + c - '0'; c = getchar(); } return neg * sum; }
const int opt[10][4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int head[200010]; int nxt[500010]; int end[500010]; int value[500010]; int cost[500010]; int dis[200010]; int prev1[200010]; int prev2[200010]; bool inq[200010]; int q[1000010]; bool dag[100][100]; int w[100][100]; int Cou = -1;
void link(int a, int b, int v, int c){ nxt[++Cou] = head[a]; head[a] = Cou; end[Cou] = b; value[Cou] = v; cost[Cou] = c;
nxt[++Cou] = head[b]; head[b] = Cou; end[Cou] = a; value[Cou] = 0; cost[Cou] = -c; }
int main(){ memset(head, -1, sizeof(head)); int n = inp(); int m = inp(); int k = inp(); int sum = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){ w[i][j] = inp(); sum += w[i][j]; } for(int i = 1; i <= k; i++){ int x = inp(); int y = inp(); dag[x][y] = true; } int s = 0; link(s, 200001, m, 0); int e = 200000; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){ if((i + j) & 1){ link(get(i, j), get(i, j) + (n * n), 1, -w[i][j]); if(i & 1){ if(j > 1) link(get(i, j) + (n * n), get(i, j - 1), 1, 0); if(j < n) link(get(i, j) + (n * n), get(i, j + 1), 1, 0); } else { if(i > 1) link(get(i, j) + (n * n), get(i - 1, j), 1, 0); if(i < n) link(get(i, j) + (n * n), get(i + 1, j), 1, 0); } } else { if(dag[i][j]) continue; link(get(i, j), get(i, j) + (n * n), 1, 0); if(i & 1){ link(get(i, j) + (n * n), e, 1, 0); } else { link(200001, get(i, j), 1, 0); for(int u = 0; u < 4; u++){ int tx = i + opt[u][0]; int ty = j + opt[u][1]; if(tx < 1 || tx > n || ty < 1 || ty > n) continue; link(get(i, j) + (n * n), get(tx, ty), 1, 0); } } } }
int flow = 0; int ans = 0; int f = INF; int ret = 0; while(f > 0){ memset(inq, false, sizeof(inq)); memset(dis, 0x3f, sizeof(dis)); dis[s] = 0; q[1] = s; int qf = 1; int qe = 1; int sum = 0; while(qf <= qe){ int u = q[qf++]; inq[u] = false; sum -= dis[u]; for(int x = head[u]; x != -1; x = nxt[x]){ if(value[x] > 0 && dis[end[x]] > dis[u] + cost[x]){ if(inq[end[x]]) sum -= dis[end[x]]; dis[end[x]] = dis[u] + cost[x]; prev1[end[x]] = u; prev2[end[x]] = x; sum += dis[end[x]]; if(!inq[end[x]]){ q[++qe] = end[x]; inq[end[x]] = true; } } } }
if(dis[e] == dis[200002]) break; int delta = f; for(int i = e; i != s; i = prev1[i]){ delta = std::min(delta, value[prev2[i]]); } f -= delta; flow += delta; ans += delta * dis[e]; for(int i = e; i != s; i = prev1[i]){ value[prev2[i]] -= delta; value[prev2[i] ^ 1] += delta; } ret = std::min(ret, ans); } printf("%d", sum + ret); }
|