【LA2796】Concert Hall Scheduling(最大费用最大流)
Description
You are appointed director of a famous concert hall, to save it from bankruptcy. The hall is very
popular, and receives many requests to use its two fine rooms, but unfortunately the previous director
was not very efficient, and it has been losing money for many years. The two rooms are of the same
size and arrangement. Therefore, each applicant wishing to hold a concert asks for a room without
specifying which. Each room can be used for only one concert per day.
In order to make more money, you have decided to abandon the previous fixed price policy, and rather
let applicants specify the price they are ready to pay. Each application shall specify a period [i, j] and
an asking price w, where i and j are respectively the first and last days of the period (1 ≤ i ≤ j ≤ 365),
and w is a positive integer in yen, indicating the amount the applicant is willing to pay to use a room
for the whole period.
You have received applications for the next year, and you should now choose the applications you
will accept. Each application should be either accepted for its whole period or completely rejected.
Each concert should use the same room during the whole applied period.
Considering the dire economic situation of the concert hall, artistic quality is to be ignored, and
you should just try to maximize the total income for the whole year by accepting the most profitable
applications.Input
The input has multiple data sets, each starting with a line consisting of a single integer n, the number
of applications in the data set. Then, it is followed by n lines, each of which represents one application
with a period [i, j] and an asking price w yen in the following format.
i j w
A line containing a single zero indicates the end of the input.
The maximum number of applications in a data set is one thousand, and the maximum asking price
is one million yen.Output
For each data set, print a single line containing an integer, the maximum total income in yen for the
data set.Sample Input
4
1 2 10
2 3 10
3 3 10
1 3 10
6
1 20 1000
3 25 10000
5 15 5000
22 300 5500
10 295 9000
7 7 6000
8
32 251 2261
123 281 1339
211 235 5641
162 217 7273
22 139 7851
194 198 9190
119 274 878
122 173 8640
0Sample Output
30
25500
38595
【题意】
你有2个房间,有365天,有n个人找你租房,第i个人要从第xi到第yi天要一个房(任意一个),付wi的钱,求怎样安排收的钱最多
【分析】
1~365天建一个点,st连1流量为2费用为0。
根据输入的继续连边,比如第i个人要从第xi到第yi天要一个房(任意一个),付wi的钱那么建一条xi->yi+1流量为1,费用为wi的边。
最后求最大费用最大流(去负跑最小费用最大流即可)。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxm 1001000
#define Maxn 400
#define INF 0xfffffff int n; struct node
{
int x,y,f,c,o,next;
}t[Maxm];int len; int first[Maxn],pre[Maxn],dis[Maxn],flow[Maxn];
bool inq[Maxn]; int mymin(int x,int y) {return x<y?x:y;}
int mymax(int x,int y) {return x>y?x:y;} void ins(int x,int y,int f,int c)
{
t[++len].x=x;t[len].y=y;t[len].f=f;t[len].c=c;
t[len].next=first[x];first[x]=len;t[len].o=len+;
t[++len].x=y;t[len].y=x;t[len].f=;t[len].c=-c;
t[len].next=first[y];first[y]=len;t[len].o=len-;
} queue<int > q;
bool bfs(int st,int ed)
{
while(!q.empty()) q.pop();
memset(dis,,sizeof(dis));
memset(inq,,sizeof(inq));
memset(pre,,sizeof(pre));
q.push(st);inq[st]=;dis[st]=;flow[st]=INF;
while(!q.empty())
{
int x=q.front();
for(int i=first[x];i;i=t[i].next) if(t[i].f>)
{
int y=t[i].y; if(dis[y]>dis[x]+t[i].c)
{
dis[y]=dis[x]+t[i].c;
pre[y]=i;
flow[y]=mymin(flow[x],t[i].f);
if(!inq[y]) {q.push(y);inq[y]=;}
}
}
q.pop();inq[x]=;
}
if(pre[ed]) return flow[ed];
return ;
} void max_flow(int x,int y)
{
int sum=,a;
while(a=bfs(x,y))
{
int now=y;
sum+=a*dis[y];
while(now!=x)
{
t[pre[now]].f-=a;
t[t[pre[now]].o].f+=a;
now=t[pre[now]].x;
}
}
printf("%d\n",-sum);
} int main()
{
while()
{
scanf("%d",&n);
if(n==) break;
memset(first,,sizeof(first));
len=;int mx=;
for(int i=;i<=n;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y+,,-c);
mx=mymax(mx,y+);
}
int st=,ed=mx;
ins(st,,,);
for(int i=;i<mx;i++) ins(i,i+,,);
max_flow(st,ed);
}
return ;
}
[LA3796]
2016-05-25 13:27:56
【LA2796】Concert Hall Scheduling(最大费用最大流)的更多相关文章
- POJ2047 Concert Hall Scheduling(最小费用最大流)
题目大概是有两个音乐厅,有n个乐队申请音乐厅,他们必须从第ii天到第ji天连续开音乐会且他们的开价是wi,每天每个音乐厅都只能供一个乐队进行音乐会.问接受哪些乐队的申请,获利最多能多少. 这题相当于在 ...
- UVaLive 2796 Concert Hall Scheduling (最小费用流)
题意:个著名的音乐厅因为财务状况恶化快要破产,你临危受命,试图通过管理的手段来拯救它,方法之一就是优化演出安排,既聪明的决定接受或拒绝哪些乐团的演出申请,使得音乐厅的收益最大化.该音乐厅有两个完全相同 ...
- [板子]最小费用最大流(Dijkstra增广)
最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...
- bzoj1927最小费用最大流
其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→ =_=你TM逗我 刚要删突然感觉dinic的模 ...
- 【Codeforces717G】Underfail Hash + 最大费用最大流
G. Underfail time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...
- ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)
将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- P3381 【模板】最小费用最大流
P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
- 最小/大费用最大流模板(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 ...
随机推荐
- javascript、jsp
1.javascript:简称js 在<body>与 </body>之间 加入<script> </script>即可 最好在<head>与 ...
- WebService学习笔记系列(二)
soap(简单对象访问协议),它是在http基础之上传递xml格式数据的协议.soap协议分为两个版本,soap1.1和soap1.2. 在学习webservice时我们有一个必备工具叫做tcpmon ...
- HTTP 错误 500.19- Internal Server Error 错误解决方法 分类: Windows服务器配置 2015-01-08 20:16 131人阅读 评论(0) 收藏
1.第一种情况如下: 解决方法如下: 经过检查发现是由于先安装Framework组件,后安装iis的缘故,只需重新注册下Framework就可以了,具体步骤如下 1 打开运行,输入cmd进入到命令提示 ...
- Android常用错误解决汇总
一.No active compatible AVD's or devices found. Relaunch this configuration after connecting a device ...
- Android NDK学习总结
一.android NDK编程步骤 java文件中声明native方法. android工程根目录新建jni文件夹. 调用javah命令为第一步声明的native方法生成相应的.h头文件. 通过win ...
- jsp - 引用 jar包.
在jsp中使用不同的方式引用jar,准备的工作也不同.我接触过的有两种:1)直接在jsp页面中引用;2)在src下的java类中引用,然后在jsp中调用java类. 1)直接引用:可以将jar包丢到W ...
- RABBITMQ安装注意点
关于 RABBITMQ的配置问题安装问题windows7 和window 10我都试了windows10安装和配置不要出现中文和空格,不然你日寒飞的心都有了ERLANG的安装也是Win7直接默认的路径 ...
- 1.RABBITMQ 入门 - WINDOWS - 获取,安装,配置
一. 背景: 公司项目有所改动,要求微信(移动端调用的接口),日志接口换位log4net,全部改成以rabbitMQ作为服务支持, 二.本地环境: windows 10 enterpr ...
- Javascript实现图片库效果
思路: 无序列表加载图片文件.用img标签加载一张图片作为占位符.当点击照片链接时,改变<a>元素的href属性.并且阻止浏览器的默认行为. 动态改变描述文字,在图片下方增加P标签.通过获 ...
- java_设计模式_迭代器模式_Iterator Pattern(2016-08-12)
迭代子(Iterator)模式又叫游标(Cursor)模式,是对象的行为模式. 定义:提供一种方法访问一个容器对象中各个元素,而又不暴露该对象的内部细节. 类型:行为类模式 类图: 如果要问java中 ...