裸的费用流啊。。。

建图:对于一个点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. Android 布局简要范例

    Android的布局决定着实际的UI界面呈现情况,正是这些UI界面的组合与千变万化,才呈现出了各式各样的风格. 而这些基础的布局框架结构很重要,需要玩的很熟悉.我将以前参考的部分代码示例,所做的相关实 ...

  2. Codeforces 713D Animals and Puzzle

    题意:一个n*m的01矩阵,Q个询问,每次询问一个矩形区域内,最大的全1正方形的边长是多少? 题解:dp[0][0][i][j]表示以(i, j)为右下角的正方形的最长边长.RMQ后,二分答案即可. ...

  3. linux rwxrwxrwt文件夹属性

    /tmp 的permission是rwxrwxrwt chmod 0777 /abc       rwxrwxrwx chmod  777 /abc        rwxrwxrwx chmod 17 ...

  4. matplotlib库的常用知识

    看看matplotlib是什么? matplotlib是python上的一个2D绘图库,它可以在夸平台上边出很多高质量的图像.综旨就是让简单的事变得更简单,让复杂的事变得可能.我们可以用matplot ...

  5. z-score

    标准分数(standard score)也叫z分数(z-score),是一个分数与平均数的差再除以标准差的过程.用公式表示为: z=(x-μ)/σ.其中x为某一具体分数, μ为平均数,σ为标准差. Z ...

  6. (三)VLAN基本概念

  7. jQuery动态加载脚本 $.getScript();

    jQuery.getScript("/path/to/myscript.js", function(data, status, jqxhr) {       /*          ...

  8. Android软键盘弹出时把布局顶上去的解决方法

    原文: 解决Andriod软键盘出现把原来的布局给顶上去的方法(转) 链接:http://blog.sina.com.cn/s/blog_9564cb6e0101g2eb.html 决方法,在main ...

  9. 加速Eclipse使其成为超快的IDE

    按照下述步骤来加速Eclipse为超快的IDE,它适用于32和64位版本的Eclipse /JDK(OS为64位Windows 7). 1.禁用防病毒软件,或将JDK.Eclipse.workspac ...

  10. foundation系列

    1如何将布尔值转为OC对象?  1把 BOOL 值包装到 NSNumber中: NSNumber *boolNumber = [NSNumber numberWithBool:YES]  2获取BOO ...