题目:Alice 要拍电影,每一天只能参与一部电影的拍摄,每一部电影只能在 Wi 周之内的指定的日子拍摄,总共需要花 Di 天时间,求能否拍完所有电影。

典型的二分图多重匹配,这里用了最大流的 dinic 算法。构图:源点向每部电影流容量为 Di 的边,电影向允许的日期流容量为 1 的边,每一天向汇点流容量为 1 的边。跑一次最大流,如果最大流等于 ∑D,那么就可以。

一开始用了多路增广忘了把流量为零的 d 设为 -1……悲剧……TLE 了。

代码:

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6. const int MAXV = 520, FILM = 400, MAXE = 15000, INF = 0x3f3f3f3f;
  7. struct edge{
  8. int to, cap, next;
  9. edge(){};
  10. edge(int to_, int cap_, int next_):to(to_), cap(cap_), next(next_){};
  11. } es[MAXE];
  12. int head[MAXV], d[MAXV], que[MAXV], tot, tmp[8];
  13. void init(){
  14. memset(head, -1, sizeof(head));
  15. tot = 0;
  16. }
  17. inline void add2(const int &a, const int &b, const int &cap){
  18. // printf("%d---%d: %d\n", a, b, cap);
  19. es[tot] = edge(b, cap, head[a]); head[a] = tot++;
  20. es[tot] = edge(a, 0, head[b]); head[b] = tot++;
  21. }
  22. bool bfs(int s, int t){
  23. memset(d, -1, sizeof(d));
  24. d[s] = 0;
  25. int hh = 0, tt = 1;
  26. que[0] = s;
  27. while(hh < tt){
  28. int u = que[hh++];
  29. if(u == t)return true;
  30. for(int i = head[u]; ~i; i = es[i].next){
  31. int v = es[i].to;
  32. if(d[v] == -1 && es[i].cap){
  33. d[v] = d[u] + 1;
  34. que[tt++] = v;
  35. }
  36. }
  37. }
  38. return false;
  39. }
  40. int dfs(int s, int t, int low){
  41. if(s == t) return low;
  42. int ret = 0;
  43. for(int i = head[s]; ~i; i = es[i].next){
  44. int v = es[i].to, f = es[i].cap;
  45. if(d[v] == d[s] + 1 && f && (f = dfs(v, t, min(low - ret, f)))){
  46. es[i].cap -= f;
  47. es[i^1].cap += f;
  48. ret += f; //多路增广
  49. if(ret == low) break;
  50. }
  51. }
  52. if(!ret) d[s] = -1; //这里剪枝非常重要
  53. return ret;
  54. }
  55. int dinic(int s, int t){
  56. int ans = 0;
  57. while(bfs(s, t)){
  58. ans += dfs(s, t, INF);
  59. }
  60. return ans;
  61. }
  62. int main(){
  63. freopen("in.txt", "r", stdin);
  64. int T, N, D, W;
  65. scanf("%d", &T);
  66. while(T--){
  67. init();
  68. int sum = 0, day = 0;
  69. scanf("%d", &N);
  70. for(int i = 0; i < N; ++i){
  71. for(int j = 1; j <= 7; ++j) scanf("%d", &tmp[j]);
  72. scanf("%d%d", &D, &W);
  73. day = max(day, W);
  74. add2(0, FILM + i, D);
  75. sum += D;
  76. for(int w = 0; w < W; ++w){
  77. for(int j = 1; j <= 7; ++j){
  78. if(tmp[j]) add2(FILM + i, w * 7 + j, 1);
  79. }
  80. }
  81. }
  82. for(int i = day * 7; i > 0; --i){
  83. add2(i, 500, 1);
  84. }
  85. int ans = dinic(0, 500);
  86. if(ans == sum) printf("Yes\n");
  87. else printf("No\n");
  88. }
  89. return 0;
  90. }

POJ 1698 Alice's Chance的更多相关文章

  1. poj 1698 Alice‘s Chance

    poj 1698  Alice's Chance 题目地址: http://poj.org/problem?id=1698 题意: 演员Alice ,面对n场电影,每场电影拍摄持续w周,每周特定几天拍 ...

  2. 图论--网络流--最大流--POJ 1698 Alice's Chance

    Description Alice, a charming girl, have been dreaming of being a movie star for long. Her chances w ...

  3. poj 1698 Alice's Chance 最大流

    题目:给出n部电影的可以在周几拍摄.总天数.期限,问能不能把n部电影接下来. 分析: 对于每部电影连上源点,流量为总天数. 对于每一天建立一个点,连上汇点,流量为为1. 对于每部电影,如果可以在该天拍 ...

  4. POJ 1698 Alice&#39;s Chance(最大流+拆点)

    POJ 1698 Alice's Chance 题目链接 题意:拍n部电影.每部电影要在前w星期完毕,而且一周仅仅有一些天是能够拍的,每部电影有个须要的总时间,问能否拍完电影 思路:源点向每部电影连边 ...

  5. poj 1698 Alice&#39;s Chance 拆点最大流

    将星期拆点,符合条件的连边,最后统计汇点流量是否满即可了,注意结点编号. #include<cstdio> #include<cstring> #include<cmat ...

  6. Alice's Chance POJ - 1698(按时间点建边)

    Alice's Chance Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7791   Accepted: 3174 De ...

  7. 2018.07.06 POJ1698 Alice's Chance(最大流)

    Alice's Chance Time Limit: 1000MS Memory Limit: 10000K Description Alice, a charming girl, have been ...

  8. POJ 1698 最大流

    Alice's Chance Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7327   Accepted: 2992 De ...

  9. 【POJ 1698】Alice's Chance(二分图多重匹配)

    http://poj.org/problem?id=1698 电影和日子匹配,电影可以匹配多个日子. 最多有maxw*7个日子. 二分图多重匹配完,检查一下是否每个电影都匹配了要求的日子那么多. #i ...

随机推荐

  1. [GraphSAGE] docker安装与程序运行

    安装Docker与程序运行 1. requirements.txt Problem: Downloading https://files.pythonhosted.org/packages/69/cb ...

  2. 使用HashOperations操作redis

    方法 c参数 s说明 Long delete(H key, Object... hashKeys); H key:集合key Object... hashKeys:key对应hashkey  删除ma ...

  3. beta版本冲刺五

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  4. 轻量级权限管理系统——MVC基础

    Microsoft Web 开发平台

  5. 微信小程序-腾讯地图显示偏差问题

    原文地址: http://fanjiajia.cn/2018/08/30/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F-%E8%85%BE%E8%AE%A ...

  6. springmvc文件上传,出现400 的错误问题

    遇见的原因是公司系统上的图片上传忽然不好使了,报错400.单独针对这个模块调了好长时间都没解决,后来才发现前几天做过一个excel上传导入的功能... 使用SptingMVC3.1.3 对于文件上传提 ...

  7. 【转】GOOGLE-PROTOBUF与FLATBUFFERS数据的序列化和反序列化

    转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/unity3d-game/1607.html  关于Protobuf 通过本文的转载和分享的相关链接,足够 ...

  8. 201621044079《Java程序设计》第二周学习总结

    Week02-Java基本语法与类库 1.本周学习总结 记录本周学习中的重点 尝试使用 原则:少而精,自己写.即使不超过5行也可,但请一定不要简单的复制粘贴 1.学习了Java的数据类型 int ch ...

  9. 【工具学习】——教你读懂Maven的配置文件

    [前言] 最近在项目中用到了maven工具,相信很多第一次接触maven的人都有这样的困惑,maven的文件很简单,就像下图中的结构一样,但是它的功能十分强大,那是怎么做到的呢?配置文件!配置文件里是 ...

  10. javascript获取和判断浏览器窗口、屏幕、网页的高度、宽度等

    主要介绍了javascript获取和判断浏览器窗口.屏幕.网页的高度.宽度等 scrollHeight: 获取对象的滚动高度.scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端 ...