【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 ...
随机推荐
- codeblocks创建和使用静态库(C语言)
静态库 (扩展名为 .a 或 .lib) 是包含函数的文件,用于在link阶段整合执行程序,动态链接库(扩展名 .dll)是不在link阶段整合进执行程序中的. DLL文件在执行阶段动态调用 下面 ...
- 安装Oracle数据库和PLSQL连接数据库
首先在Oracle官网上下载: 安装前要注意:将win64_11gR2_database_2of2中的\win64_11gR2_database_2of2\database\stage\Compone ...
- Java-Android 之出滚动条和卷轴页面
<?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:andr ...
- 微信公众平台开发(一)——接入指南(asp.net)
第一步:申请消息接口 在公众平台网站的高级功能 – 开发模式页,点击“成为开发者”按钮,填写URL和Token,其中URL是开发者用来接收微信服务器数据的接口URL.Token可由开发者任意填写,用作 ...
- C#当中的多线程_线程同步
第2章 线程同步 原来以为线程同步就是lock,monitor等呢,看了第二章真是大开眼界啊! 第一章中我们遇到了一个叫做竞争条件的问题.引起的原因是没有进行正确的线程同步.当一个线程在执行操作时候, ...
- BigDecimal类型的详情
一.简介 1.概述 BigDecimal由任意精度的整数非标度值和32位的整数标度(scale)组成.如果为零或正数,则标度是小数点后的位数.如果为负数,则将该数的非标度值乘以10的负scale次幂. ...
- CI 自动操作日志
在控制器中,继承一个总控制器,MY_Controller,让其他集成的控制器,继承my控制器 在MY_Controller控制器中,重写构造方法, 代码如下,测试pass! class MY_Cont ...
- Maven3(笔记一)
第一节:Maven 简介 百度百科:Maven 官网:http://maven.apache.org/ 第二节:Maven 安装与配置 Maven 下载:http://maven.apache.org ...
- autoreleasepool的笔记
1.autoreleasepool总是会被问到,放在自动释放池中的对象合适被释放?理解不正确的答案:{}出了大括号.出了作用域等等.个人认为参考答案是,1.在不是手动添加的AutoreleasePoo ...
- xmlns:tools="http://schemas.android.com/tools"以及tools:context=".ConfActivity"是什么意思
xmlns:tools="http://schemas.android.com/tools"这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么 ...