【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;
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值