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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| #include <cstdio> #include <cstring> #define maxN 1010 #define maxM 300010 #define inf 2147483647 struct edge{ int x, y, c, g; } b[maxM << 1]; int len = 1, S = 0, T = 0; int dep[maxM << 1], f[maxM << 1]; int A[maxN], B[maxN], X[maxM], Y[maxM], h[maxM << 1]; int min (int x, int y) { return x < y ? x : y; } void ins (int x, int y, int c) { len++; b[len].x = x; b[len].y = y; b[len].c = c; b[len].g = h[x]; h[x] = len; len++; b[len].x = y; b[len].y = x; b[len].c = 0; b[len].g = h[y]; h[y] = len; } void px (int l, int r) { int x = l, y = r, mid1 = X[(l + r) >> 1], mid2 = A[Y[(l + r) >> 1]]; while(x <= y) { while(X[x] < mid1 || (X[x] == mid1 && A[Y[x]] < mid2)) { x++; } while(X[y] > mid1 || (X[y] == mid1 && A[Y[y]] > mid2)) { y--; } if(x <= y) { int t = X[x]; X[x] = X[y]; X[y] = t; t = Y[x]; Y[x] = Y[y]; Y[y] = t; x++; y--; } } if(l < y) { px(l, y); } if(x < r) { px(x, r); } } bool bfs () { const int M = (maxM << 1); int tou = 1, wei = 2; f[1] = S; dep[S] = 1; while(tou != wei) { int x = f[tou]; for(register int i = h[x];i;i = b[i].g) { int y = b[i].y; if(b[i].c && !dep[y]) { dep[y] = dep[x] + 1; f[wei] = y; wei++; if(wei >= M) { wei = 1; } if(y == T) { return true; } } } tou++; if(tou >= M) { tou = 1; } } return false; } int dfs (int x, int flow) { if(x == T) { return flow; } int now = flow; for(register int i = h[x];i;i = b[i].g) { int y = b[i].y; if(b[i].c && dep[y] == dep[x] + 1) { int ma = min(now, b[i].c); now -= ma; b[i].c -= ma; b[i ^ 1].c += ma; int rest = ma - dfs(y, ma); now += rest; b[i].c += rest; b[i ^ 1].c -= rest; } } return flow - now; } int main () { int n = 0, m = 0; scanf("%d %d", &n, &m); T = n + m + 1; for(int i = 1;i <= n; i++) { scanf("%d", &A[i]); } for(int i = 1;i <= n; i++) { scanf("%d", &B[i]); } for(int i = 1;i <= m; i++) { scanf("%d %d", &X[i], &Y[i]); } px(1, m); for(int i = 1;i <= n; i++) { ins(S, i, B[i]); } int now = 1, cnt = n; for(int i = 1;i <= n; i++) { while(X[now] < i && now <= m) { now++; } if(now > m) { continue; } int last = 0; int Yz = 0, Az = 0; while(X[now] == i && now <= m) { cnt++; if(last) { ins(last, cnt, Az); } last = cnt; Yz = Y[now]; Az = A[Y[now]]; ins(Yz, cnt, inf); now++; } if(last) { ins(cnt, T, Az); } } int Ans = 0; while(bfs()) { Ans += dfs(S, inf); memset(dep, 0, sizeof(dep)); } printf("%d", Ans); return 0; }
|