裸的费用流啊。。。

建图:对于一个点p拆成两个p1和p2,S向p1连边,流量为1,费用为0;p2向T连边流量为1,费用为0

然后i1向a2到b2分别连边,不妨设i1向p2连边,流量为1,费用为|i - p| * ki

跑一下费用流,如果流量不为n,NIE!然后答案就是费用之和。。。

 /**************************************************************
Problem: 1520
User: rausen
Language: C++
Result: Accepted
Time:1296 ms
Memory:2380 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std;
const int N = ;
const int M = 1e5 + ;
const int inf = 1e9; struct edges {
int next, to, f, cost;
edges() {}
edges(int _n, int _t, int _f, int _c) : next(_n), to(_t), f(_f), cost(_c) {}
} e[M]; int n, S, T;
int first[N], tot = ;
int q[N], d[N], g[N];
bool v[N]; inline int read() {
int x = , sgn = ;
char ch = getchar();
while (ch < '' || '' < ch) {
if (ch == '-') sgn = -;
ch = getchar();
}
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return sgn * x;
} inline void Add_Edges(int x, int y, int f, int c) {
e[++tot] = edges(first[x], y, f, c), first[x] = tot;
e[++tot] = edges(first[y], x, , -c), first[y] = tot;
} inline int calc() {
int flow = inf, x;
for (x = g[T]; x; x = g[e[x ^ ].to])
flow = min(flow, e[x].f);
for (x = g[T]; x; x = g[e[x ^ ].to])
e[x].f -= flow, e[x ^ ].f += flow;
return flow;
} #define y e[x].to
bool spfa() {
int x, now, l, r;
for (x = ; x <= T; ++x)
d[x] = inf;
d[S] = , v[S] = , q[] = S;
for(l = r = ; l != (r + ) % N; ) {
now = q[l], ++l %= N;
for (x = first[now]; x; x = e[x].next) {
if (d[now] + e[x].cost < d[y] && e[x].f) {
d[y] = d[now] + e[x].cost, g[y] = x;
if (!v[y]) {
v[y] = ;
if (d[y] < d[q[l]])
q[(l += N - ) %= N] = y;
else q[++r %= N] = y;
}
}
}
v[now] = ;
}
return d[T] != inf;
}
#undef y inline int work() {
static int res, tot;
res = , tot = ;
while (spfa()) {
tot += calc();
res += d[T];
}
if (tot == n) printf("%d\n", res);
else puts("NIE");
} int main() {
int i, j, a, b, k, m;
n = read(), S = * n + , T = S + ;
for (i = ; i <= n; ++i)
Add_Edges(S, i, , ), Add_Edges(i + n, T, , );
for (i = ; i <= n; ++i) {
m = read(), a = read(), b = read(), k = read();
for (j = a; j <= b; ++j)
Add_Edges(i, n + j, , abs((j - m) * k));
}
work();
return ;
}

BZOJ1520 [POI2006]Szk-Schools的更多相关文章

  1. 【费用流】bzoj1520 [POI2006]Szk-Schools

    注意:建图的时候,一定要把旧标号相同的分开. #include<cstdio> #include<algorithm> #include<cstring> #inc ...

  2. 【BZOJ1520】[POI2006]Szk-Schools KM算法

    [BZOJ1520][POI2006]Szk-Schools Description Input Output 如果有可行解, 输出最小代价,否则输出NIE. Sample Input 5 1 1 2 ...

  3. 【bzoj1520】[POI2006]Szk-Schools 费用流

    题目描述 输入 输出 如果有可行解, 输出最小代价,否则输出NIE. 样例输入 5 1 1 2 3 1 1 5 1 3 2 5 5 4 1 5 10 3 3 3 1 样例输出 9 题解 费用流 设xi ...

  4. POJ1236Network of Schools[强连通分量|缩点]

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16571   Accepted: 65 ...

  5. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  6. Network of Schools --POJ1236 Tarjan

    Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...

  7. BZOJ 1121: [POI2008]激光发射器SZK

    1121: [POI2008]激光发射器SZK Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 792  Solved: 653[Submit][Sta ...

  8. 【BZOJ-1121】激光发射器SZK 物理 + 数学 + 乱搞

    1121: [POI2008]激光发射器SZK Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 682  Solved: 565[Submit][Sta ...

  9. POJ 1236 Network of Schools(强连通分量/Tarjan缩点)

    传送门 Description A number of schools are connected to a computer network. Agreements have been develo ...

随机推荐

  1. jQuery实现的简单文字提示效果模拟title(转)

    来源 http://www.cnblogs.com/puzi0315/archive/2012/10/17/2727693.html 模拟title实现效果,可以修改文字的样式,换行等. 文件下载: ...

  2. MVC服务器前台提示

    [HttpPost] public ActionResult AddMsg(MsgModel model) { string strSql = "insert into tbl_msg(ti ...

  3. Data Science 和 Finance 两个领域的融合是什么样子的?

    Data Science 和 Finance 两个领域的融合是什么样子的? 2015-05-24 董可人 现在大部分人所说的Quant一般是指各大投行里做衍生品定价,信用评估,风险控制之类工作的人,这 ...

  4. Nginx基础知识之————多模块(非覆盖安装、RTMP在线人数实例安装测试)

    说明:已经安装好的nginx,需要添加一个未被编译安装的模块,需要怎么弄呢? 具体:这里以安装第三方nginx-rtmp-module和nginx-accesskey-2.0.3模块为例,nginx的 ...

  5. c 函数调用产生的汇编指令和数据在内存情况(1)

    一直对函数调用的具体汇编指令和各种变量在内存的具体分配,一知半解.各种资料都很详细,但是不实践,不亲自查看下内存总不能笃定.那就自己做下. 两个目的: 一,函数和函数调用编译后的汇编指令基本样貌 二, ...

  6. 线程高级应用-心得5-java5线程并发库中Lock和Condition实现线程同步通讯

    1.Lock相关知识介绍 好比我同时种了几块地的麦子,然后就等待收割.收割时,则是哪块先熟了,先收割哪块. 下面举一个面试题的例子来引出Lock缓存读写锁的案例,一个load()和get()方法返回值 ...

  7. maven的聚合与继承5

    一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 1 <modules> 2 <module>模块一</module&g ...

  8. JavaWeb学习总结(六)—HttpServletResponse

    Response概述: response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServletResponse.在客户端发出每个请求时,服务 ...

  9. 【Todo】pthread_key_t 和 pthread_once_t学习

    这两个函数应该都是和线程局部变量有关的.有时间学习一下. 可以参考如下文章: <Linux线程私有数据pthread_key_t> <posix多线程有感--线程高级编程(pthre ...

  10. 检测到在集成的托管管道模式下不适用的 ASP.NET 设置。

    <!--解决让程序在集成模块通道下运行--> <system.webServer> <validation validateIntegratedModeConfigura ...