传送门:点击打开链接
题意:有m个人,n个糖果,告诉你每个人喜欢哪些糖果,如果人分到喜欢的糖果幸福值+k,分到不喜欢的幸福值+1,现在告诉你每个人所需要的最低幸福值,问能否让所有人都开心。
思路:这是一道很6666的费用流的建模题
首先我们可以去分析,如果不喜欢,那么把这个糖果分给谁,幸福值都能+1,所以我们不如把s连接所有的糖果,容量为1,费用为0,如果糖果被人喜欢了,就连一条糖果到人的边容量为1费用为0,然后人连接t的边,我们需要拆成两条。令a=B[i]/k,b=B[i]%k,如果a大于0,说明1个糖果能让人的幸福值+k,所以容量为a费用为-k.如果b为0,说明他喜欢的糖果的数量如果是整数倍那就刚好能使他开心,就不考虑,如果b为1,那么这个糖果就和普通糖果没什么区别了,就不一定会是最优的。那么如果b>1,有一条边能通过,那么获得的幸福值肯定比把它当成普通糖果的要大,这样肯定是最优的,连一条边容量为1费用为b,然后跑一遍费用流,最大流就是特殊糖果,那么n-最大流就是当成普通的糖果了,费用就是已经满足了多少总幸福值,所以只需要验证费用+n-最大流是否大于等于sigma B[i]就行了..
#include<map> #include<set> #include<cmath> #include<ctime> #include<stack> #include<queue> #include<cstdio> #include<cctype> #include<string> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define fuck printf("fuck") #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w+",stdout) using namespace std; typedef long long LL; const int MX = 1000 + 5; const int MM = 1000 + 5; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow, cost; Edge() {} Edge(int _to, int _next, int _cap, int _flow, int _cost) { to = _to; next = _next; cap = _cap; flow = _flow; cost = _cost; } } E[MM]; int Head[MX], tol; int pre[MX]; //储存前驱顶点 int dis[MX]; //储存到源点s的距离 bool vis[MX]; int N;//节点总个数,节点编号从0~N-1 void init(int n) { tol = 0; N = n + 2; memset(Head, -1, sizeof(Head)); } void edge_add(int u, int v, int cap, int cost) { E[tol] = Edge(v, Head[u], cap, 0, cost); Head[u] = tol++; E[tol] = Edge(u, Head[v], 0, 0, -cost); Head[v] = tol++; } bool spfa(int s, int t) { queue<int>q; for (int i = 0; i < N; i++) { dis[i] = INF; vis[i] = false; pre[i] = -1; } dis[s] = 0; vis[s] = true; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for (int i = Head[u]; i != -1; i = E[i].next) { int v = E[i].to; if (E[i].cap > E[i].flow && dis[v] > dis[u] + E[i].cost) { dis[v] = dis[u] + E[i].cost; pre[v] = i; if (!vis[v]) { vis[v] = true; q.push(v); } } } } if (pre[t] == -1) return false; else return true; } //返回的是最大流, cost存的是最小费用 int minCostMaxflow(int s, int t, int &cost) { int flow = 0; cost = 0; while (spfa(s, t)) { int Min = INF; for (int i = pre[t]; i != -1; i = pre[E[i ^ 1].to]) { if (Min > E[i].cap - E[i].flow) Min = E[i].cap - E[i].flow; } for (int i = pre[t]; i != -1; i = pre[E[i ^ 1].to]) { E[i].flow += Min; E[i ^ 1].flow -= Min; cost += E[i].cost * Min; } flow += Min; } return flow; } int B[MX]; int main() { int T, ansk = 0; //FIN; scanf("%d", &T); while(T--) { int n, m, k; scanf("%d%d%d", &n, &m, &k); int sum = 0; for(int i = 1; i <= m; i++) { scanf("%d", &B[i]); sum += B[i]; } int s = 0, t = n + m + 1, ok; init(t); for(int i = 1; i <= n; i++) { edge_add(s, i, 1, 0); } for(int i = 1; i <= m; i++) { int a = B[i] / k, b = B[i] % k; if(a) edge_add(n + i, t, a, -k); if(b > 1) edge_add(n + i, t, 1, -b); for(int j = 1; j <= n; j++) { scanf("%d", &ok); if(ok) edge_add(j, n + i, 1, 0); } } int cost, flow = minCostMaxflow(s, t, cost); cost = -cost; printf("Case #%d: ", ++ansk); if(n - flow + cost >= sum) printf("YES\n"); else printf("NO\n"); } return 0; }