裸的费用流啊。。。

建图:对于一个点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. Codeforces Round #375 (Div. 2) C. Polycarp at the Radio 贪心

    C. Polycarp at the Radio time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. MongoDB资料汇总

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 它的特点是高性能.易部署.易使用,存储数据非常方便.主要功能特性有: 面向集合存 ...

  3. ThreadLocal 实现线程内共享变量

    package com.cn.gbx; import java.util.Date; import java.util.Random; import java.util.Timer; import j ...

  4. RARP

    ARP的工作原理如下:1. 首先,每台主机都会在自己的ARP缓冲区 (ARP Cache)中建立一个 ARP列表,以表示IP地址和MAC地址的对应关系.2. 当源主机需要将一个数据包要发送到目的主机时 ...

  5. hibernate对象关系实现(二)一对一

    双向一对一以部门和经理为例: a.部门和经理类中各自由对方的引用:(省略了get/set方法) b.数据库两种方式实现:一种(b.1)是外键映射,并将外键添加唯一约束(至于哪个对象的主键做外键,可随意 ...

  6. javascript js中的typeof使用

    typeof运算符介 绍:typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型.它返回值是一个字符串,该字符串说明运算数的类型. 你 知道下面typeof运算的结果吗? typeof ...

  7. 删除github账号的方法

    如果你不想使用自己的github账号了,github官网允许你删除账号,具体操作步骤为: 1.登录github网站,网站右上角的头像,在下拉菜单中选择"Settings"(设置): ...

  8. 注意64位整形,int64,long long

    在Xplorer中编译运行: int64_t  mask = 0xffffffffffffffffLL; if((j+64)>width) { printf("%x %d " ...

  9. Hbase之批量删除数据

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...

  10. ListView配合BaseAdapter

    BaseAdapter使用比较麻烦,它是个抽象类,需要重写4个方法分别是getCount() getItem(..) getItemId(..) getVew(..),相应的使用BaseAdapter ...