【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 ...
随机推荐
- telnet的使用
1.要打开 telnet 不是内部或外部 命令 解决方案: 程序添加删除功能,添加即可 或法二 C:\WINDOWS\system32\telnet.exe (或用C:\WINDOWS\system3 ...
- react native web
http://rawgit.com/taobaofed/react-web/master/pages/uiexplorer.html#/scene_1?_k=7vm99j
- iOS Runtime 实践(1)
很多时候我们都在看iOS开发中的黑魔法——Runtime.懂很多,但如何实践却少有人提及.本文便是iOS Runtime的实践第一篇. WebView 我们这次的实践主题,是使用针对接口编程的方式,借 ...
- 2016 11 9遇到问题 http请求的各种方式
遇到问题:对接网易七鱼 调用他们接口是出现问题 1.对方要求 除上传文件外,其他所有接口请求Content-Type类型为:application/json;charset=utf-8:请求内容需要 ...
- C#教程之打印和打印预览
最近研究一了一下关于PDF打印和打印预览的功能,在此小小的总结记录一下学习过程. 实现打印和打印预览的方法,一般要实现如下的菜单项:打印.打印预览.页面设置. PrintDocument类 Print ...
- 自己做的demo--左连接
下面四张表是数据库中已经有的数据: 第一步: 1.left join左连接,left outer join 左外连接,只是写法不同,相同的概念. 2.左连接查出来的结果是一定包含left关键字左边的表 ...
- C#当中的多线程_任务并行库(下)
4.8 处理任务中的异常 下面这个例子讨论了任务当中抛出异常,以及任务异常的获取 class Program { static void Main(string[] a ...
- 初学HTML5系列三:事件
Window 事件属性 针对 window 对象触发的事件(应用到 <body> 标签): 属性 值 描述 onafterprint script 文档打印之后运行的脚本. onbefor ...
- Spring Security Encryption三种加密方式
Encryption One-way encryption 单项加密,客户端将要传递的值先加密(使用特定的加密方法),将原值和加密好的值传递过去,服务器端将原始数据也进行一次加密(两者加密 ...
- 从外国html5网站上扒来一个鼠标经过的css3 效果,感觉很不错
鼠标经过的时候,感觉有点像一张纸卷上去的感觉. 下面是代码 <div class="main-container types"> <div class=" ...