No Smoking, Please Gym - 101518H
No Smoking, Please
最小割~
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxv = 1000110; 4 const int maxe = 4000010; 5 const int inf = 0x3f3f3f3f; 6 struct Edge{ 7 int u, v, nex; 8 int cap, flow; 9 Edge(int u=0, int v=0, int nex=0, int cap=0, int flow=0): 10 u(u), v(v), nex(nex), cap(cap), flow(flow){} 11 }e[maxe<<1]; 12 int head[maxv]; 13 int cnt; 14 void init(){ 15 memset(head, -1, sizeof(head)); 16 cnt = 0; 17 } 18 19 void add(int u, int v, int cap){ 20 e[cnt] = Edge(u, v, head[u], cap, 0); 21 head[u] = cnt++; 22 e[cnt] = Edge(v, u, head[v], cap, 0); 23 head[v] = cnt++; 24 } 25 26 int d[maxv], num[maxv], cur[maxv], p[maxv]; 27 int vis[maxv]; 28 int S, T; 29 int N; 30 31 void bfs(){ 32 memset(d, -1, sizeof(d)); 33 memset(vis, 0, sizeof(vis)); 34 queue<int> q; 35 q.push(T); 36 vis[T] = 1; 37 d[T] = 0; 38 while(!q.empty()){ 39 int u = q.front(); 40 q.pop(); 41 for(int i = head[u]; ~i; i = e[i].nex){ 42 // int id = i&(-2); //正边 //这里错了!!! 43 int id = i; 44 int v = e[id].v; 45 if(!vis[v] && e[id].cap > e[id].flow){ 46 vis[v] = 1; 47 d[v] = d[u] + 1; 48 q.push(v); 49 } 50 } 51 } 52 } 53 54 int augment(){ 55 int u = T; 56 int a = inf; 57 while(u!=S){ 58 int id = p[u]; 59 a = min(a, e[id].cap - e[id].flow); 60 u = e[id].u; 61 } 62 u = T; 63 while(u!=S){ 64 int id = p[u]; 65 e[id].flow += a; 66 e[id^1].flow -= a; 67 u = e[id].u; 68 } 69 return a; 70 } 71 72 int ISAP() { 73 bfs(); 74 int flow = 0; 75 memset(num, 0, sizeof(num)); 76 for(int i = 0; i < N; i++) { 77 cur[i] = head[i]; 78 if(~d[i]) num[d[i]]++; 79 } 80 int u = S; 81 while(d[S] < N) { 82 if(u == T) { 83 flow += augment(); 84 u = S; 85 } 86 int ok = 0; 87 for(int i = cur[u]; ~i; i = e[i].nex){ 88 int v = e[i].v; 89 if(d[u] == d[v]+1 && e[i].cap > e[i].flow){ 90 p[v] = i; 91 ok = 1; 92 cur[u] = i; 93 u = v; 94 break; 95 } 96 } 97 if(!ok){ 98 int m = N-1; 99 for(int i = head[u]; ~i; i = e[i].nex){ 100 if(e[i].cap > e[i].flow && ~d[e[i].v]) m = min(m, d[e[i].v]); 101 } 102 if(--num[d[u]] == 0) break; 103 num[d[u]=m+1]++; 104 cur[u] = head[u]; 105 if(u != S) u = e[p[u]].u; 106 } 107 } 108 return flow; 109 } 110 void pre(){ 111 freopen("in.txt", "w", stdout); 112 int t = 1000; 113 cout<<t<<endl; 114 while(t--){ 115 srand(rand()); 116 int n = rand()%50+2; 117 int m = rand()%50+2; 118 cout<<n<<" "<<m<<endl; 119 int sx = rand()%50+2, sy = rand()%50+2; 120 int ex = rand()%50+2, ey = rand()%50+2; 121 while(sx == ex && sy == ey) { 122 ex = rand()%50+2; 123 ey = rand()%50+2; 124 } 125 printf("%d %d %d %d\n", sx, sy, ex, ey); 126 for(int i = 0; i < n; i++){ 127 for(int j = 0; j < m-1; j++){ 128 cout<<rand()%110<<" "; 129 } 130 cout<<endl; 131 } 132 for(int i = 0; i < n-1; i++){ 133 for(int j = 0; j < m; j++){ 134 cout<<rand()%110<<" "; 135 } 136 cout<<endl; 137 } 138 } 139 } 140 int main(){ 141 int t; 142 // pre(); 143 // freopen("in.txt", "r", stdin); 144 // freopen("out.txt", "w", stdout); 145 scanf("%d", &t); 146 while(t--){ 147 init(); 148 int n, m; 149 scanf("%d %d", &n, &m); 150 int a, b; 151 scanf("%d %d", &a, &b); 152 S = a*m + b; 153 scanf("%d %d", &a, &b); 154 T = a*m + b; 155 N = n*m; 156 for(int i = 0; i < n; i++){ 157 for(int j = 0; j < m-1; j++){ 158 scanf("%d", &a); 159 if(!a) continue; 160 int id = i*m+j; 161 add(id, id+1, a+1); 162 } 163 } 164 for(int i = 0; i < n-1; i++){ 165 for(int j = 0; j < m; j++){ 166 scanf("%d", &a); 167 if(!a) continue; 168 int id = i*m+j; 169 add(id, id+m, a+1); 170 } 171 } 172 int ans = ISAP(); 173 printf("%d\n", ans*1000); 174 } 175 return 0; 176 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define CLR(m,a) memset(m,a,sizeof(m)) 4 const int inf=0x3f3f3f3f; 5 const int maxv=1000010; 6 const int maxe=4000010; 7 int N; 8 struct Edge 9 { 10 int u,v,cap,flow; 11 int nex; 12 }e[maxe<<1]; 13 int head[maxv]; 14 int cnt=0; 15 void init() 16 { 17 CLR(head,-1); 18 cnt=0; 19 } 20 void add(int u,int v,int cap) 21 { 22 e[cnt].u=u; 23 e[cnt].v=v; 24 e[cnt].cap=cap; 25 e[cnt].flow=0; 26 e[cnt].nex=head[u]; 27 head[u]=cnt++; 28 } 29 30 int vis[maxv],d[maxv],cur[maxv]; 31 int s,t; //这里设成了全局变量,用此模板前需要初始化源和汇 32 33 int bfs() 34 { 35 CLR(vis,0); 36 queue<int> q; 37 q.push(s); 38 d[s]=0; 39 vis[s]=1; 40 while(!q.empty()) 41 { 42 int u=q.front(); 43 q.pop(); 44 for(int i=head[u];~i;i=e[i].nex) 45 { 46 int v=e[i].v; 47 if(!vis[v]&&e[i].cap>e[i].flow) 48 { 49 vis[v]=1; 50 d[v]=d[u]+1; 51 q.push(v); 52 } 53 } 54 } 55 return vis[t]; 56 } 57 58 int dfs(int x,int a) 59 { 60 if(x==t||a==0) return a; 61 int flow=0,f; 62 for(int &i=cur[x];~i;i=e[i].nex) 63 { 64 int v=e[i].v; 65 if(d[x]+1==d[v]&&(f=dfs(v,min(a,e[i].cap-e[i].flow)))>0) 66 { 67 e[i].flow+=f; 68 e[i^1].flow-=f; 69 flow+=f; 70 a-=f; 71 if(a==0) break; 72 } 73 } 74 return flow; 75 } 76 77 int Dinic() 78 { 79 int flow=0; 80 while(bfs()) 81 { 82 for(int i=0;i<N;i++) cur[i]=head[i]; 83 flow+=dfs(s,inf); 84 } 85 return flow; 86 } 87 int main(){ 88 int T; 89 // freopen("in.txt", "r", stdin); 90 scanf("%d", &T); 91 while(T--){ 92 init(); 93 int n, m; 94 scanf("%d %d", &n, &m); 95 int a, b; 96 scanf("%d %d", &a, &b); 97 s = a*m + b; 98 scanf("%d %d", &a, &b); 99 t = a*m + b; 100 N = n*m; 101 for(int i = 0; i < n; i++){ 102 for(int j = 0; j < m-1; j++){ 103 scanf("%d", &a); 104 if(!a) continue; 105 int id = i*m+j; 106 add(id, id+1, a+1); 107 add(id+1, id, a+1); 108 } 109 } 110 for(int i = 0; i < n-1; i++){ 111 for(int j = 0; j < m; j++){ 112 scanf("%d", &a); 113 if(!a) continue; 114 int id = i*m+j; 115 add(id, id+m, a+1); 116 add(id+m, id, a+1); 117 } 118 } 119 int ans = Dinic(); 120 printf("%d\n", ans*1000); 121 } 122 return 0; 123 }
终于过了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int maxv = 1000110; 5 const int maxe = 4000010; 6 const int inf = 0x3f3f3f3f; 7 struct Edge{ 8 int u, v, nex; 9 int cap, flow; 10 Edge(int u=0, int v=0, int nex=0, int cap=0, int flow=0): 11 u(u), v(v), nex(nex), cap(cap), flow(flow){} 12 }e[maxe<<1]; 13 int head[maxv]; 14 int cnt; 15 void init(){ 16 memset(head, -1, sizeof(head)); 17 cnt = 0; 18 } 19 20 void add(int u, int v, int cap){ 21 e[cnt] = Edge(u, v, head[u], cap, 0); 22 head[u] = cnt++; 23 e[cnt] = Edge(v, u, head[v], cap, 0); 24 head[v] = cnt++; 25 } 26 27 int d[maxv], num[maxv], cur[maxv], p[maxv]; 28 int vis[maxv]; 29 int S, T; 30 int N; 31 32 void bfs(){ 33 memset(d, -1, sizeof(d)); 34 memset(vis, 0, sizeof(vis)); 35 queue<int> q; 36 q.push(T); 37 vis[T] = 1; 38 d[T] = 0; 39 while(!q.empty()){ 40 int u = q.front(); 41 q.pop(); 42 for(int i = head[u]; ~i; i = e[i].nex){ 43 // int id = i&(-2); //正边 //这里错了!!! 44 int id = i; 45 int v = e[id].v; 46 if(!vis[v] && e[id].cap > e[id].flow){ 47 vis[v] = 1; 48 d[v] = d[u] + 1; 49 q.push(v); 50 } 51 } 52 } 53 } 54 55 int augment(){ 56 int u = T; 57 int a = inf; 58 while(u!=S){ 59 int id = p[u]; 60 a = min(a, e[id].cap - e[id].flow); 61 u = e[id].u; 62 } 63 u = T; 64 while(u!=S){ 65 int id = p[u]; 66 e[id].flow += a; 67 e[id^1].flow -= a; 68 u = e[id].u; 69 } 70 return a; 71 } 72 73 int ISAP() { 74 bfs(); 75 int flow = 0; 76 memset(num, 0, sizeof(num)); 77 for(int i = 0; i < N; i++) { 78 cur[i] = head[i]; 79 if(~d[i]) num[d[i]]++; 80 } 81 int u = S; 82 while(d[S] < N) { 83 if(u == T) { 84 flow += augment(); 85 u = S; 86 } 87 int ok = 0; 88 for(int i = cur[u]; ~i; i = e[i].nex){ 89 int v = e[i].v; 90 if(d[u] == d[v]+1 && e[i].cap > e[i].flow){ 91 p[v] = i; 92 ok = 1; 93 cur[u] = i; 94 u = v; 95 break; 96 } 97 } 98 if(!ok){ 99 int m = N-1; 100 for(int i = head[u]; ~i; i = e[i].nex){ 101 if(e[i].cap > e[i].flow && ~d[e[i].v]) m = min(m, d[e[i].v]); 102 } 103 if(--num[d[u]] == 0) break; 104 num[d[u]=m+1]++; 105 cur[u] = head[u]; 106 if(u != S) u = e[p[u]].u; 107 } 108 } 109 return flow; 110 } 111 void pre(){ 112 freopen("in.txt", "w", stdout); 113 int t = 1000; 114 cout<<t<<endl; 115 while(t--){ 116 srand(rand()); 117 int n = rand()%50+1; 118 int m = rand()%50+1; 119 cout<<n<<" "<<m<<endl; 120 int sx = rand()%50+1, sy = rand()%50+1; 121 int ex = rand()%50+1, ey = rand()%50+1; 122 while(sx == ex && sy == ey) { 123 ex = rand()%50+1; 124 ey = rand()%50+1; 125 } 126 printf("%d %d %d %d\n", sx, sy, ex, ey); 127 for(int i = 0; i < n; i++){ 128 for(int j = 0; j < m-1; j++){ 129 cout<<rand()%110<<" "; 130 } 131 cout<<endl; 132 } 133 for(int i = 0; i < n-1; i++){ 134 for(int j = 0; j < m; j++){ 135 cout<<rand()%110<<" "; 136 } 137 cout<<endl; 138 } 139 } 140 } 141 int main(){ 142 int t; 143 pre(); 144 freopen("in.txt", "r", stdin); 145 freopen("out.txt", "w", stdout); 146 scanf("%d", &t); 147 while(t--){ 148 init(); 149 int n, m; 150 scanf("%d %d", &n, &m); 151 int a, b; 152 scanf("%d %d", &a, &b); 153 S = a*m + b; 154 scanf("%d %d", &a, &b); 155 T = a*m + b; 156 N = n*m; 157 for(int i = 0; i < n; i++){ 158 for(int j = 0; j < m-1; j++){ 159 scanf("%d", &a); 160 if(!a) continue; 161 int id = i*m+j; 162 add(id, id+1, a+1); 163 } 164 } 165 for(int i = 0; i < n-1; i++){ 166 for(int j = 0; j < m; j++){ 167 scanf("%d", &a); 168 if(!a) continue; 169 int id = i*m+j; 170 add(id, id+m, a+1); 171 } 172 } 173 int ans = ISAP(); 174 if(ans) printf("%d\n", ans*1000); 175 } 176 return 0; 177 } 178 */ 179 #include <bits/stdc++.h> 180 using namespace std; 181 #define CLR(m,a) memset(m,a,sizeof(m)) 182 const int inf=0x3f3f3f3f; 183 const int maxv=1000010; 184 const int maxe=4000010; 185 int N; 186 struct Edge 187 { 188 int u,v,cap,flow; 189 int nex; 190 }e[maxe<<1]; 191 int head[maxv]; 192 int cnt=0; 193 void init() 194 { 195 CLR(head,-1); 196 cnt=0; 197 } 198 void add(int u,int v,int cap) 199 { 200 e[cnt].u=u; 201 e[cnt].v=v; 202 e[cnt].cap=cap; 203 e[cnt].flow=0; 204 e[cnt].nex=head[u]; 205 head[u]=cnt++; 206 } 207 208 int vis[maxv],d[maxv],cur[maxv]; 209 int s,t; //这里设成了全局变量,用此模板前需要初始化源和汇 210 211 int bfs() 212 { 213 CLR(vis,0); 214 queue<int> q; 215 q.push(s); 216 d[s]=0; 217 vis[s]=1; 218 while(!q.empty()) 219 { 220 int u=q.front(); 221 q.pop(); 222 for(int i=head[u];~i;i=e[i].nex) 223 { 224 int v=e[i].v; 225 if(!vis[v]&&e[i].cap>e[i].flow) 226 { 227 vis[v]=1; 228 d[v]=d[u]+1; 229 q.push(v); 230 } 231 } 232 } 233 return vis[t]; 234 } 235 236 int dfs(int x,int a) 237 { 238 if(x==t||a==0) return a; 239 int flow=0,f; 240 for(int &i=cur[x];~i;i=e[i].nex) 241 { 242 int v=e[i].v; 243 if(d[x]+1==d[v]&&(f=dfs(v,min(a,e[i].cap-e[i].flow)))>0) 244 { 245 e[i].flow+=f; 246 e[i^1].flow-=f; 247 flow+=f; 248 a-=f; 249 if(a==0) break; 250 } 251 } 252 return flow; 253 } 254 255 int Dinic() 256 { 257 int flow=0; 258 while(bfs()) 259 { 260 for(int i=0;i<N;i++) cur[i]=head[i]; 261 flow+=dfs(s,inf); 262 } 263 return flow; 264 } 265 int main(){ 266 int T; 267 freopen("in.txt", "r", stdin); 268 freopen("out1.txt", "w", stdout); 269 scanf("%d", &T); 270 while(T--){ 271 init(); 272 int n, m; 273 scanf("%d %d", &n, &m); 274 int a, b; 275 scanf("%d %d", &a, &b); 276 s = a*m + b; 277 scanf("%d %d", &a, &b); 278 t = a*m + b; 279 N = n*m; 280 for(int i = 0; i < n; i++){ 281 for(int j = 0; j < m-1; j++){ 282 scanf("%d", &a); 283 if(!a) continue; 284 int id = i*m+j; 285 add(id, id+1, a+1); 286 add(id+1, id, a+1); 287 } 288 } 289 for(int i = 0; i < n-1; i++){ 290 for(int j = 0; j < m; j++){ 291 scanf("%d", &a); 292 if(!a) continue; 293 int id = i*m+j; 294 add(id, id+m, a+1); 295 add(id+m, id, a+1); 296 } 297 } 298 int ans = Dinic(); 299 if(ans)printf("%d\n", ans*1000); 300 } 301 return 0; 302 }