void isap(int s,int t){ bfs(t); for(int i = 1;i<=node;++i) cur[i] = st[i]; int u = s; while(d[s] < node){ if(u == t) u = aug(t); bool adv = 0; FIND(p,u) if(d[u] == d[v[p]]+1 && flow[p] < w[p]) { cur[u] = p,pre[v[p]] = p,u = v[p],adv = 1; break; } if(!adv){ if(--num[d[u]] == 0) break; cur[u] = st[u]; int md = node; TRA(p,u) if(flow[p] < w[p]) md = min(md,d[v[p]]); d[u] = md+1; ++num[d[u]]; if(u != s) u = v[pre[u]^1]; } } }
int main() { freopen("t.in", "r", stdin); scanf("%d%d", &N, &K); for ( int i = 1; i <= N * K / 2; i ++ ) { int a, b; scanf("%d%d", &a, &b); addEdge(a, b, i); } for ( int i = 1; i <= N; i ++ ) if ( !vis[i] ) dfs(i); for ( int i = 0; i < top; i ++ ) link[stk[i]->inv->to][stk[i]->to] = stk[i]->id; int matchCnt = 0; for ( int i = 1; i <= N; i ++ ) { memset(vis, 0, sizeof(vis)); matchCnt += aug(i); } if ( matchCnt < N ) printf("NO\n"); else { printf("YES\n"); for ( int i = 1; i <= N; i ++ ) printf("%d\n", link[match[i]][i]); } }
bool sap(int cut) { memset(v,0,sizeof(v)); memset(list,0,sizeof(list)); memset(layer,0,sizeof(layer)); memset(path,0,sizeof(path)); memset(counter,0,sizeof(counter)); for(int i=1; i<=k; ++i) v[s][i]=m; for(int i=1; i<=k; ++i) for(int j=k+1; j<=k+c; ++j) if(f[i][j]<=cut) v[i][j]=1; for(int i=k+1; i<=k+c; ++i) v[i][t]=1; // for(int i=s;i<=t;++i) // for(int j=s;j<=t;++j) if(v[i][j]) printf("%d %d %d\n",i,j,v[i][j]); // printf("--------------------\n"); judge=0; counter[0]=t+1; while(layer[s]<t) { minf=oo; found=false; aug(s); } return judge==c; }
void isap(int s,int t){ bfs(t); int u = s; for(int i = 1;i<=node;++i) cur[i] = st[i]; while(d[s] < node){ if(u == t) u = aug(t); int adv = 0; FIND(x,u) if(BAL(x)>0 && d[lk[x].v]+1 == d[u]) { cur[u] = x;u = lk[x].v;pre[u] = x; adv = 1; break; } if(!adv){ GAP(u); int md = node; TRA(x,u) if(BAL(x)>0) md = min(md,d[lk[x].v]); d[u] = md+1;cur[u]=st[u];++num[d[u]]; if(u != s) u = lk[pre[u]^1].v; } }
void aug(int now){ if(now==T){ ans+=minf; found=1; return; } Edge *i; double tmp=minf; int minl=T-1; for(i=edge[now];i;i=i->next){ if(!zero(i->w)&&i->w>0){ if(layer[now]==layer[i->v]+1){ minf=min(minf,i->w); aug(i->v); if(layer[S]>=T) return; if(found) break; minf=tmp; } minl=min(minl,layer[i->v]); } } if(!found){ if(!(--counter[layer[now]])) layer[S]=T; ++counter[layer[now]=minl+1]; }else{ i->w-=minf; i->pair->w+=minf; } }
int main () { freopen ("Boxes.in", "r", stdin); freopen ("Boxes.out", "w", stdout); scanf ("%d", &T); while (T--) { memset (fst, 0, sizeof (fst)); tot = 1, cf = 0; scanf ("%d", &n); s = n + 1, t = s + 1; for (int i = 1, c; i <= n; ++i) { scanf ("%d", &c); if (i > 1) addedge (i, i - 1, oo, 1); else addedge (i, n, oo, 1); if (i < n) addedge (i, i + 1, oo, 1); else addedge (i, 1, oo, 1); addedge (s, i, c, 0); addedge (i, t, 1, 0); } while (spfa ()) aug (); printf ("%d\n", cf); } return 0; }
int sap() { memset(gap,0,sizeof(gap)); memset(dis,0,sizeof(dis)); gap[0]=n; int ans=0; for(int i=1;i<=n;i++) cur[i]=head[i]; while (dis[s]<n) ans+=aug(s,INF); return ans; }
bool aug(int x) { for ( int y = 1; y <= N; y ++ ) if ( link[x][y] && !vis[y] ) { vis[y] = true; if ( !match[y] || aug(match[y]) ) { match[y] = x; return true; } } return false; }
int dinic(int sVtx, int tVtx) { int maxflow = 0; while(preLable(sVtx, tVtx)) { memcpy(cur, begin, sizeof(edge_t *) * nVtx); while(int tmp = aug(sVtx, tVtx, INFINITY)) maxflow += tmp; } return maxflow; }
int aug(int u,int f) { if(u==t) { ans+=flow*f; return f; } vis[u]=1; int tmp=f; for(int i=head[u];i!=-1;i=e[i].next) if(e[i].w && ! e[i].c && ! vis[e[i].v]) { int delta=aug(e[i].v,tmp<e[i].w?tmp:e[i].w); e[i].w-=delta; e[i^1].w+=delta; tmp-=delta; if(! tmp)return f; } return f-tmp; }
void sap(){ memset(layer,0,sizeof(layer)); memset(counter,0,sizeof(counter)); counter[0]=T+1; ans=0; while(layer[S]<T){ found=0; minf=oo; aug(S); } }
int mincost_maxflow() { piS = cost = 0; while(modlabel()) { do { CLR(v,0); }while(aug(S, INF)); } return -cost; }
void SAP() { memset(h,0,sizeof(h)); memset(vh,0,sizeof(vh)); vh[0]=n; while(h[0]<n) { found=0; augc=INF; aug(0); } }
void SAP() { memset(h,0,sizeof(h)); memset(vh,0,sizeof(vh)); vh[0]=2*N; //从0开始计数... ans=0; while(h[S]<2*N) { found=0; augc=INF; aug(S); } }
void SAP() { memset(h,0,sizeof(h)); memset(vh,0,sizeof(vh)); vh[0]=size; //从0开始计数... ans=0; while(h[S]<size) { found=0; augc=INF; aug(S); } printf("%d\n",ans); }
int aug(int no, int m) { if (no == T) return cost += piS * m, m; v[no] = true; int l = m; for (Edge *i = e[no]; i; i = i->next) if (i->c && !i->u && !v[i->t]) { int d = aug(i->t, l < i->c ? l : i->c); i->c -= d, i->pair->c += d, l -= d; if (!l) return m; } return m - l; }
void aug(int x) { int minh=n,y,tmp; int augco=augc; if(x==n-1) { found=1; ans+=augc; /// return ; } //T[x]=first[x]; for(T[x] ;T[x]!=-1;T[x]=e[T[x]].next) ////当前弧优化..... { tmp=T[x]; if(e[tmp].c>e[tmp].f) { y=e[tmp].y; if(h[y]+1==h[x]) { if(e[tmp].c-e[tmp].f<augc) augc=e[tmp].c-e[tmp].f; aug(y); if(h[0]>=n) return ; if(found) break; augc=augco; } if(h[y]<minh) minh=h[y]; } } T[x]=first[x]; if(!found) { vh[h[x]]--; if(vh[h[x]]==0) h[0]=n; h[x]=minh+1; vh[h[x]]++; } else { e[tmp].f+=augc; e[tmp^1].f-=augc; } }
void SAP() { memset(h,0,sizeof(h)); memset(vh,0,sizeof(vh)); vh[0]=n; ans=0; while(h[0]<n) { found=0; augc=INF; aug(0); } if(ans==total) q[++top]=T-1; }
int aug(int x, int limit){ if(x == T) return limit; int kk, ll = limit; for(; FV[x]; FV[x] = FV[x] -> nxt) if(FV[x] -> u && dist[FV[x]->t] + 1 == dist[x]){ kk = aug(FV[x] -> t, std::min(ll, FV[x] -> u)); FV[x] -> u -= kk, FV[x] -> op -> u += kk; ll -= kk; if(!ll || gapFlag) return limit - ll; } if(--cntDist[dist[x]] == 0){ gapFlag = true; } dist[x] = T + 1; return limit - ll; }
void sap() { memset(list,0,sizeof(list)); memset(layer,0,sizeof(layer)); memset(path,0,sizeof(path)); memset(counter,0,sizeof(counter)); counter[0]=t+1; ans=0; while(layer[s]<=t) { minf=oo; found=false; aug(s); } }
int test() { memset(h,0,sizeof(h)); memset(vh,0,sizeof(vh)); vh[0]=N+1;ans=0; while(h[0]<N+1) { found=0; augc=INF; aug(0); } if(ans>=total) return 1; return 0; }
int SAP() { memset(h,0,sizeof(h)); memset(vh,0,sizeof(vh)); vh[0]=N; ans=0; while(h[1]<N) { found=0; augc=INF; aug(1); } if(ans>=total) return 1; return 0; }
int test() { memset(h,0,sizeof(h)); memset(vh,0,sizeof(vh)); vh[0]=2*N+2; ans=0; while(h[2*N+1]<2*N+2) { found=0; augc=10000000; aug(2*N+1); } if(ans>=total) return 1; return 0; }
void SAP() { memset(h,0,sizeof(h)); memset(vh,0,sizeof(vh)); vh[0]=N; ans=0; while(h[0]<N) { found=0; augc=INF; aug(0); } if(ans==total) printf("possible\n"); else printf("impossible\n"); }
int aug(int u,int m) { if(u==t) return m; int flag=0,l=m,d; for(int i=cur[u];i!=-1;i=e[i].next) if(dis[e[i].v]+1==dis[u] && e[i].w>0) { d=aug(e[i].v,min(e[i].w,l)); e[i].w-=d; e[i^1].w+=d; l-=d; if(! flag){ flag=1; cur[u]=i; } if(dis[s]>=n || l==0) return m-l; } if(! flag) { int mindis=n; for(int i=head[u];i!=-1;i=e[i].next) if(e[i].w>0 && dis[e[i].v]<mindis){ mindis=dis[e[i].v]; cur[u]=i; } if(--gap[dis[u]]==0) dis[s]=n; else ++gap[dis[u]=mindis+1]; } return m-l; }
void SAP() { int i; memset(h,0,sizeof(h)); memset(vh,0,sizeof(vh)); vh[0]=n; ans=0; for(i=0;i<n;i++) T[i]=first[i]; while(h[0]<n) { found=0; augc=INF; aug(0); } printf("%d\n",ans); }
void aug(int x) { int minh=n,y,tmp; __int64 augco=augc; if(x==n-1) { found=1; total-=augc; /// return ; } tmp=first[x]; for( ;tmp!=-1;tmp=e[tmp].next) { if(e[tmp].c>e[tmp].f) { y=e[tmp].y; if(h[y]+1==h[x]) { if(e[tmp].c-e[tmp].f<augc) augc=e[tmp].c-e[tmp].f; aug(y); if(h[0]>=n) return ; if(found) break; augc=augco; } if(h[y]<minh) minh=h[y]; } } if(!found) { vh[h[x]]--; if(vh[h[x]]==0) h[0]=n; h[x]=minh+1; vh[h[x]]++; } else { e[tmp].f+=augc; e[tmp^1].f-=augc; } }
int main() { freopen("test.in", "r", stdin); int n, m; char str[1001]; while(scanf("%d%d", &n, &m) != EOF) { if (!n && !m) { break; } cntmen = cnthouse = 0; memset(edge, 0, sizeof(edge)); memset(mempool, 0, sizeof(mempool)); memnum = 0; for (int i = 0; i < n; ++i) { scanf("%s", str); for (int j = 0; j < m; ++j) { if (str[j] == 'm') { men[++cntmen].x = i; men[cntmen].y = j; } else if (str[j] == 'H') { house[++cnthouse].x = i; house[cnthouse].y = j; } } } S = 0, T = cntmen + cnthouse + 1; for (int i = 1; i <= cntmen; ++i) { for (int j = 1; j <= cnthouse; ++j) { add(i, j + cntmen, mandis(men[i], house[j]), 1); } } for (int i = 1; i <= cntmen; ++i) { add(S, i, 0, 1); } for (int j = 1; j <= cnthouse; ++j) { add(j + cntmen, T, 0, 1); } cost = 0; while(spfa()) { aug(); } printf("%d\n",cost); } return 0; }
bool aug(int x, int y) { for(int o = 0; o < 4; o ++) { int x2 = x + dx[o], y2 = y + dy[o]; if(0 <= x2 && x2 < r) if(0 <= y2 && y2 < c) if(fine[x2][y2] && vis[x2][y2] != ctime) { vis[x2][y2] = ctime; if(match[x2][y2] == -1 || aug(match[x2][y2] / c, match[x2][y2] % c)) { match[x2][y2] = x * c + y; return true; } } } return false; }
void aug(int x) { int i,minh=size,augco=augc; if(x==T) { found=1; ans+=augc; return ; } for(i=0;i<size;++i) if(c[x][i]>0) { if(h[i]+1==h[x]) { if(c[x][i]<augc) augc=c[x][i]; aug(i); if(h[S]>=size) return ; if(found) break; augco=augc; } if(h[i]<minh) minh=h[i]; } if(!found) { vh[h[x]]--; if(vh[h[x]]==0) h[S]=size; h[x]=minh+1; vh[h[x]]++; } else { c[x][i]-=augc; c[i][x]+=augc; } }