简单的说,就是我们想知道取其中的一些点,使得他们之间满足题目中给出的要求,然后问这样的集合的权值的最大值。
将问题这样展开之后,实际上就是一个最大权闭合子图问题了,什么是最大权闭合子图?就是求一个集合,集合的所有的点的出边所指向的点也在集合内,求这样的集合的点的权值和的最大。
于是,这个问题就是一个网络流求最大权闭合子图问题了。
分两种状态考虑。
情况1:1 x y
那么,我们让,意思是我们要取x的话,就必须取y。
情况2:2 x y
我们让,指的是要取y的端点,就需要取x。
于是,就可以建图跑了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define pii pair<int, int>
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 2e4 + 7, maxM = 2.4e5 + 7;
int N, M;
int L[maxN], R[maxN];
ll A[maxN], B[maxN], C[maxN], val[maxN][2];
int fu_2a_b[maxN], pos[maxN][2];
namespace Graph
{
int head[maxN], cnt;
struct Eddge
{
int nex, to; ll flow;
Eddge(int a=-1, int b=0, ll c=0):nex(a), to(b), flow(c) {}
} edge[maxM];
inline void addEddge(int u, int v, ll w)
{
edge[cnt] = Eddge(head[u], v, w);
head[u] = cnt++;
}
inline void _add(int u, int v, ll w) { addEddge(u, v, w); addEddge(v, u, 0); }
inline void init()
{
cnt = 0;
for(int i=0; i<=(N << 1) + 1; i++) head[i] = -1;
}
};
namespace Dinic
{
using namespace Graph;
int S, T, cur[maxN], node;
int deep[maxN], que[maxN], top, tail;
inline bool bfs()
{
for(int i=0; i<=node; i++) deep[i] = 0;
top = tail = 0; que[tail++] = S; deep[S] = 1;
while(top < tail)
{
int u = que[top++];
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to; ll f = edge[i].flow;
if(!deep[v] && f)
{
deep[v] = deep[u] + 1;
que[tail++] = v;
}
}
}
return deep[T];
}
ll dfs(int u, ll dist)
{
if(u == T) return dist;
for(int &i = cur[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
ll f = edge[i].flow;
if(deep[v] == deep[u] + 1 && f)
{
ll flow = dfs(v, min(f, dist));
if(flow)
{
edge[i].flow -= flow;
edge[i ^ 1].flow += flow;
return flow;
}
}
}
return 0;
}
inline ll Max_Flow()
{
ll ans = 0, tmp;
while(bfs())
{
for(int i=0; i<=node; i++) cur[i] = head[i];
while((tmp = dfs(S, INF))) ans += tmp;
}
return ans;
}
};
using namespace Dinic;
inline ll Slope(ll a, ll b, ll c, ll x) { return a * x * x + b * x + c; }
int main()
{
scanf("%d%d", &N, &M);
init();
Dinic::S = 0; Dinic::T = (N << 1) + 1;
ll ans = 0;
for(int i=1, c[2]; i<=N; i++)
{
scanf("%d%d%lld%lld%lld", &L[i], &R[i], &A[i], &B[i], &C[i]);
fu_2a_b[i] = (int)round(-1. * B[i] / (2. * A[i]));
c[0] = L[i] + 1;
c[1] = R[i] - 1;
if(c[0] <= c[1])
{
pos[i][0] = fu_2a_b[i];
if(A[i] == 0)
{
if(Slope(A[i], B[i], C[i], c[0]) > Slope(A[i], B[i], C[i], c[1])) pos[i][0] = c[0];
else pos[i][0] = c[1];
}
else if(A[i] < 0)
{
if(fu_2a_b[i] >= c[0] && fu_2a_b[i] <= c[1]) pos[i][0] = fu_2a_b[i];
else
{
if(Slope(A[i], B[i], C[i], c[0]) > Slope(A[i], B[i], C[i], c[1])) pos[i][0] = c[0];
else pos[i][0] = c[1];
}
}
else
{
if(Slope(A[i], B[i], C[i], c[0]) > Slope(A[i], B[i], C[i], c[1])) pos[i][0] = c[0];
else pos[i][0] = c[1];
}
val[i][0] = Slope(A[i], B[i], C[i], pos[i][0]);
if(val[i][0] > 0)
{
_add(S, (i << 1) - 1, val[i][0]);
ans += val[i][0];
}
else if(val[i][0] < 0) _add((i << 1) - 1, T, -val[i][0]);
}
c[0]--;
c[1]++;
if(Slope(A[i], B[i], C[i], c[0]) > Slope(A[i], B[i], C[i], c[1])) pos[i][1] = c[0];
else pos[i][1] = c[1];
val[i][1] = Slope(A[i], B[i], C[i], pos[i][1]);
ll tmp = val[i][1] - val[i][0];
if(tmp > 0)
{
_add(S, (i << 1), tmp);
ans += tmp;
}
else if(tmp < 0) _add((i << 1), T, -tmp);
_add((i << 1), (i << 1) - 1, INF);
}
for(int i = 1, op, x, y; i <= M; i++)
{
scanf("%d%d%d", &op, &x, &y);
if(op == 1)
{
_add((y << 1) - 1, (x << 1) - 1, INF);
}
else
{
_add((y << 1), (x << 1) - 1, INF);
}
}
Dinic::node = Dinic::T;
printf("%lld\n", ans - Max_Flow());
return 0;
}