题意:有n<=50个点,每个点有xi有[li, ri]种取值,-100 <= li <= ri <= 100,并且给定m<=100条边,每条边为u,v,d表示xu<=xv+d。

每个点value fi(x) = ai*x^2 + bi*x + ci。现在求一种合法的方案,使得权值和最大。

思路:先不考虑的xu<=xv + d。那么建图:

首先考虑到每个点的权值可能为负,并且求最大与最小割相反,

所以先 取反再+oo(一个很大的数),最后再减掉即可

对于每个点,拆成ri-li+1个点,

对于第k个点,node(k, i)表示第k个点值为i对应的标号

值为i-1跟i连一条边<node(k, i-1), node(k, i),  oo - f(k, i)>的边,

S到第一个点连<S, node(k, l[k]), f(k, l[k])>

最后一个点到T连<node(k,r[k]), Inf>

那么很明显n*oo-最小割就是答案。。

但是如果有了限制条件xu<=xv + d,我们怎么把限制条件加到图上呢?

对于一对关系,xu<=xv+d

考虑到点u,如果node(u, i)到node(u,i+1)之间的边在割集里,那么说明xu=i+1

也就是说如果xv<xu-d是非法的,也就是说对于v在xu-d之前出现割是非法的。

那么我们可以连<node(u,i), node(v, i-d), Inf>的边,使得方案合法。。

code:

 #include <bits/stdc++.h>
using namespace std;
#define M0(a) memset(a, 0, sizeof(a))
#define Inf 0x3fffffff
const int maxn = ;
const int maxm = ;
const int big = ;
struct oo{
int y, f, next;
};
struct MaxFlow{
int n, S, T, tot;
int son[maxn], dist[maxn], gap[maxn];
oo e[maxm];
int sap(int x, int aug){
if (x == T) return aug;
int mind = n;
int sum = , f;
for (int p = son[x]; p != -; p = e[p].next){
int y = e[p].y;
if (dist[y] + == dist[x] && e[p].f){
f = sap(y, min(e[p].f, aug - sum));
e[p].f -= f;
e[p^].f += f;
sum += f;
if (sum == aug || dist[S] >= n) return sum;
}
if (e[p].f) mind = min(mind, dist[y]);
}
if (!sum){
if (!(--gap[dist[x]])) dist[S] = n;
++gap[dist[x] = mind + ];
}
return sum;
}
void add(int x, int y, int f){
e[tot].y = y; e[tot].f = f;
e[tot].next = son[x]; son[x] = tot++;
e[tot].y = x; e[tot].f = ;
e[tot].next = son[y]; son[y] = tot++;
} void init(int S, int T, int n){
memset(son, -, sizeof(son));
tot = ;
this->S = S, this->T = T, this->n = n;
}
int maxflow(){
M0(gap);
M0(dist);
gap[] = n;
int ans = ;
while (dist[S] < n) ans += sap(S, Inf);
return ans;
}
} F;
int S, T;
int n, m, a[], b[], c[], l[], r[];
inline int f(const int&k, const int& x){
return big - (a[k] * x * x + b[k] * x + c[k]);
} inline int node(const int&k, const int& x){
return x == l[k] - ? S : (k-) * + x + ;
} void solve(){
for (int i = ; i <= n; ++i) scanf("%d%d%d", &a[i], &b[i], &c[i]);
for (int i = ; i<= n; ++i) scanf("%d%d", &l[i], &r[i]);
S = , T = * n + ;
F.init(S, T, T + );
for (int i = ; i <= n; ++i){
for (int j = l[i]; j <= r[i]; ++j)
F.add(node(i, j-), node(i, j), f(i, j));
F.add(node(i, r[i]), T, Inf);
}
int u, v, d;
int x;
for (int i = ; i <= m; ++i){
scanf("%d%d%d", &u, &v, &d);
for (int j = l[v]; j <= r[v]; ++j) if (j + d <= r[u]){
x = j + d;
if (x < l[u]) x = l[u] -;
F.add(node(u, x), node(v, j), Inf);
}
}
int ans = big * n;
ans -= F.maxflow();
cout << ans << endl;
} int main(){
// freopen("a.in", "r", stdin);
while (scanf("%d%d", &n, &m) != EOF){
solve();
}
}

codeforces 434D的更多相关文章

  1. CodeForces - 434D Nanami's Power Plant

    Codeforces - 434D 题目大意: 给定一个长为n的序列,序列中的第i为上的值\(x_i\),序列第i位上的值\(x_i\in[l_i,r_i]\),价值为\(f_i(x_i)\),其中\ ...

  2. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. 简单DP入门四连发

    复习一下一直不太懂的dp. dp博大精深,路还长着呢 第一题;http://acm.hdu.edu.cn/showproblem.php?pid=2084 从下往上就是水题 #include<c ...

  2. 关于ip包长度

    http://blog.csdn.net/naturebe/article/details/6712153 这篇文章总结的不错,转自:http://hi.baidu.com/to_wait/blog/ ...

  3. Codeforces55D Beautiful numbers

    原题链接 虽然依旧是套模板,但是因为我太弱了,不会建状态,所以去看了题解.. 这里就直接引用我看的题解吧,写的不错的. 题解 //我的代码 #include<cstdio> #includ ...

  4. AppStore企业账号打包发布APP流程详解

    一.通过企业账号申请证书 1 Certificate Signing Request (CSR)文件 在Mac系统中进入“钥匙串访问”,选择“钥匙串访问”-“证书助理”-“从证书颁发机构请求证书…”, ...

  5. 20172306 《Java程序设计与数据结构》第七周学习总结

    20172306<Java程序设计>第七周学习总结 教材学习内容总结 这一章的标题是继承.主要学习了有关继承的相关知识.其中在这五节中,我学到了以下几点: 1.继承主要表达的是" ...

  6. 一个漂亮的lazarus做的pagecontrol

    厌倦了屏幕上的默认灰色?让我们来欣赏一下商业配色. 这个组件实现了高光,点睛色,描边边等效果, 再配几组色彩,应该非常不错. 基于 lazarus 1.08 大家可以上 www.fpccn.com 看 ...

  7. 设计师们做UI设计和交互设计、界面设计等一般会去什么网站呢?

    明明可靠颜值吃饭,却偏偏要靠才华立身,UI设计师就是这样一群神奇的物种.面对“大的同时小一点”.“五彩斑斓黑”.“下班之前给我”……这些甲方大大刁钻的需求,设计师每天都在咬牙微笑讨生活.你可以批评我的 ...

  8. [Selenium Grid] 搭建Hub和Node环境

    Note : 先在Hub和Node的机器上安装好JDK,  IE浏览器,Chrome浏览器,Firefox浏览器 准备好红色框标示的东西: Hub.bat :启动Hub AllNodes.bat  : ...

  9. Spring ConversionService 类型转换(一)Converter

    Spring ConversionService 类型转换(一)Converter Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.h ...

  10. Can I win LT464

    In the "100 game," two players take turns adding, to a running total, any integer from 1.. ...