【POJ】【3680】Intervals
网络流/费用流
引用下题解:
lyd:
首先把区间端点离散化,设原来的数值i离散化后的标号是c[i]。这样离散化之后,整个数轴被分成了一段段小区间。
1.建立S和T,从S到离散化后的第一个点连容量K,费用0的边。离散化后的最后一个点到T连容量K、费用0的边。
2.离散化后的相邻点之间(从i到i+1)连容量为K,费用为0的边。
3.输入的区间从离散化后的左端点到右端点连容量1、费用W的边。
感觉好神啊……
其实应该只要把源点到第一个点的流量限制为k应该就可以了……这个构思蛮巧妙的……限制了每个地方最多有k个流出去。
另外,这题是最大费用最大流,所以按上面的建图方式需要改一下预处理和增广的条件,或者就是建图的时候所有的费用取负,再将最后答案取负。
Source Code
Problem: User: sdfzyhy
Memory: 752K Time: 563MS
Language: G++ Result: Accepted Source Code //BZOJ 3680
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
#define CC(a,b) memset(a,b,sizeof(a))
using namespace std;
int getint(){
int v=,sign=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') sign=-; ch=getchar();}
while(isdigit(ch)) {v=v*+ch-''; ch=getchar();}
return v*sign;
}
const int N=,M=,INF=~0u>>;
const double eps=1e-;
/*******************template********************/
int n,m,k,ans,a[N],b[N],c[N],w[N];
struct edge{int from,to,v,c;};
struct Net{
edge E[M];
int head[N],next[M],cnt;
void ins(int x,int y,int z,int c){
E[++cnt]=(edge){x,y,z,c};
next[cnt]=head[x]; head[x]=cnt;
}
void add(int x,int y,int z,int c){
ins(x,y,z,c); ins(y,x,,-c);
}
int S,T,d[N],Q[M],from[N];
bool inq[N];
bool spfa(){
int l=,r=-;
F(i,S,T) d[i]=INF;
d[S]=; Q[++r]=S; inq[S]=;
while(l<=r){
int x=Q[l++]; inq[x]=;
for(int i=head[x];i;i=next[i])
if(E[i].v && d[x]+E[i].c<d[E[i].to]){
d[E[i].to]=d[x]+E[i].c;
from[E[i].to]=i;
if (!inq[E[i].to]){
Q[++r]=E[i].to;
inq[E[i].to]=;
}
}
}
return d[T]!=INF;
}
void mcf(){
int x=INF;
for(int i=from[T];i;i=from[E[i].from])
x=min(x,E[i].v);
for(int i=from[T];i;i=from[E[i].from]){
E[i].v-=x;
E[i^].v+=x;
}
ans+=x*d[T];
}
void init(){
n=getint(); k=getint();
cnt=; ans=;
memset(head,,sizeof head);
int x,y;
F(i,,n){
a[i]=c[(i<<)-]=getint();
b[i]=c[i<<]=getint();
w[i]=getint();
}
sort(c+,c+n*+);
int num=unique(c+,c+n*+)-c-;
S=; T=num+;
F(i,,num) add(i,i+,k,);
F(i,,n){
a[i]=lower_bound(c+,c+num+,a[i])-c;
b[i]=lower_bound(c+,c+num+,b[i])-c;
add(a[i],b[i],,-w[i]);
}
while(spfa()) mcf();
printf("%d\n",-ans);
}
}G1;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
int T=getint();
while(T--) G1.init();
return ;
}
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 6880 | Accepted: 2859 |
Description
You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task is to pick some of the intervals to maximize the total weights under the limit that no point in the real axis is covered more than k times.
Input
The first line of input is the number of test case.
The first line of each test case contains two integers, N and K (1 ≤ K ≤ N ≤ 200).
The next N line each contain three integers ai, bi, wi(1 ≤ ai < bi ≤ 100,000, 1 ≤ wi ≤ 100,000) describing the intervals.
There is a blank line before each test case.
Output
For each test case output the maximum total weights in a separate line.
Sample Input
4 3 1
1 2 2
2 3 4
3 4 8 3 1
1 3 2
2 3 4
3 4 8 3 1
1 100000 100000
1 2 3
100 200 300 3 2
1 100000 100000
1 150 301
100 200 300
Sample Output
14
12
100000
100301
Source
[Submit] [Go Back] [Status] [Discuss]
【POJ】【3680】Intervals的更多相关文章
- 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...
- 【POJ 1459 power network】
不可以理解的是,测评站上的0ms是怎么搞出来的. 这一题在建立超级源点和超级汇点后就变得温和可爱了.其实它本身就温和可爱.对比了能够找到的题解: (1)艾德蒙·卡普算法(2)迪尼克算法(3)改进版艾德 ...
- 【POJ 2728 Desert King】
Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...
- 【POJ 2976 Dropping tests】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 13849Accepted: 4851 Description In a certa ...
- 【POJ 3080 Blue Jeans】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...
- 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)
1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- 【POJ 2823 Sliding Window】 单调队列
题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...
- 【POJ 2406 Power Strings】
Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be the ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
随机推荐
- phpcms v9 源码解析-1 index.php
这个是phpcms V9 的入口文件index.php. V9程序的执行绝大多数是从这个文件开始的,但不绝对,在项目下面的api.php和plugin.php是另外的入口文件,这里我们先不做深究. 在 ...
- 添加TextView隐藏进度条的方法
在TextView中添加 android:scrollbars="vertical" android:singleLine="false" 在Activity代 ...
- SIMATIC IT HISTORIAN在烟用二醋酸纤维素生产中应用
原文转载自:http://www.soft6.com/tech/5/54287.html 本文介绍了西门子MES核心产品SIMATIC IT HISTORIAN实时数据库及客户端工具在流程生产中的具体 ...
- 第十一章 管理类型(In .net4.5) 之 管理对象的生命周期
1. 概述 本章内容包括 管理非托管资源.使用IDisposable接口 以及 管理析构器和垃圾回收. 2. 主要内容 2.1 理解垃圾回收机制 ① 代码执行的时候,内存中有两个地方存放数据项:堆 和 ...
- Python学习教程(learning Python)--2.3 Python自定义函数传参函数设计
Python里自定义子函数时,可以在调用时携带一些参数到子函数里去处理.具体用法结构如下: def func(arguments): statement statement etc. 定义子函数一定要 ...
- C 简易基础开发框架 - simple c
引言 一个为 简单高效而生的 简易跨平台的 纯C开发框架. githup上源码 https://github.com/wangzhione/sconsole_project 请容我细说 s ...
- shell括号操作符
以下以bash环境下做解说 一.单小括号() 二.双小括号(()) 可作数值条件操作,也可作数值运算使用(近似于 let 命令) 如 C 语言语法一样,支持运算符:<<.<<= ...
- 配置php5.6的运行环境
所需要的原材料:(提供链接) php-5.6.10-Win32-VC11-x86 (zip)(注意php版本分为了IIS版和Apache版) httpd-2.4.12-x86-r2(apache) ( ...
- PAT乙级真题1001. 害死人不偿命的(3n+1)猜想 (15)(解题)
卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在1950年的世界数 ...
- js-提前声明和new操作符理解
1.提前声明:声明变量后,js会把声明部分提前到作用域前面. var a=1; function aheadOfStatement(){ alert(a); var a=2; } 这段代码结果是und ...