bzoj4276
线段树优化建图+费用流
朴素的做法是每个强盗直接对每个区间的每个点连边,然后跑最大权匹配,这样有5000*5000条边,肯定过不去,那么我们用线段树优化一下,因为线段树能把一个O(n)的区间划分为O(logn)段
然后就建一棵线段树,每个节点向两个儿子连(inf,0)的边,叶子结点连向sink,(1,0),每个强盗向对应区间节点连边,这样边数就将为了nlogn条。据说正解是贪心?
抄了个板子
#include<bits/stdc++.h>
using namespace std;
const int N = , inf = 0x3f3f3f3f;
struct edge {
int nxt, to, f, c;
} e[N * ];
int n, m, k, source, sink, tot, cnt = , sum;
int head[N], pree[N], prev[N], vis[N], d[N];
inline void link(int u, int v, int f, int c)
{
e[++cnt].nxt = head[u];
head[u] = cnt;
e[cnt].f = f;
e[cnt].to = v;
e[cnt].c = c;
}
inline void insert(int u, int v, int f, int c)
{
link(u, v, f, c);
link(v, u, , -c);
}
bool spfa()
{
memset(d, -, sizeof(d));
d[source] = ;
queue<int> q;
q.push(source);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for(int i = head[u]; i; i = e[i].nxt) if(e[i].f && (d[e[i].to] < d[u] + e[i].c || d[e[i].to] == -))
{
pree[e[i].to] = i;
prev[e[i].to] = u;
d[e[i].to] = d[u] + e[i].c;
if(vis[e[i].to] == )
{
q.push(e[i].to);
vis[e[i].to] = ;
}
}
}
return d[sink] != -;
}
inline int Edmonds_Karp()
{
int ans = ;
while(spfa())
{
int now = sink, delta = inf;
while(now != source)
{
delta = min(delta, e[pree[now]].f);
now = prev[now];
}
now = sink;
while(now != source)
{
e[pree[now]].f -= delta;
e[pree[now] ^ ].f += delta;
now = prev[now];
}
ans += delta * d[sink];
}
return ans;
}
void build(int l, int r, int x)
{
if(l == r)
{
insert(x, sink, , );
return;
}
int mid = (l + r) >> ;
build(l, mid, x << );
build(mid + , r, x << | );
insert(x, x << , inf, );
insert(x, x << | , inf, );
}
void update(int l, int r, int x, int a, int b, int c, int pos)
{
if(l > b || r < a) return;
if(l >= a && r <= b)
{
insert(pos, x, , c);
return;
}
int mid = (l + r) >> ;
update(l, mid, x << , a, b, c, pos);
update(mid + , r, x << | , a, b, c, pos);
}
int main()
{
scanf("%d", &n);
sink = + n + ;
build(, , );
for(int i = ; i <= n; ++i)
{
int l, r, c;
scanf("%d%d%d", &l, &r, &c);
insert(source, i + , , );
update(, , , l, r - , c, i + );
}
printf("%d\n", Edmonds_Karp());
return ;
}
bzoj4276的更多相关文章
- 【BZOJ4276】[ONTAK2015]Bajtman i Okrągły Robin 线段树优化建图+费用流
[BZOJ4276][ONTAK2015]Bajtman i Okrągły Robin Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1],[a[i]+1,a[i]+2 ...
- BZOJ4276 : [ONTAK2015]Bajtman i Okrągły Robin
建立线段树, S向每个叶子连边,容量1,费用0. 孩子向父亲连边,容量inf,费用0. 每个强盗向T连边,容量1,费用为c[i]. 对应区间内的点向每个强盗,容量1,费用0. 求最大费用流即可. #i ...
- 最小/大费用最大流模板(codevs1914)
void addedge(int fr,int to,int cap,int cos){ sid[cnt].fr=fr;sid[cnt].des=to;sid[cnt].cap=cap;sid[cnt ...
- BZOJ.2034.[2009国家集训队]最大收益(二分图匹配 贪心)
题目链接 双倍经验:BZOJ.4276.[ONTAK2015]Bajtman i Okrągły Robin(然而是个权限题.区间略有不同) \(Description\) 有\(n\)个任务,完成一 ...
随机推荐
- Codeforces Round #277 (Div. 2 Only)
A:SwapSort http://codeforces.com/problemset/problem/489/A 题目大意:将一个序列排序,可以交换任意两个数字,但要求交换的次数不超过n,输出任意一 ...
- iOS时钟,秒针扫秒样式
昨天做一个时钟小demo,发现了一些问题. 描述能力有限,我封装好了一个时钟框架,朋友们可以参考 https://github.com/qianlishun/ClockView 点击这里可以 ...
- SQL SERVER 2012 第五章 创建和修改数据表 の SQL SERVER中的对象名
[ServerName.[DataBaseName.[SchemeName.]]]ObjectName 服务器名,数据库名,模式名,对象名 其中模式是一个新出的坑爹的东西.
- Dividing--hdu1059(动态规划)
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- P2384 最短路 洛谷
https://www.luogu.org/problem/show?pid=2384 题目背景 狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗? 他会 ...
- Extjs.panel.Panel赋值的问题
初学extjs,很是不爽.也是只有初学者才犯的错误,发出来以免再犯. 先创建一个panel var panel1 = Ext.create('Ext.panel.Panel', { id: 'p1', ...
- SQL根据某一父节点查询所有子节点,无限
;with cte as( select id,ParentCategoryId from Category where id = 17 union all select a.id,a.ParentC ...
- 扫描仪共享工具(BlindScanner Pro) 3.23 特别版
http://www.xdowns.com/soft/1/126/2014/Soft_125206.html
- Use JavaScript to Export Your Data as CSV
原文: http://halistechnology.com/2015/05/28/use-javascript-to-export-your-data-as-csv/ --------------- ...
- 【机器学习具体解释】SVM解二分类,多分类,及后验概率输出
转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/51073885 CSDN−勿在浮沙筑高台 支持向量机(Support Vecto ...