题面
https://www.luogu.org/problem/P1402
题解
// luogu-judger-enable-o2 #include<cstdio> #include<cstring> #include<iostream> #include<vector> #include<queue> #define ri register int #define N 500 #define INF 1000000007 #define M 50500 using namespace std; int n,p,q; struct Dinic { vector<int> ed[N]; int w[M],to[M],d[N],cur[N]; int cnt; void init() {cnt=-1;} void add_edge(int a,int b,int c) { ed[a].push_back(++cnt); w[cnt]=c; to[cnt]=b; ed[b].push_back(++cnt); w[cnt]=0; to[cnt]=a; } bool bfs() { memset(d,0x3f,sizeof(d)); queue<int> qu; d[0]=0; qu.push(0); while (!qu.empty()) { int x=qu.front(); qu.pop(); //cout<<x<<endl; for (ri i=0;i<ed[x].size();i++) { int e=ed[x][i]; if (!w[e]) continue; if (d[x]+1<d[to[e]]) { d[to[e]]=d[x]+1; qu.push(to[e]); } } } return d[p+2*n+q+1]<=N*10; } int dfs(int x,int limit) { if (!limit || x==p+2*n+q+1) return limit; int tot=0; for (ri &i=cur[x];i<ed[x].size();i++) { int e=ed[x][i]; if (w[e] && d[to[e]]==d[x]+1) { int f=dfs(to[e],min(limit,w[e])); if (!f) continue; w[e]-=f; w[1^e]+=f; tot+=f; limit-=f; if (!limit) return tot; } } return tot; } int solve() { int ret=0; while (bfs()) { memset(cur,0,sizeof(cur)); ret+=dfs(0,INF); } return ret; } } zyx; int main() { int w; cin>>n>>p>>q; zyx.init(); for (ri i=1;i<=n;i++) for (ri j=1;j<=p;j++) { cin>>w; if (w) zyx.add_edge(j,p+i,1); } for (ri i=1;i<=n;i++) for (ri j=1;j<=q;j++) { cin>>w; if (w) zyx.add_edge(p+i+n,p+2*n+j,1); } for (ri i=1;i<=p;i++) zyx.add_edge(0,i,1); for (ri i=1;i<=n;i++) zyx.add_edge(p+i,p+i+n,1); for (ri i=p+2*n+1;i<=p+2*n+q;i++) zyx.add_edge(i,p+2*n+q+1,1); cout<<zyx.solve()<<endl; return 0; }
原文:https://www.cnblogs.com/shxnb666/p/11278589.html