maximum value
Time limit: 3000 ms Memory limit: 131072 KB
Topic description
Shy has n power stations. Each power station has a level (a positive or negative integer), the level of power station i should be between l[i], r[i] (inclusive), and Level x will bring fi(x) power generation quantity.
Shy also has m limits. The restriction is in the form, x[u]≤x[v]+d, which means that the level of u is less than or equal to the level of v plus d (d is an integer).
What is the maximum power output?
enter
The first line contains two integers n, where m represents the number of power stations and the number of restrictions; in the
next n lines, three integers ai,bi,ci represent fi,fi(x)=ai x x+bi*x+ ci;
the next n lines each have two integers l[i], r[i];
the next m lines, each line contains three integers u, v, d, indicating x[u]≤x[v]+d.
output
A positive integer represents the answer.
input sample
5 8
1 -8 20
2 -4 0
-1 10 -10
0 1 0
0 -1 1
1 9
1 4
0 10
3 11
7 9
2 1 3
1 2 3
2 3 3
3 2 3
3 4 3
4 3 3
4 5 3
5 4 3
Sample output
46
data scale
For 30% of the data, 1≤n≤3;
for 100% of the data, 1≤n≤50, 0≤m≤100, |ai|≤10, |bi|≤1000, |ci|≤1000, -100 ≤|li|≤|ri|≤100, 1≤u, v≤n, u≠v, |di|≤200.
network flow.
Water blogging for reasons like a math problem. . . Not tired.
You need to learn to optimize the current arc, otherwise you will be stuck (90 points)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010, s = 0, t = 100005, INF = 0x3f3f3f3f;
struct lpl{
int to, dis;
}lin;
queue<int> q;
vector<lpl> edge;
vector<int> point[maxn];
int layer[maxn], itr[maxn];
int n, m, cnt = -1, ans, a[105], b[105], c[105], l[105], r[105], u[105], v[105], d[105];
inline void putit()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%d%d%d", &a[i], &b[i], &c[i]);
for(int i = 1; i <= n; ++i) scanf("%d%d", &l[i], &r[i]);
for(int i = 1; i <= m; ++i) scanf("%d%d%d", &u[i], &v[i], &d[i]);
}
inline void connect(int A, int B, int D)
{
cnt++; lin.to = B; lin.dis = D; edge.push_back(lin); point[A].push_back(cnt);
cnt++; lin.to = A; lin.dis = 0; edge.push_back(lin); point[B].push_back(cnt);
}
inline int calc(int t, int now)
{
return a[t] * now * now + b[t] * now + c[t];
}
inline int id(int data, int line)
{
int ret = (line - 1) * 201;
ret += (data - l[line] + 1);
return ret;
}
inline void prepare()
{
for(int i = 1; i <= n; ++i){
connect(s, (i - 1) * 201 + 1, INF);
int p = 1;
for(int j = l[i]; j < r[i]; ++j){
connect((i - 1) * 201 + p, (i - 1) * 201 + p + 1, 0 - calc(i, j) + 10000005);
p++;
}
connect((i - 1) * 201 + p, t, 0 - calc(i, r[i]) + 10000005);
}
int beg, en, now;
for(int i = 1; i <= m; ++i){
beg = l[v[i]]; en = r[v[i]];
for(int j = beg; j <= en; ++j){
now = j + d[i];
if(now >= r[u[i]]) break;
if(j == en) {connect(id(now, u[i]) + 1, t, INF); break;}
connect(id(now, u[i]) + 1, id(j, v[i]) + 1, INF);
}
}
}
inline bool bfs()
{
memset(itr, 0, sizeof(itr));
memset(layer, 0, sizeof(layer));
q.push(s); layer[s] = 1;
while(!q.empty()){
int now = q.front(); q.pop();
for(int i = point[now].size() - 1; i >= 0; --i){
int qwe = point[now][i];
if(edge[qwe].dis <= 0 || layer[edge[qwe].to] != 0) continue;
layer[edge[qwe].to] = layer[now] + 1;
q.push(edge[qwe].to);
}
}
return layer[t];
}
inline int dfs(int a, int w)
{
if(a == t || w == 0) return w;
int ret = 0, llppdd = point[a].size() - 1;
for(int i = itr[a]; i <= llppdd; ++i){
int now = point[a][i];
if(layer[edge[now].to] != layer[a] + 1 || edge[now].dis <= 0) continue;
int tmp = dfs(edge[now].to, min(w, edge[now].dis));
edge[now].dis -= tmp; edge[now ^ 1].dis += tmp; w -= tmp; ret += tmp;
itr[a] = i;
if(!w) break;
}
return ret;
}
inline void Dinic()
{
while(bfs()) ans += dfs(s, INF);
printf("%d", n * 10000005 - ans);
}
int main()
{
putit();
prepare();
Dinic();
return 0;
}