标签:type define art ota lan record 钥匙 tab sort
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20738 | Accepted: 9481 |
Description
Input
Output
Sample Input
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6
Sample Output
7
题目链接:POJ 1149
题意:有M个猪圈,每一个猪圈有初始数量的猪,所有猪都是一样的,有N个人,每一个人有K把钥匙可以打开K个猪圈,对与被打开的猪圈可以进行重新分配猪的数量,每个人又有要买的猪的数量,每一个人按照输入的顺序进来购买,求最后所有人能买到的猪的最大个数。
解题思路:首先肯定是在源点连到每一个猪圈,容量为该猪圈的初始数量;然后把人连到汇点,容量为人要购买的头数,由于当前买的时候可能对应猪圈已经被重新分配过,因此不是从源点连过来,而是从上一个拥有这个猪圈的钥匙的人连过来一条容量为无穷大的边;若你是第一个开猪圈的人则要从猪圈连过来一条容量为无穷大的边。然后求源点到汇点的最大流即可。
代码:
#include <stdio.h> #include <iostream> #include <algorithm> #include <cstdlib> #include <sstream> #include <numeric> #include <cstring> #include <bitset> #include <string> #include <deque> #include <stack> #include <cmath> #include <queue> #include <set> #include <map> using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 1110; const int M = 110 + N + 110 * N + N; struct edge { int to, nxt, cap; edge(int To = 0, int Nxt = 0, int Cap = 0): to(To), nxt(Nxt), cap(Cap) {} }; edge E[M << 1]; int head[N], d[N], tot; int opened[N]; void init() { CLR(head, -1); tot = 0; CLR(opened, 0); } inline void add(int s, int t, int cap) { E[tot] = edge(t, head[s], cap); head[s] = tot++; E[tot] = edge(s, head[t], 0); head[t] = tot++; } int bfs(int s, int t) { CLR(d, -1); d[s] = 0; queue<int>q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; ~i; i = E[i].nxt) { int v = E[i].to; if (d[v] == -1 && E[i].cap > 0) { d[v] = d[u] + 1; if (v == t) return 1; q.push(v); } } } return ~d[t]; } int dfs(int s, int t, int f) { if (s == t || !f) return f; int ret = 0; for (int i = head[s]; ~i; i = E[i].nxt) { int v = E[i].to; if (d[v] == d[s] + 1 && E[i].cap > 0) { int df = dfs(v, t, min(f, E[i].cap)); if (df > 0) { E[i].cap -= df; E[i ^ 1].cap += df; ret += df; f -= df; if (!f) break; } } } if (!ret) d[s] = -1; return ret; } int Dinic(int s, int t) { int ret = 0; while (bfs(s, t)) ret += dfs(s, t, INF); return ret; } int main(void) { int m, n, b, c, i, j, k, id; while (~scanf("%d%d", &m, &n)) { init(); int S = 0, T = n + m + 1; for (i = 1; i <= m; ++i) { scanf("%d", &c); add(S, i, c); } for (i = 1; i <= n; ++i) { scanf("%d", &k); for (j = 0; j < k; ++j) { scanf("%d", &id); if (!opened[id]) add(id, m + i, INF); else add(opened[id], m + i, INF); opened[id] = m + i; } scanf("%d", &b); add(m + i, T, b); } printf("%d\n", Dinic(S, T)); } return 0; }
标签:type define art ota lan record 钥匙 tab sort
原文地址:http://www.cnblogs.com/Blackops/p/6351677.html