标签:customer any another bec return line bfs written numbers
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6Sample Output
7
题意 : 有N个顾客,有M个猪圈,每个猪圈有一定的猪,在开始的时候猪圈都是关闭的,
顾客来买猪,顾客打开某个猪圈,可以在其中挑选一定的猪的数量,在这个顾客走后,可以在打开的
猪圈中将某个猪圈的一些猪牵到另外一个打开的猪圈,然后所有的猪圈会关闭,这样下一个顾客来了继续上面的工作
思路分析 :
参考此文章即可 :http://wenku.baidu.com/view/0ad00abec77da26925c5b01c.html
代码示例 :
const int maxn = 1e4+5; const int inf = 0x3f3f3f3f; int m, n; int a[1005]; vector<int>ve[1005]; struct node { int to, flow; int next; }e[maxn]; int head[maxn]; int cnt = 0; int s, t; void addedge(int u, int v, int w){ e[cnt].next = head[u], e[cnt].to = v, e[cnt].flow = w, head[u] = cnt++; e[cnt].next = head[v], e[cnt].to = u, e[cnt].flow = 0, head[v] = cnt++; } int belong[1005]; int dep[1005], que[maxn]; bool bfs(int s, int t){ int head1 = 0, tail = 1; memset(dep, 0, sizeof(dep)); que[0] = s; dep[s] = 1; while(head1 < tail){ int v = que[head1++]; for(int i = head[v]; i != -1; i = e[i].next){ int to = e[i].to; if (e[i].flow && !dep[to]) { dep[to] = dep[v]+1; que[tail++] = to; } } } return dep[t]; } int dfs(int u, int f1){ if (f1 == 0 || u == t) return f1; int f = 0; for(int i = head[u]; i != -1; i = e[i].next){ int to = e[i].to; if (e[i].flow && dep[to] == dep[u]+1){ int x = dfs(to, min(e[i].flow, f1)); e[i].flow -= x; e[i^1].flow += x; f1 -= x, f += x; if (f1 == 0) return f; } } if (!f) dep[u] = -2; return f; } int maxflow(int s, int t){ int ans = 0; while(bfs(s, t)){ ans += dfs(s, inf); } return ans; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int x, y; cin >> m >> n; s = 0, t = n+1; memset(head, -1, sizeof(head)); for(int i = 1; i <= m; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++){ scanf("%d", &x); for(int j = 1; j <= x; j++) { scanf("%d", &y); ve[i].push_back(y); } scanf("%d", &y); addedge(i, t, y); } for(int i = 1; i <= n; i++){ for(int j = 0; j < ve[i].size(); j++){ int v = ve[i][j]; if (!belong[v]) { belong[v] = i; addedge(s, i, a[v]); } else { addedge(belong[v], i, inf); belong[v] = i; } } } printf("%d\n", maxflow(s, t)); return 0; }
标签:customer any another bec return line bfs written numbers
原文地址:https://www.cnblogs.com/ccut-ry/p/9932842.html