【USACO】Dining
【题目链接】
【JZXX】点击打开链接
【caioj】点击打开链接
【算法】
拆点+网络流
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 1000 int i,tot,N,F,D,sf,sd,ans,x; int U[MAXN*5],V[MAXN*5],W[MAXN*5],Head[MAXN*5], Next[MAXN*5],other[MAXN*5],h[MAXN*5]; template <typename T> void read(T &x) { int f=1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c=='-') f=-1; } for (; isdigit(c); c = getchar()) x=x*10+c-'0'; x*=f; } inline void add(int a,int b,int c) { ++tot; U[tot] = a; V[tot] = b; W[tot] = c; Next[tot] = Head[a]; Head[a] = tot; other[tot] = tot + 1; ++tot; U[tot] = b; V[tot] = a; W[tot] = 0; Next[tot] = Head[b]; Head[b] = tot; other[tot] = tot - 1; } inline bool BFS() { int i,x,y; queue<int> q; memset(h,0,sizeof(h)); h[1] = 1; q.push(1); while (!q.empty()) { x = q.front(); q.pop(); for (i = Head[x]; i; i = Next[i]) { y = V[i]; if ((W[i] > 0) && (!h[y])) { h[y] = h[x] + 1; q.push(y); } } } if (h[F+2*N+D+2]) return true; else return false; } int maxflow(int x,int f) { int i,y,s=0,t; if (x == F + 2 * N + D + 2) return f; for (i = Head[x]; i; i = Next[i]) { y = V[i]; if ((W[i] > 0) && (h[y] == h[x] + 1) && (s < f)) { s += (t = maxflow(y,min(W[i],f-s))); W[i] -= t; W[other[i]] += t; } } if (!s) h[x] = 0; return s; } int main() { read(N); read(F); read(D); for (i = 1; i <= F; i++) add(1,1+i,1); for (i = 1; i <= D; i++) add(1+F+2*N+i,F+2*N+D+2,1); for (i = 1; i <= N; i++) add(1+F+i,1+F+N+i,1); for (i = 1; i <= N; i++) { read(sf); read(sd); while (sf--) { read(x); add(1+x,i+F+1,1); } while (sd--) { read(x); add(1+F+N+i,1+F+2*N+x,1); } } while (BFS()) { ans += maxflow(1,2e9); } cout<< ans << endl; return 0; }