codeforces 434D
题意:有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的更多相关文章
- CodeForces - 434D Nanami's Power Plant
Codeforces - 434D 题目大意: 给定一个长为n的序列,序列中的第i为上的值\(x_i\),序列第i位上的值\(x_i\in[l_i,r_i]\),价值为\(f_i(x_i)\),其中\ ...
- Codeforce 水题报告(2)
又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- 转录组的组装Stingtie和Cufflinks
转录组的组装Stingtie和Cufflinks Posted: 十月 18, 2017 Under: Transcriptomics By Kai no Comments 首先这两款软件都是用 ...
- linux shell 重定向中的 & 符号
写一个简单的 demo 示例 #include <stdio.h> int main() { fprintf(stdout, "stdout output\n"); f ...
- SQL语句的三大类
数据定义语言(DDL Data Defination Language):用于创建和定义数据库对象,并且将对这些对象的定义保存到数据库字典中,通过DDL语句可以创建数据库对象.修改数据库对象.删除数据 ...
- 重新学pytorch
安装: conda install pytorch -c pytorch pip3 install torchvision pip install torchtext 这3个命令就够了
- tolua杂记
1 字符串调用luaFunc :DoString public class CallLuaFunction : MonoBehaviour { private string script = @&q ...
- nginx的hash
hash结构中有若干个桶,桶内是hash(key)值相同的若干数据. 查找数据时,首先对key值进行hash计算,然后hash值对桶的个数进行求余,得到数据所在的桶.然后在桶中使用key逐个查找,直到 ...
- 使用EventLog Analyzer监控、管理及分析日志
- java学习第六周
这是暑假学习的第六周,在这周我练习了老师给的例题,还是有一些地方看不懂,这周我对那些不懂的地方用看视频来进行解答,以及进行第二次复习. 下周我会对Java进行更加详细的复习,做好笔记,在LeetCod ...
- 初识kbmmw 5 中httpsys的支持
前两天kbmmw 发布了5.0 版.里面一个非常令人兴奋的特性就是原生内部支持http.sys. 有关http.sys 的介绍及优势,我就在这里不多说了,大家可以参照一下我以前的文章. 关于http. ...
- 52.tableViewCell重用机制避免重复显示问题
表刷新超出页面显示的内容会重复出现 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInd ...