#include<queue> #include<cstdio> #include<cstring> #include<iostream> using namespace std; #define maxn 1000 #define maxm 100000 #define inf 1000000000 #define Clear(a,c) memset(a,c,sizeof(a)) class Costflow { struct Edge { int u,v,f,cap,cost,n; }; int h[maxn],d[maxn],pre[maxn]; int minf,u,v,p,cnt; bool vis[maxn]; Edge e[maxm]; queue<int> Q; public: int S,T,flow,cost;; Costflow() { Clear(h,255); cnt=0; flow=0; cost=0; } void init() { Clear(h,255); cnt=0; flow=0; cost=0; } private: void add(int u,int v,int cap,int cost) { e[cnt].u=u; e[cnt].v=v; e[cnt].f=0; e[cnt].cap=cap; e[cnt].cost=cost; e[cnt].n=h[u]; h[u]=cnt; cnt++; } bool spfa() { Clear(vis,0); Clear(pre,255); Clear(d,127); Q.push(S); d[S]=0; vis[S]=1; while (!Q.empty()) { u=Q.front(); Q.pop(); vis[u]=0; for (p=h[u];p>=0;p=e[p].n) if (e[p].cap>e[p].f && d[e[p].v]>d[u]+e[p].cost) { v=e[p].v; d[v]=d[u]+e[p].cost; pre[v]=p; if (!vis[v]) Q.push(v),vis[v]=1; } } if (d[T]>inf) return 0; else return 1; } public: void addedge(int u,int v,int cap,int cost) { add(u,v,cap,cost); add(v,u,0,-cost); } void costflow() { while (spfa()) { minf=inf; for (p=pre[T];p>=0;p=pre[e[p].u]) minf=min(minf,e[p].cap-e[p].f); for (p=pre[T];p>=0;p=pre[e[p].u]) e[p].f+=minf,e[p^1].f-=minf; flow+=minf; cost+=minf*d[T]; } } };
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define Clear(a,c) memset(a,c,sizeof(a)) #define maxn 50000 #define maxm 1000000 #define inf 1000000000 class Maxflow { private: struct Edge { int v,f,n; }; int g[maxn],vg[maxn],h[maxn]; int aug,cnt,find; Edge e[maxm]; void auge(int u) { int mgap,taug,v,p; mgap=A-1; taug=aug; if (u==T) { flow+=aug; find=1; return; } for (p=h[u];p>=0;p=e[p].n) if (e[p].f) { v=e[p].v; if (g[u]==g[v]+1) { aug=min(aug,e[p].f); auge(v); if (g[S]>=A) return; if (find) break; aug=taug; } mgap=min(mgap,g[v]); } if (find) e[p].f-=aug,e[p^1].f+=aug; else { vg[g[u]]--; if (!vg[g[u]]) g[S]=A; g[u]=mgap+1; vg[g[u]]++; } } public: int S,T,A,flow; Maxflow() { Clear(h,255); Clear(g,0); Clear(vg,0); cnt=0; flow=0; } void init() { Clear(h,255); Clear(g,0); Clear(vg,0); cnt=0; flow=0; } void add(int u,int v,int c) { e[cnt].v=v; e[cnt].f=c; e[cnt].n=h[u]; h[u]=cnt++; e[cnt].v=u; e[cnt].f=0; e[cnt].n=h[v]; h[v]=cnt++; } void maxflow() { while (g[S]<A) { aug=inf; find=0; auge(S); } } };