Problem L.Videos

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 455    Accepted Submission(s): 222

Problem Description
C-bacteria takes charge of two kinds of videos: ’The Collection of Silly Games’ and ’The Collection of Horrible Games’.
For simplicity’s sake, they will be called as videoA and videoB.
There are some people who want to watch videos during today, and they will be happy after watching videos of C-bacteria.
There are n hours a day, m videos are going to be show, and the number of people is K.
Every video has a type(videoA or videoB), a running time, and the degree of happi- ness after someone watching whole of it.
People can watch videos continuous(If one video is running on 2pm to 3pm and another is 3pm to 5pm, people can watch both of them).
But each video only allows one person for watching.
For a single person, it’s better to watch two kinds to videos alternately, or he will lose W happiness.
For example, if the order of video is ’videoA, videoB, videoA, videoB, …’ or ’B, A, B, A, B, …’, he won’t lose happiness; But if the order of video is ’A, B, B, B, A, B, A, A’, he will lose 3W happiness.
Now you have to help people to maximization the sum of the degree of happiness.
 
Input
Multiple query.
On the first line, there is a positive integer T, which describe the number of data. Next there are T groups of data.
for each group, the first line have four positive integers n, m, K, W : n hours a day, m videos, K people, lose W happiness when watching same videos).
and then, the next m line will describe m videos, four positive integers each line S, T, w, op : video is the begin at S and end at T, the happiness that people can get is w, and op describe it’s tpye(op=0 for videoA and op=1 for videoB).
There is a blank line before each groups of data.
T<=20, n<=200, m<=200, K<=200, W<=20, 1<=S<T<=n, W<=w<=1000,
op=0 or op=1
 
Output
Your output should include T lines, for each line, output the maximum happiness for the corresponding datum.
 
Sample Input
  1.  
Sample Output
2000
1990

解析   很容易看出来 这是一道费用流的题  首先数量级很小 然后很多个电影 每个电影只能看一遍   可以得到一个快乐值 (容量,费用)

   关键有k个人,按照一个人一个人来贪心,策略貌似不对,所以试一下网络流。

建图

  1 把每个电影拆成俩个点v,v' 有向边 v->v' 的边权为观看的该电影的快乐值的相反数  容量为1(只能观看一次)

  2 然后源点s到次源点s'连一条有向边s->s' 权值为0  容量为 k

  3  次源点s' 到每个电影的 v 点都连一条s'->v的有向边 权值为0  容量为1

  4  每个电影的v'点到汇点 t 连一条有向边 v'-> t   权值为0 容量为1

  5 电影与电影之间 根据开始 结束时间是否相交,判断是否可以跳转观看。若可以,连一条有向边 u'->v ,再判断两个电影类型是否相同,相同权值为w,不相同权值为0,容量为1

然后跑一边最小费用最大流  答案就是费用取反

AC代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define mp make_pair
  5. #define X first
  6. #define Y second
  7. #define all(a) (a).begin(), (a).end()
  8. #define fillchar(a, x) memset(a, x, sizeof(a))
  9. #define huan printf("\n");
  10. #define debug(a,b) cout<<a<<" "<<b<<" ";
  11. const int maxn=1e3+,mod=1e9+,inf=0x3f3f3f3f;
  12. typedef long long ll;
  13. struct MCMF {
  14. struct Edge {
  15. int from, to, cap, cost;
  16. Edge(int u, int v, int w, int c): from(u), to(v), cap(w), cost(c) {}
  17. };
  18. int n, s, t;
  19. vector<Edge> edges;
  20. vector<int> G[maxn];
  21. int inq[maxn], d[maxn], p[maxn], a[maxn];
  22.  
  23. void init(int n) {
  24. this->n = n;
  25. for (int i = ; i <= n; i ++) G[i].clear();
  26. edges.clear();
  27. }
  28. void addedge(int from, int to, int cap, int cost) {
  29. edges.push_back(Edge(from, to, cap, cost));
  30. edges.push_back(Edge(to, from, , -cost));
  31. int m = edges.size();
  32. G[from].push_back(m - );
  33. G[to].push_back(m - );
  34. }
  35. bool BellmanFord(int s, int t, int &flow, int &cost) {
  36. for (int i = ; i <= n; i ++) d[i] = inf;
  37. memset(inq, , sizeof(inq));
  38. d[s] = ; inq[s] = ; p[s] = ; a[s] = inf;
  39.  
  40. queue<int> Q;
  41. Q.push(s);
  42. while (!Q.empty()) {
  43. int u = Q.front(); Q.pop();
  44. inq[u] = ;
  45. for (int i = ; i < G[u].size(); i ++) {
  46. Edge &e = edges[G[u][i]];
  47. if (e.cap && d[e.to] > d[u] + e.cost) {
  48. d[e.to] = d[u] + e.cost;
  49. p[e.to] = G[u][i];
  50. a[e.to] = min(a[u], e.cap);
  51. if (!inq[e.to]) {
  52. Q.push(e.to);
  53. inq[e.to] = ;
  54. }
  55. }
  56. }
  57. }
  58. if (d[t] == inf) return false;
  59. flow += a[t];
  60. cost += d[t] * a[t];
  61. int u = t;
  62. while (u != s) {
  63. edges[p[u]].cap -= a[t];
  64. edges[p[u] ^ ].cap += a[t];
  65. u = edges[p[u]].from;
  66. }
  67. return true;
  68. }
  69. int solve(int s, int t) {
  70. int flow = , cost = ;
  71. while (BellmanFord(s, t, flow, cost));
  72. return cost;
  73. }
  74. }solver;
  75. struct node
  76. {
  77. int l,r,w,type,id;
  78. }a[maxn];
  79. void build(int n,int m,int k,int w)
  80. {
  81. solver.init(*m+);
  82. for(int i=;i<m;i++)
  83. {
  84. solver.addedge(a[i].id,a[i].id^,,-a[i].w);
  85. solver.addedge(*m+,a[i].id,,);
  86. solver.addedge(a[i].id^,*m+,,);
  87. }
  88. for(int i=;i<m;i++)
  89. {
  90. for(int j=i+;j<m;j++)
  91. {
  92. if(a[i].r<=a[j].l)
  93. {
  94. if(a[i].type==a[j].type)
  95. solver.addedge(a[i].id^,a[j].id,,w);
  96. else
  97. solver.addedge(a[i].id^,a[j].id,,);
  98. }
  99. else if(a[i].l>=a[j].r)
  100. {
  101. if(a[i].type==a[j].type)
  102. solver.addedge(a[j].id^,a[i].id,,w);
  103. else
  104. solver.addedge(a[j].id^,a[i].id,,);
  105. }
  106. }
  107. }
  108. solver.addedge(*m,*m+,k,);
  109. }
  110. int main()
  111. {
  112. int n,m,t,w,k;
  113. scanf("%d",&t);
  114. while(t--)
  115. {
  116. scanf("%d%d%d%d",&n,&m,&k,&w);
  117. for(int i=;i<m;i++)
  118. {
  119. scanf("%d%d%d%d",&a[i].l,&a[i].r,&a[i].w,&a[i].type);
  120. a[i].id=i*;
  121. }
  122. build(n,m,k,w);
  123. int maxflow; // 汇点 2m+2 源点 2m 次源点2m+1
  124. maxflow=solver.solve(*m,*m+);
  125. printf("%d\n",-maxflow);
  126. }
  127. return ;
  128. }

HDU 6437 最(大) 小费用最大流的更多相关文章

  1. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  2. HDU 5988.Coding Contest 最小费用最大流

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  3. hdu 3667(拆边+最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...

  4. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  5. hdu 2686 Matrix && hdu 3367 Matrix Again (最大费用最大流)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))

    Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. POJ-2135 Farm Tour---最小费用最大流模板题(构图)

    题目链接: https://vjudge.net/problem/POJ-2135 题目大意: 主人公要从1号走到第N号点,再重N号点走回1号点,同时每条路只能走一次. 这是一个无向图.输入数据第一行 ...

  8. hdu 1533 Going Home 最小费用最大流 入门题

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. HDU–5988-Coding Contest(最小费用最大流变形)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. Zabbix使用外部命令fping处理ICMP ping的请求

    Zabbix使用外部命令fping处理ICMP ping的请求,fping不包含在zabbix的发行版本中,需要额外去下载安装fping程序, 安装完毕之后需要在zabinx_server.conf中 ...

  2. 原创:E325: ATTENTION vim超完整超给力的问题与解决方法

    又到了老葵花哥哥开课的时间 这是给大家提供一个企业常见的错误 我相信大家生活还编程中会长期使用接触这个错误 这里我们经常用的两个选项 (E)dit any way 编辑原来的文件,忽略刚刚做的修改 ( ...

  3. pylint安装失败的解决方法

    原文链接http://www.cnblogs.com/Loonger/p/7815335.html 使用命令pip3 install pylint安装pylint是出现错误.查了一圈也找不到答案.仔细 ...

  4. FastDFS和集中存储方式对比

    指标 FastDFS   NFS  集中存储设备如NetApp.NAS 线性扩容性  高  差  差 文件高并发访问性能 高 差  一般 文件访问方式 专有API POSIX  支持POSIX 硬件成 ...

  5. POJ-2442-Sequence(二叉堆)

    POJ-2442 Description Given m sequences, each contains n non-negative integer. Now we may select one ...

  6. 7. ENGINES

    7. ENGINES ENGINES表提供有关存储引擎的信息. 这对于检查是否支持存储引擎或查看默认引擎是什么特别有用. INFORMATION_SCHEMA Name SHOW Name ENGIN ...

  7. kvm中内存过载使用

    与CPU过载使用类似,在KVM中内存也是允许过载使用(over commit)的,KVM能够让分配给客户机的内存总数大于实际可用的物理内存总数. 由于客户机操作系统及其上的应用程序并非一直100%地利 ...

  8. iPhone模拟定位(非越狱修改手机定位)

    剩下的事情就是build一下到手机,那么,就可以看到神奇的效果!   本次带来一个简单又好玩的实用功能,比如定位装逼(共享定位非分享可选那种),又或者定位打卡之类,由于改变的是设备级别的定位,本设备所 ...

  9. Chrome浏览器 v68.0.3440.106 正式版怎么样?

    谷歌浏览器Google Chrome稳定版迎来v68正式版第三个维护版本发布,详细版本号为v68.0.3440.106,上一个正式版v68.0.3440.84发布于8月1日,时隔8天Google又发布 ...

  10. 搭建Mysql主从复制

    mysql 主从复制流程图 Server version: 10.0.24-MariaDB-7 Ubuntu 16.04 Master 记录二进制文件 导出数据并记录二进制位置 导入数据,设置二进制位 ...