题意:个著名的音乐厅因为财务状况恶化快要破产,你临危受命,试图通过管理的手段来拯救它,方法之一就是优化演出安排,既聪明的决定接受或拒绝哪些乐团的演出申请,使得音乐厅的收益最大化。该音乐厅有两个完全相同的房间,因此个乐团在申请演出的时候并不会指定房间,你只需要随便分配一个即可。每个演出都会持续若干天,每个房间每天只能举行一场演出。申请数目n为不超过100的正整数,每个申请用3个整数i,j,w来表示,表示从第i天到第j天,愿意支付w元。

析:把每一天都看成是一个结点,然后相邻两天加一个容量为2,费用为0的边,然后对于每个区间可以直接从左端点到右端点+1连一条容量为1,费用为-w的边,最后从最左边跑到最右边一次最小费用流量,取反即可。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <list>
  18. #include <assert.h>
  19. #include <bitset>
  20. #include <numeric>
  21. #define debug() puts("++++")
  22. #define gcd(a, b) __gcd(a, b)
  23. #define lson l,m,rt<<1
  24. #define rson m+1,r,rt<<1|1
  25. #define fi first
  26. #define se second
  27. #define pb push_back
  28. #define sqr(x) ((x)*(x))
  29. #define ms(a,b) memset(a, b, sizeof a)
  30. #define sz size()
  31. #define pu push_up
  32. #define pd push_down
  33. #define cl clear()
  34. #define all 1,n,1
  35. #define FOR(i,x,n) for(int i = (x); i < (n); ++i)
  36. #define freopenr freopen("in.txt", "r", stdin)
  37. #define freopenw freopen("out.txt", "w", stdout)
  38. using namespace std;
  39.  
  40. typedef long long LL;
  41. typedef unsigned long long ULL;
  42. typedef pair<int, int> P;
  43. const int INF = 0x3f3f3f3f;
  44. const LL LNF = 1e17;
  45. const double inf = 1e20;
  46. const double PI = acos(-1.0);
  47. const double eps = 1e-3;
  48. const int maxn = 400 + 10;
  49. const int maxm = 3e5 + 10;
  50. const int mod = 1000000007;
  51. const int dr[] = {-1, 0, 1, 0};
  52. const int dc[] = {0, -1, 0, 1};
  53. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  54. int n, m;
  55. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  56. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  57. inline bool is_in(int r, int c) {
  58. return r >= 0 && r < n && c >= 0 && c < m;
  59. }
  60.  
  61. struct Edge{
  62. int from, to, cap, flow, cost;
  63. };
  64.  
  65. struct MinCostMaxFlow{
  66. int n, m, s, t;
  67. vector<Edge> edges;
  68. vector<int> G[maxn];
  69. int d[maxn];
  70. int p[maxn];
  71. bool inq[maxn];
  72. int a[maxn];
  73.  
  74. void init(int n){
  75. this-> n = n;
  76. for(int i = 0; i < n; ++i) G[i].cl;
  77. edges.cl;
  78. }
  79.  
  80. void addEdge(int from, int to, int cap, int cost){
  81. edges.pb((Edge){from, to, cap, 0, cost});
  82. edges.pb((Edge){to, from, 0, 0, -cost});
  83. m = edges.sz;
  84. G[from].pb(m - 2);
  85. G[to].pb(m - 1);
  86. }
  87.  
  88. bool bellman(int &flow, int &cost){
  89. ms(inq, 0); ms(d, INF); inq[s] = 1;
  90. d[s] = 0; p[s] = 0; a[s] = INF;
  91. queue<int> q; q.push(s);
  92.  
  93. while(!q.empty()){
  94. int u = q.front(); q.pop();
  95. inq[u] = 0;
  96. for(int i = 0; i < G[u].sz; ++i){
  97. Edge &e = edges[G[u][i]];
  98. if(e.cap > e.flow && d[e.to] > d[u] + e.cost){
  99. d[e.to] = d[u] + e.cost;
  100. a[e.to] = min(a[u], e.cap - e.flow);
  101. p[e.to] = G[u][i];
  102. if(!inq[e.to]){ inq[e.to] = 1; q.push(e.to); }
  103. }
  104. }
  105. }
  106. if(d[t] == INF) return false;
  107. flow += a[t];
  108. cost += d[t] * a[t];
  109. int u = t;
  110. while(u != s){
  111. edges[p[u]].flow += a[t];
  112. edges[p[u]^1].flow -= a[t];
  113. u = edges[p[u]].from;
  114. }
  115. return true;
  116. }
  117.  
  118. int mincostmaxflow(int s, int t, int &flow){
  119. this-> s = s;
  120. this-> t = t;
  121. int cost = 0;
  122. while(bellman(flow, cost));
  123. return cost;
  124. }
  125. };
  126.  
  127. MinCostMaxFlow mcmf;
  128.  
  129. int main(){
  130. while(scanf("%d", &n) == 1 && n){
  131. int s = 0, t = 366;
  132. mcmf.init(t + 5);
  133. for(int i = 0; i <= 365; ++i) mcmf.addEdge(i, i+1, 2, 0);
  134. while(n--){
  135. int u, v, c;
  136. scanf("%d %d %d", &u, &v, &c);
  137. mcmf.addEdge(u, v+1, 1, -c);
  138. }
  139. int flow = 0;
  140. printf("%d\n", -mcmf.mincostmaxflow(s, t, flow));
  141. }
  142. return 0;
  143. }

  

UVaLive 2796 Concert Hall Scheduling (最小费用流)的更多相关文章

  1. 【LA2796】Concert Hall Scheduling(最大费用最大流)

    Description You are appointed director of a famous concert hall, to save it from bankruptcy. The hal ...

  2. POJ2047 Concert Hall Scheduling(最小费用最大流)

    题目大概是有两个音乐厅,有n个乐队申请音乐厅,他们必须从第ii天到第ji天连续开音乐会且他们的开价是wi,每天每个音乐厅都只能供一个乐队进行音乐会.问接受哪些乐队的申请,获利最多能多少. 这题相当于在 ...

  3. 哈希UVALive 6326 Contest Hall Preparation

                              Encrypting passwords is one of the most important problems nowadays, and y ...

  4. UVaLive 6853 Concert Tour (DP)

    题意:给定 n 个城市,m 个月,表示要在这 n 个城市连续 m 个月开演唱会,然后给定每个月在每个城市开演唱会能获得的利润,然后就是演唱会在不同城市之间调动所要的费用, 问你,怎么安排这 n 个演唱 ...

  5. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  6. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  7. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  8. POJ动态规划题目列表

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  9. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

随机推荐

  1. python中关键字的总结

    python中各种关键字的总结:用表格形式,解释关键字符号的作用和案例说明                    关键字                                         ...

  2. hibernate自带的注解和jpa注解的冠希

    hibernate是实现了JPA规范,在我们使用hibernate框架的时候,我们引入了hibernate3或者4这个核心包.hibernate-jpa-2.0-api-1.0.0.Final.jar ...

  3. DataType 数据类型

    基本类型:四类八种:数值 : 整数:byte,short,int,long.默认是 int 小数:float,double                  默认是 double 布尔:boolean ...

  4. 线性判别分析LDA详解

    1 Linear Discriminant Analysis    相较于FLD(Fisher Linear Decriminant),LDA假设:1.样本数据服从正态分布,2.各类得协方差相等.虽然 ...

  5. Java发送HTTPS请求

    前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊, ...

  6. cmd 获取 拖拽文件名

    1. @echo off & setlocal enableDelayedExpansion set a= set /p a=Please drag your txt file for spl ...

  7. 为什么JAVA要提供 wait/notify 机制?是为了避免轮询带来的性能损失

    wait/notify  机制是为了避免轮询带来的性能损失. 为了说清道理,我们用“图书馆借书”这个经典例子来作解释. 一本书同时只能借给一个人.现在有一本书,图书馆已经把这本书借了张三. 在简单的s ...

  8. android示例:一个简单的登陆程序

    最近写了个简单的登陆程序,有几点收获: 1.懂得如何在LinearLayout中嵌套LinearLayout,完善布局的行列: 2.用android:layout_weight控制控件的比重: 3.用 ...

  9. 求含有n个因子的最小正整数(n<=1000000)

    题目链接:https://ac.nowcoder.com/acm/contest/331/G 思路: 根据唯一分解定理,如果一个数n可以表示成 n=p1a1*p2a2*...*pkak (pi是第i个 ...

  10. suse安装gcc,升级到4.8.5

    前面这些是挂载iso,如果iso可以使用,就不需要下面几步. cd /etc/zypp/repos.d mkdir iso chmod -R 777 iso mount -o loop /media/ ...