题目大意:有一个屌丝要给M个女神拍照,每个女神至少要拍G张照片
给出屌丝每天的拍照任务,每天要给C个女神拍照,因为内存有限,所以每天至多拍D张照片
女神i要求该天至少要拍L张,至多拍R张
问屌丝能否满足所有的女神的要求,因为是屌丝,当然希望拍的照片越多越好了,所以,要求求出这个屌丝能拍的最多张照片是多少张
解题思路:有源有汇上下界最大流,将每个女神抽象成一个点,每天抽象成一个点,每个任务抽象成一条边,建图
源点连接每一天,下界为0,上界为D
汇点连接每个女神,下界为G,上界为INF
每一天和该天任务的女神连线,下界为L,上界为R
附上一个有讲解上下界最大流的传送门
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXNODE = 1400;
const int MAXEDGE = 1000010;
typedef int Type;
const Type INF = 0x3f3f3f3f;
struct Edge{
int u, v, next;
Type cap, flow;
Edge() {}
Edge(int u, int v, Type cap, Type flow, int next) : u(u), v(v), cap(cap), flow(flow), next(next){}
};
struct Dinic{
int n, m, s, t;
Edge edges[MAXEDGE];
int head[MAXNODE];
int cur[MAXNODE];
bool vis[MAXNODE];
Type d[MAXNODE];
vector<int> cut;
void init(int n) {
this->n = n;
memset(head, -1, sizeof(head));
m = 0;
}
void AddEdge(int u, int v, Type cap) {
edges[m] = Edge(u, v, cap, 0, head[u]);
head[u] = m++;
edges[m] = Edge(v, u, 0, 0, head[v]);
head[v] = m++;
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = head[u]; ~i; i = edges[i].next) {
Edge &e = edges[i];
if (!vis[e.v] && e.cap > e.flow) {
vis[e.v] = true;
d[e.v] = d[u] + 1;
Q.push(e.v);
}
}
}
return vis[t];
}
Type DFS(int u, Type a) {
if (u == t || a == 0) return a;
Type flow = 0, f;
for (int &i = cur[u]; i != -1; i = edges[i].next) {
Edge &e = edges[i];
if (d[u] + 1 == d[e.v] && (f = DFS(e.v, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[i ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
Type Maxflow(int s, int t) {
this->s = s; this->t = t;
Type flow = 0;
while (BFS()) {
for (int i = 0; i < n; i++)
cur[i] = head[i];
flow += DFS(s, INF);
}
return flow;
}
void Mincut() {
cut.clear();
for (int i = 0; i < m; i += 2) {
if (vis[edges[i].u] && !vis[edges[i].v])
cut.push_back(i);
}
}
}dinic;
#define maxn 2010
int source, sink, SuperSource, SuperSink, n, m;
int d[maxn], ans[MAXEDGE], MaxDay[maxn];
void solve() {
source = n + m, sink = source + 1;
SuperSource = sink + 1, SuperSink = SuperSource + 1;
dinic.init(SuperSink + 1);
memset(d, 0, sizeof(d));
int G;
for (int i = 0; i < m; i++) {
scanf("%d", &G);
d[sink] += G;
d[i + n] -= G;
dinic.AddEdge(i + n, sink, INF);
}
int start = dinic.m;
int cnt = 0, T, L, R, C;
for (int i = 0; i < n; i++) {
scanf("%d%d", &C, &MaxDay[i]);
for (int j = 0; j < C; j++) {
scanf("%d%d%d", &T, &L, &R);
d[i] -= L;
d[T + n] += L;
ans[cnt] = L;
dinic.AddEdge(i, T + n, R - L);
cnt++;
}
}
int end = dinic.m;
for (int i = 0; i < n; i++)
dinic.AddEdge(source, i, MaxDay[i]);
int Sum = 0;
for (int i = 0; i <= sink; i++) {
if (d[i] > 0) {
Sum += d[i];
dinic.AddEdge(SuperSource, i, d[i]);
}
if (d[i] < 0) dinic.AddEdge(i, SuperSink, -d[i]);
}
dinic.AddEdge(sink, source, INF);
if (dinic.Maxflow(SuperSource, SuperSink) != Sum) printf("-1\n");
else {
dinic.head[SuperSource] = -1;
dinic.head[SuperSink] = -1;
int flow = dinic.Maxflow(source, sink);
printf("%d\n", dinic.edges[dinic.m - 2].flow + flow);
for (int i = start, j = 0; i < end; i += 2, j++)
printf("%d\n", ans[j] + dinic.edges[i].flow);
}
printf("\n");
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) solve();
return 0;
}