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 ...
随机推荐
- centos vncviewer
CentOS6.5 安装vncserver实现图形化访问 一. 安装gnome图形化桌面 #yum groupinstall -y "X Window System" #yum ...
- linux命令行下执行循环动作
在当前子目录下分别创建x86_64 for dir in `ls `;do (cd $dir;mkdir x86_64);done
- java.lang.NoClassDefFoundError: org/apache/ibatis/cursor/Cursor
因为mybatis的版本和mybatis-spring的版本不兼容导致的,解决方法:mybatis的3.4.0及以上版本用mybatis-spring1.3.0及以上版本:mybatis的3.4.0以 ...
- 洛谷1312 Mayan游戏
原题链接 讨厌这种大搜索题 基本就是模拟搜索,注意细节即可. 以下是我用的两个剪枝: 将块向左移的前提是左边为空,因为该题要求先右后左,所以若左边有块,那么在上一次搜索向右移的时候一定会搜过,且字典序 ...
- jQuery操作(一)
基本语法:$(selector).action() 寻找元素(重要的选择器和筛选器) 一:选择器 1.1 基本选择器: $("*") $("#id") $(&q ...
- PHP 批量移动文件改名
public function changeCoverName(){ //$type = '考研'; //$coverPath = './Public/course_cover/kaoyan/'; $ ...
- sql条件查询-日期比较(取年月部分)
查询当年当月的数据: select * from compalete_task where to_Char(create_date,'yyyyMM') = to_Char(sysdate,'yyyyM ...
- OCIlib的几个函数的执行效率(附上pro*c的性能对比)
ocilib提供了以下几个执行sql语句的函数 OCI_ExecuteStmt/OCI_ExecuteStmtFmt 使用没有绑定变量的语句 OCI_Execute 使用有绑定变量的语句 OCI_Im ...
- 利用PHP脚本辅助MySQL数据库管理3-删除重复表索引
<?php $dbi = new DbMysql; $dbi->dbh = 'mysql://root:mysql@127.0.0.1/coffeetest'; $map = array( ...
- Easyui form 表单中的 radio 无法一开始就选中原因
是因为你在form表单里面,所以会出现样式没有选中状态,必须要从后台去获取数据,然后再绑定.也就是初始化数据.