由于对于每个牛有2个限制,所以要把牛拆成两个点,连一条边容量为1 他可以吃的food和牛的左端点相连,drink和右端点相连。然后每个food和源点相连容量为drink和汇点相连容量为1,从而达到限制每个食物只被一个牛吃的目的。然后就流吧
主要代码如下:略去网络流模板
int main() { int csc, s, t, vfrom, vto, tmpcap; int n, d, f, tmpd, tmpf; while (scanf("%d %d %d", &n, &f, &d) == 3) { s = 0, t = f + n * 2 + d + 1; maxflow.firststart(); for (int i = 1; i <= f; i++) { maxflow.addedge(s, i, 1); maxflow.addedge(i, s, 0); } for (int i = 1; i <= d; i++) { maxflow.addedge(f + n * 2 + i, t, 1); maxflow.addedge(t, f + n * 2 + i, 0); } for (int i = 1; i <= n; i++) { maxflow.addedge(f + i * 2 - 1, f + 2 * i, 1); maxflow.addedge(f + i * 2, f + i * 2 - 1, 0); } for (int i = 1; i <= n; i++) { scanf("%d %d", &tmpf, &tmpd); for (int j = 0; j < tmpf; j++) { int v; scanf("%d", &v); maxflow.addedge(v, f + i * 2 - 1, 1); maxflow.addedge(f + i * 2 - 1, v, 0); } for (int j = 0; j < tmpd; j++) { int v; scanf("%d", &v); maxflow.addedge(f + i * 2, f + n * 2 + v, 1); maxflow.addedge(f + n * 2 + v, f + 2 * i, 0); } } printf("%d\n", maxflow.maxflowsap(t + 1, s, t)); } return 0; }