BZOJ1520 [POI2006]Szk-Schools
裸的费用流啊。。。
建图:对于一个点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的更多相关文章
- 【费用流】bzoj1520 [POI2006]Szk-Schools
注意:建图的时候,一定要把旧标号相同的分开. #include<cstdio> #include<algorithm> #include<cstring> #inc ...
- 【BZOJ1520】[POI2006]Szk-Schools KM算法
[BZOJ1520][POI2006]Szk-Schools Description Input Output 如果有可行解, 输出最小代价,否则输出NIE. Sample Input 5 1 1 2 ...
- 【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 ...
- POJ1236Network of Schools[强连通分量|缩点]
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- Network of Schools --POJ1236 Tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...
- BZOJ 1121: [POI2008]激光发射器SZK
1121: [POI2008]激光发射器SZK Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 792 Solved: 653[Submit][Sta ...
- 【BZOJ-1121】激光发射器SZK 物理 + 数学 + 乱搞
1121: [POI2008]激光发射器SZK Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 682 Solved: 565[Submit][Sta ...
- POJ 1236 Network of Schools(强连通分量/Tarjan缩点)
传送门 Description A number of schools are connected to a computer network. Agreements have been develo ...
随机推荐
- getting started with Baxter Research Robot
1. Baxter Research Robot Baxter Research Robot is from Rethink Robotics. There is also a video on TE ...
- SynchronizationContext的研究之一(非WPF及Forms)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Quick-Cocos2d-x v3.3 异步加载Spine方案 转
Quick-Cocos2d-x v3.3 异步加载Spine方案 浩月难求也与2015-03-25 15:06:3441 次阅读 背景 项目中使用了Quick-Cocos2d-x 3.3,由于Spin ...
- Spring security3入门(转)
http://kingxss.iteye.com/blog/1908011 补充类图: 注意:1.修改为链接mysql数据库: 2.代码地址:https://github.co ...
- html,CSS文字大小单位px、em、pt的关系换算
html,CSS文字大小单位px.em.pt的关系换算 这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章,题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与 ...
- JavaSE复习_1 Java的基本格式和运算符
△.代表在当前目录.classpath能在任何路径下访问类文件. △单行注释可以嵌套,多行注释不能嵌套 △java中的标识符只能有数字,字母,$和_,其他的符号都是错误的,不合法的.其中数字不能是开头 ...
- 下载安装APK
protected void downloadApk() { //apk下载链接地址,放置apk的所在路径 //1,判断sd卡是否可用,是否挂在上 if(Environment.getExternal ...
- Java 14 类型信息
14 类型信息 运行是识别对象和类的信息 两种方式RTTI 假定编译时已经知道所有的类型反射 运行时发现和使用类的信息 1 RTTI //多态 创建一个具体的对象(Circle Square Tria ...
- mustache.js渲染带事件的模板
http://zccst.iteye.com/blog/2183111 最近遇到这样一个问题,预览内容可点击,问题是通过$.Mustache.render("templateId" ...
- Spring Boot修改内置Tomcat端口号 (zhuan)
http://blog.csdn.net/argel_lj/article/details/49851625 ********************************************* ...