【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 ...
随机推荐
- java8新特性笔记
1.forEach(),遍历数据结构中的元素,括号内可以带一个闭包的方法 2.双冒号用法:forEach(this::doSchedule),如果运行环境是闭包,java允许使用双冒号的写法来直接调用 ...
- OJ推荐【转】
来自:http://blog.csdn.net/zdp072/article/details/16207111 一. Online Judge简介: Online Judge系统(简称OJ)是一个在线 ...
- OVER Clause是个好东西,常和ROW_NUMBER()、Sum、AVG、Count、Min、Max配合使用
根据SQL官方帮助的实例: USE AdventureWorks2012; GO SELECT ROW_NUMBER() OVER(PARTITION BY PostalCode ORDER BY S ...
- 在EF的code frist下写稳健的权限管理系统:界面设计(四)
基本都是采用pure设计(中文官网:http://purecss.org,英文官网:http://purecss.io).pure只是一个简单强大的cssUI库,支持响应式设计,适合自己设计或者给美工 ...
- python爬取糗百第一页的笑话
自学python网络爬虫,发现request比urllib还是要好用一些,因此利用request和BeautifulSoup来实现糗百的首页笑话的抓取.BeautifulSoup通过find和find ...
- hdu 1228 A + B
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1228 A + B Description 读入两个小于100的正整数A和B,计算A+B.需要注意的是: ...
- MVC4.0网站发布和部署到IIS7.0上的方法【转:http://www.th7.cn/Program/net/201403/183756.shtml】
最近在研究MVC4,使用vs2010,开发的站点在发布和部署到iis7上的过程中遇到了很多问题,现在将解决的过程记录下来,以便日后参考,整个过程主要以截图形式呈现 vs2010的安装和mvc4的安装不 ...
- linux下安装protobuf教程+示例(详细)
(.pb.h:9:42: fatal error: google/protobuf/stubs/common.h: No such file or directory 看这个就应该知道是没有找到头文件 ...
- 推荐:一个个人开发者搞app赚钱之后的总结!有图有真相。
2011年已经过去了,回顾2011有收获,更有许多不足.收获就是了却了一件人生大事(女儿出生),还有就是算入门了android并利用它开发 了一 款还算有些许收获的应用.不足的地方是单位工作上没有太好 ...
- [转]论window和Linux之长短
论window和Linux之长短 王垠 http://www.kerneltravel.net/jiqiao/whyLinux.htm — 摈弃 Windows 低效率的工作方式,发掘 Linux 身 ...