思路:这题我在下午重现的时候就用的费用流做,可是各种悲催的超时,只是我一开始的那种建图方式多了一个二分查找。

戏剧性的是,求距离的返回值写成int型了,CodeBlock编译器又没有警告,然后就WA啊WA,AC率一下就被拉低了。

当然,对每种工人分别建图是不变的,因为每种工人互不影响。

后来想到了一个较好的建图方式,将每个点拆成3个点,i,i+n,i+2*n。

1号点就是仓库,也就是超级源点,3*n+1号点为超级汇点。

由1号点想每个i建一条流量为ty[i][j],费用为1的边。表示每次增加流量,人数就增加。

由i向i+2*n建一条流量为ty[i][j],费用为0的边。

由i+2*n向汇点建一条流量为ty[i][j],费用为0的边。

由1向每个i+n建一条流量为ty[i][j],费用为0的边。

对i号点,寻找开始时间sta[k]满足b[i]+p[i]+d[i][j]的点k,然后由i+n向k+2*n建一条流量为ty[i][j],费用为0的边。

这样,如果i号点的工人能到k号点工作,那么k号点的k+2*n到3*n+1的流量就会减少ty[i][j]。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<cstdio>
  6. #define inf 100000000
  7. using namespace std;
  8. const int Maxn = ;
  9. struct Edge{
  10. int v;
  11. int val;
  12. int cost;
  13. int next;
  14. }edge[Maxn*];
  15. struct Point{
  16. double x,y;
  17. }p[Maxn];
  18. int head[Maxn],n,m,k;
  19. int e;
  20. int dis[Maxn],pre[Maxn], pos[Maxn],sta[Maxn],en[Maxn],ty[Maxn][],flow;
  21. int que[Maxn*];
  22. double d[Maxn][Maxn];
  23. bool vis[Maxn];
  24. void add(int u, int v, int val, int cost)
  25. {
  26. edge[e].v = v;
  27. edge[e].val = val;
  28. edge[e].cost = cost;
  29. edge[e].next = head[u];
  30. head[u] = e++;
  31. edge[e].v = u;
  32. edge[e].val = ;
  33. edge[e].cost = -cost;
  34. edge[e].next = head[v];
  35. head[v] = e++;
  36. }
  37. double DIS(Point a,Point b)
  38. {
  39. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  40. }
  41. void init()
  42. {
  43. memset(head,-,sizeof(head));
  44. e=;
  45. }
  46. bool spfa(int s, int t)
  47. {
  48. int i;
  49. memset(pre, -, sizeof(pre));
  50. memset(vis, , sizeof(vis));
  51. int Head, tail;
  52. Head = tail = ;
  53. for(i = ; i < Maxn; i++)
  54. dis[i] = inf;
  55. que[tail++] = s;
  56. pre[s] = s;
  57. dis[s] = ;
  58. vis[s] = ;
  59. while(Head != tail)
  60. {
  61. int now = que[Head++];
  62. vis[now] = ;
  63. for(i=head[now]; i != -; i = edge[i].next)
  64. {
  65. int adj = edge[i].v;
  66. if(edge[i].val > && dis[now] + edge[i].cost < dis[adj])
  67. {
  68. dis[adj] = dis[now] + edge[i].cost;
  69. pre[adj] = now;
  70. pos[adj] = i;
  71. if(!vis[adj])
  72. {
  73. vis[adj] = ;
  74. que[tail++] = adj;
  75. }
  76. }
  77. }
  78. }
  79. return pre[t] != -;
  80. }
  81. int MinCostFlow(int s, int t)
  82. {
  83. int i;
  84. int cost = ;
  85. flow = ;
  86. while(spfa(s, t))
  87. {
  88. int f = ;
  89. for(i = t; i != s; i = pre[i])
  90. if (edge[pos[i]].val < f)
  91. f = edge[pos[i]].val;
  92. flow += f;
  93. cost += dis[t] * f;
  94. for(i = t; i != s; i = pre[i])
  95. {
  96. edge[pos[i]].val -= f;
  97. edge[pos[i] ^ ].val += f;
  98. }
  99. }
  100. return cost;
  101. }
  102. void build(int type)
  103. {
  104. int i,j;
  105. init();
  106. for(i=;i<=n;i++){
  107. add(,i,ty[i][type],);
  108. add(i,i+*n,ty[i][type],);
  109. add(,i+n,ty[i][type],);
  110. add(i+*n,*n+,ty[i][type],);
  111. for(j=;j<=n;j++){
  112. if(sta[i]+en[i]+d[i][j]<=sta[j]){
  113. add(i+n,j+*n,ty[i][type],);
  114. }
  115. }
  116. }
  117. }
  118. int solve()
  119. {
  120. int i,j,u,v;
  121. int ans=;
  122. for(i=;i<=m;i++){
  123. build(i);
  124. ans+=MinCostFlow(,*n+);
  125. }
  126. return ans;
  127. }
  128. int main()
  129. {
  130. int i,j,u,v,c,t;
  131. scanf("%d",&t);
  132. while(t--)
  133. {
  134. init();
  135. scanf("%d%d",&n,&m);
  136. scanf("%lf%lf",&p[].x,&p[].y);
  137. for(i=;i<=n;i++){
  138. scanf("%lf%lf%d%d",&p[i].x,&p[i].y,&sta[i],&en[i]);
  139. for(j=;j<=m;j++){
  140. scanf("%d",&ty[i][j]);
  141. }
  142. }
  143. for(i=;i<=n;i++){
  144. for(j=i+;j<=n;j++){
  145. d[i][j]=d[j][i]=DIS(p[i],p[j]);
  146. }
  147. }
  148. printf("%d\n",solve());
  149. }
  150. return ;
  151. }

hdu 4494 最小费用流的更多相关文章

  1. hdu 4494 Teamwork 最小费用最大流

    Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...

  2. Going Home (hdu 1533 最小费用流)

    集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...

  3. hdu 1853 最小费用流好题 环的问题

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Tota ...

  4. HDU 6188最小费用流

    题目链接:http://hdu.hustoj.com/showproblem.php?pid=6118 掉坑里了,图很好建,Wa了一发,看了Disscuss里面有人提供了一组样例,画图发现:最小流模板 ...

  5. hdu 4744 最小费用流

    #include <cstdio> #include <queue> #include <cstring> #include <cmath> #defi ...

  6. hdu 4411 最小费用流

    思路:主要就是要把一个每个城市拆为两个点,建一条容量为1,费用为-inf的边,保证每个城市都会被遍历. /*最小费用最大流*/ #include<iostream> #include< ...

  7. hdu 4494 Teamwork (可行流的最小流)

    去年通话邀请赛的B题,当时居然过的那么少...明明是一道非常裸的可行流最小流麽..仅仅要对每种人分别求一下可行最小流加起来就能够了.建图是对每一个点拆点,容量上下届都设为v[i],然后每一个点间能连边 ...

  8. HDU 3667.Transportation 最小费用流

    Transportation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. HDU 4067 hdoj 4067 Random Maze 最小费用流

    给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...

随机推荐

  1. git强制覆盖本地文件

    git fetch --all git reset --hard origin/master

  2. Linux 基本权限(一)

    1. 权限概念 root@hang:/home# ll 总用量 20#文件权限 链接数量 文件所有者 所属用户组 容量大小B 创建(修改)时间 文件名 drwxr-xr-x root root 11月 ...

  3. android自定义相册 支持低端机不内存溢出

    1 之前在网上看的自定义相册很多时候在低端机都会内存溢出开始上代码把 首先我们要拿到图片的所有路径 cursor = context.getContentResolver().query( Media ...

  4. Cocos2d-x 3.0 动作

    http://blog.csdn.net/lnb333666/article/details/16858635 //运行一个action动作对象 runAction("action对象&qu ...

  5. android仿win8 metro磁贴布局

    代码下载     //更新代码,   这里是更新后的代码 //////////////////////// 1,含一个图片无限滚动的控件,自己实现的 2.可新增删除每个磁贴 3.来个图片吧 ////* ...

  6. [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects

    Each time you use the Async Pipe, you create a new subscription to the stream in the template. This ...

  7. [Node.js] Broswerify -- 1

    Browserify is a tool that brings node.js style development to the browser. The thing you can see on ...

  8. 使用贝赛尔曲线画扇形、圆形、弧线、多边形,实现App下载时的动画效果demo

    // // MyView.swift // TestUIBezierPath // // Created by iCodeWoods on 16/5/8. // Copyright © 2016年 i ...

  9. oc-27-@property的参数

    //01加强-10 @property .4前 ) @property + 手动实现 ) @property int age; + @synthesize age;//get和set方法的声明和实现都 ...

  10. 源码分析:静态分析 C 程序函数调用关系图

    http://www.tinylab.org/callgraph-draw-the-calltree-of-c-functions/