rt,已经调麻了。
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000000000
#define V 100010
#define E 5000010
typedef long long int ll;
struct edge {
int to, next;
ll capa;
};
int cnt = 0, head[V], n, m, st, ed; edge node[E]; ll judge = 0;
inline void add(int fir, int nxt, ll w) {
node[cnt].to = nxt,
node[cnt].capa = w,
node[cnt].next = head[fir],
head[fir] = cnt++;
}
int s, t, dep[V], gap[V], cur[V]; queue<int>que; ll sum = 0;
inline void initing() {
memset(dep, -1, V * sizeof(int));
memcpy(cur, head, (t + 1) * sizeof(int));
}
inline void bfs() {
int fro, ito;
que.push(t); dep[t] = 0; ++gap[dep[t]];
while (!que.empty()) {
fro = que.front(); que.pop();
for (register int i = head[fro]; ~i; i = node[i].next) {
ito = node[i].to;
if (dep[ito] == -1) {
dep[ito] = dep[fro] + 1;
que.push(ito);
++gap[dep[ito]];
}
}
}
}
ll dfs(int u, ll flow) {
if (u == t || flow == 0)return flow; ll used = 0, wei = 0;
for (register int i = cur[u]; ~i; i = node[i].next) {
cur[u] = i;
if (dep[u] == dep[node[i].to] + 1 && node[i].capa) {
wei = dfs(node[i].to, min(flow - used, node[i].capa));
if (wei) {
node[i].capa -= wei;
node[i ^ 1].capa += wei;
used += wei;
}
}
if (used == flow)return used;
}
--gap[dep[u]];
if (!gap[dep[u]])dep[s] = t + 1;
++gap[++dep[u]];
return used;
}
ll ISAP() {
initing(); bfs();
while (dep[s] < t) {
sum += dfs(s, inf);
memcpy(cur, head, (t + 1) * sizeof(int));
}
return sum;
}
ll tot[V];
inline void addE(int u, int v, ll l, ll r) {
add(u, v, r - l);
add(v, u, 0);
tot[u] -= l; tot[v] += l;
}
inline void addedge(int u, int v, ll w) {
add(u, v, w);
add(v, u, 0);
}
inline void clear() {
for (int i = 0; i < cnt; i++)node[i].capa = node[i].to = 0, node[i].next = -1;
for (int i = 0; i <= t + 2; i++)cur[i] = head[i] = dep[i] = -1, gap[i] = 0;
cnt = 0; n = m = st = ed = s = t = 0;
}
inline ll solve() {
sum = 0;
for (int i = 1; i <= ed; i++) {
if (tot[i] > 0)addedge(s, i, tot[i]), judge += tot[i];
else if (tot[i] < 0)addedge(i, t, -tot[i]);
tot[i] = 0;
}
addedge(ed, st, inf); ISAP();
ll ans = 0;
if (sum != judge){clear(); return -1;}
else {
ans = node[cnt - 1].capa;
node[cnt - 1].capa = node[cnt - 2].capa = 0;
s = st, t = ed;
ans += ISAP();
}
clear();
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(); cout.tie();
memset(head, -1, V * sizeof(int));
int f, l; ll w;
while(cin >> n >> m) {
st = n + m + 1, ed = n + m + 2;
s = ed + 1, t = ed + 2; ll g;
for (int i = 1; i <= m; i++) {
cin >> g;
addE(i, ed, g, inf);
}
int c, bh; ll d, l, r;
for (int i = 1; i <= n; i++) {
cin >> c >> d;
addedge(st, i + m, d);
for (int j = 1; j <= c; j++) {
cin >> bh >> l >> r;
addE(i + m, bh + 1, l, r);
}
}
cout << solve() << "\n\n";
}
return 0;
}