【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 ...
随机推荐
- Android 自定义View修炼-Android开发之自定义View开发及实例详解
在开发Android应用的过程中,难免需要自定义View,其实自定义View不难,只要了解原理,实现起来就没有那么难. 其主要原理就是继承View,重写构造方法.onDraw,(onMeasure)等 ...
- JDK1.8 Lambda
1.模拟Model /** * Author:JsonLu * DateTime:16/12/8 14:01 * Email:jsonlu@qq.com * Desc: */ public class ...
- H TML5 之 (1) 初识HTML5
新特性 HTML5 中的一些有趣的新特性: 用于绘画的 canvas 元素 用于媒介回放的 video 和 audio 元素 对本地离线存储的更好的支持 新的特殊内容元素,比如 article.foo ...
- SqlSugar简单工模式数据访问简单Demo
源代码地址:http://git.oschina.net/tiama3798/BootstrapBack_Demo 1.Model层 2.抽象层实例: 基础接口 /// <summary> ...
- C# 实现的多线程异步Socket数据包接收器框架
转载自Csdn : http://blog.csdn.net/jubao_liang/article/details/4005438 几天前在博问中看到一个C# Socket问题,就想到笔者2004年 ...
- TP-LINK wr703n openwrt 挂载 U盘
1.首先设置好DNS 2.点SYSTEM 点SOFTWARE 更新软件列表 3.安装下列软件: block-mount kmod-usb-storage kmod-fs-ext4 e2fsprogs ...
- error MSB6006: “CL.exe”已退出
解决方案之一: 删除 \Windows\System32 目录下 mspdb110.dll. 试试吧.
- wireshark添加用户执行
默认情况下,访问网络端口需要root权限,而wireshark的只是/usr/share/dumpcap的一个UI,/usr/share/dumpcap需要root权限,所以没法non-root用户无 ...
- String,StringBuffer,StringBuilder的简单比较
原文:http://blog.csdn.net/rmn190/article/details/1492013 String 字符串常量StringBuffer 字符串变量(线程安全)StringB ...
- power designer中查看表sql去掉字段属性双引号设置
把Database → Edit Current DBMS → General → Script → Sql → Format → CaseSensitivityUsingQuote设置为NO