裸的费用流啊。。。

建图:对于一个点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. sql server日期时间函数

    From:http://www.cnblogs.com/linzheng/archive/2010/11/17/1880208.html 1.  当前系统日期.时间  select getdate() ...

  2. nyoj 19擅长排列的小明 (DFS)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  3. netbeans环境的建立

    这是一个简单的实验,熟悉NetBeans的IDE环境的开发 首先下载一个NetBeans,可以在官网上下https://netbeans.org/downloads/index.html 要装NetB ...

  4. rpm 更新/升级 软件包(libGL-devel手动安装过程)

    rpm参数解释 -i 安装 -h 解压rpm的时候打印50个斜条 (#) -v 显示详细信息 升级命令rpm -Uvh rpm文件名 参数解释 -U 升级 -h 解压rpm的时候打印50个斜条 (#) ...

  5. 阻塞与非阻塞的IO网络读写

    看我之前的文章就知道,一般对于网络读的socket,都会加上O_NONBLOCK,非阻塞的选项. int setnonblocking(int fd) { int old_option = fcntl ...

  6. Object Pascal 方法与技巧

    4 方法与技巧 4.1 设置代码模板 代码模板是Delphi 的代码感知特性的一种,通过它可以快速.高效和正确地输入代码.代码模板将一些常用的语句块保存在模板中,然后程序员只要在代码编辑器中按下“Ct ...

  7. 数位dp——统计'1'的个数

    今天去牛客网看了看 包含一 这道题,一开始没看清,以为它要统计 1~n 所有数中数字 '1' 出现的总次数,也就是说,若 n == 11,则 ans = 4:而按照题目的原意 ans 应该为 3.看错 ...

  8. html的空格和换行显示【摘自网络】

    一.HTML 代码中的所有连续的空格或空行(换行)都会被显示为一个空格,不管是内容还是标签之间. 二.当我们想让它们在同一行连续显示时,就让所有的代码之间没有空格,也不要换行. 三.当我们想要显示连续 ...

  9. [html] Webp、Apng图片格式

    WebP格式,谷歌(google)开发的一种旨在加快图片加载速度的图片格式.图片压缩体积大约只有JPEG的2/3,并能节省大量的服务器带宽资源和数据空间. 与JPEG相同,WebP是一种有损压缩.但谷 ...

  10. git的作用和原理(待续)

    先选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令: $ sudo git init --bare sample.git Git就会创建一个裸仓库,裸仓库没有 ...