题意

类似的一道排队等候,算最小总等待时间的题目。

思路

但是这道题的边数很多,直接跑会tle,可以动态加边,就是先连上倒数第一次操作的边,跑一遍费用流,然后对使用了倒数第一条边的点,连上相应的倒数第二条边。以此类推

  1. #include <algorithm>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <iomanip>
  7. #include <bitset>
  8. #include <cctype>
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include <stack>
  13. #include <cmath>
  14. #include <queue>
  15. #include <list>
  16. #include <map>
  17. #include <set>
  18. #include <cassert>
  19.  
  20. /*
  21.  
  22. ⊂_ヽ
  23.   \\ Λ_Λ 来了老弟
  24.    \('ㅅ')
  25.     > ⌒ヽ
  26.    /   へ\
  27.    /  / \\
  28.    レ ノ   ヽ_つ
  29.   / /
  30.   / /|
  31.  ( (ヽ
  32.  | |、\
  33.  | 丿 \ ⌒)
  34.  | |  ) /
  35. 'ノ )  Lノ
  36.  
  37. */
  38.  
  39. using namespace std;
  40. #define lson (l , mid , rt << 1)
  41. #define rson (mid + 1 , r , rt << 1 | 1)
  42. #define debug(x) cerr << #x << " = " << x << "\n";
  43. #define pb push_back
  44. #define pq priority_queue
  45.  
  46. typedef long long ll;
  47. typedef unsigned long long ull;
  48. //typedef __int128 bll;
  49. typedef pair<ll ,ll > pll;
  50. typedef pair<int ,int > pii;
  51. typedef pair<int,pii> p3;
  52.  
  53. //priority_queue<int> q;//这是一个大根堆q
  54. //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
  55. #define fi first
  56. #define se second
  57. //#define endl '\n'
  58.  
  59. #define boost ios::sync_with_stdio(false);cin.tie(0)
  60. #define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
  61. #define max3(a,b,c) max(max(a,b), c);
  62. #define min3(a,b,c) min(min(a,b), c);
  63.  
  64. const ll oo = 1ll<<;
  65. const ll mos = 0x7FFFFFFF; //
  66. const ll nmos = 0x80000000; //-2147483648
  67. const int inf = 0x3f3f3f3f;
  68. const ll inff = 0x3f3f3f3f3f3f3f3f; //
  69. const ll mod = ;
  70. const double esp = 1e-;
  71. const double PI=acos(-1.0);
  72. const double PHI=0.61803399; //黄金分割点
  73. const double tPHI=0.38196601;
  74.  
  75. template<typename T>
  76. inline T read(T&x){
  77. x=;int f=;char ch=getchar();
  78. while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
  79. while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
  80. return x=f?-x:x;
  81. }
  82.  
  83. inline void cmax(int &x,int y){if(x<y)x=y;}
  84. inline void cmax(ll &x,ll y){if(x<y)x=y;}
  85. inline void cmin(int &x,int y){if(x>y)x=y;}
  86. inline void cmin(ll &x,ll y){if(x>y)x=y;}
  87.  
  88. /*-----------------------showtime----------------------*/
  89.  
  90. const int maxn = ;
  91. int n,m;
  92. int p[maxn],mp[maxn][maxn];
  93.  
  94. struct E
  95. {
  96. int v,val,cost;
  97. int nxt;
  98. }edge[];
  99. int head[maxn*maxn],gtot;
  100. void addedge(int u,int v,int val, int cost){
  101. edge[gtot].v = v;
  102. edge[gtot].val = val;
  103. edge[gtot].cost = cost;
  104. edge[gtot].nxt = head[u];
  105. head[u] = gtot++;
  106.  
  107. edge[gtot].v = u;
  108. edge[gtot].val = ;
  109. edge[gtot].cost = -cost;
  110. edge[gtot].nxt = head[v];
  111. head[v] = gtot++;
  112. }
  113.  
  114. int vis[maxn*maxn],pre[maxn*maxn],path[maxn*maxn];
  115. ll dis[maxn*maxn];
  116. bool spfa(int s,int t){
  117.  
  118. for(int i=s; i<=t; i++) dis[i] = inff,pre[i] = -,vis[i] = ;
  119.  
  120. queue<int>que; que.push(s);
  121. vis[s] = ;
  122. dis[s] = ;
  123. while(!que.empty()){
  124. int u = que.front(); que.pop(); vis[u] = ;
  125.  
  126. for(int i=head[u]; ~i; i =edge[i].nxt){
  127. int v = edge[i].v,val = edge[i].val, cost = edge[i].cost;
  128. if(val > && dis[v] > dis[u] + cost){
  129. dis[v] = dis[u] + cost;
  130. pre[v] = u; path[v] = i;
  131. if(vis[v] == ) {
  132. vis[v] = ;
  133. que.push(v);
  134. }
  135. }
  136. }
  137. }
  138. return pre[t] != -;
  139.  
  140. }
  141. int sp = ;
  142. ll mcmf(int s,int t){
  143.  
  144. ll flow = , cost = ;
  145. while(spfa(s, t)){
  146. int f = inf;
  147. for(int i=t; i!=s; i=pre[i]){
  148. f = min(f, edge[path[i]].val);
  149. }
  150. flow += f;
  151. cost += 1ll*f * dis[t];
  152. for(int i=t; i!=s; i=pre[i]){
  153. edge[path[i]].val -= f;
  154. edge[path[i]^].val += f;
  155. }
  156.  
  157. int la = edge[path[t]^].v + ;
  158.  
  159. int p = (la - - n)/sp + ;
  160. int b = (la - - n)% sp + ;
  161.  
  162. addedge(la, t, , );
  163. for(int i=; i<=n; i++){
  164. addedge(i, la, , mp[i][p] * b);
  165. }
  166. }
  167. return cost;
  168. }
  169. int main(){
  170. memset(head, -, sizeof(head));
  171. scanf("%d%d", &n, &m);
  172. rep(i, , n) scanf("%d", &p[i]),sp += p[i];
  173. rep(i, , n) rep(j, , m) scanf("%d", &mp[i][j]);
  174. int s = , t = n+m*sp+;
  175.  
  176. for(int i=; i<=n; i++) addedge(s, i, p[i], );
  177. for(int i=; i<=m; i++){
  178. addedge(n + (i-)*sp + , t, , );
  179. }
  180. for(int i=; i<=n; i++){
  181. for(int j=; j<=m; j++){
  182. addedge(i, n + (j-)*sp + , , mp[i][j]);
  183. }
  184. }
  185.  
  186. printf("%lld\n", mcmf(s, t));
  187. return ;
  188. }

P2050 [NOI2012]美食节 动态连边优化费用流的更多相关文章

  1. P2050 [NOI2012]美食节 动态加边加点

    修车数据加强版 需要动态加边加点 #include<bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; , MA ...

  2. P2050 [NOI2012]美食节(费用流)

    P2050 [NOI2012]美食节 P2053 [SCOI2007]修车的加强版 因为数据较大,一次性把所有边都加完会T 于是我们每次只连需要的边跑费用流 就是开始先连所有厨师做倒数第1道菜 跑费用 ...

  3. P2050 [NOI2012]美食节

    题目地址:P2050 [NOI2012]美食节 先来讲一下P2053 [SCOI2007]修车(如果会做请跳过) 同一时刻有 \(N\) 位车主带着他们的爱车来到了汽车维修中心.维修中心共有 \(M\ ...

  4. HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解

    题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...

  5. 【BZOJ2879】[Noi2012]美食节 动态加边网络流

    [BZOJ2879][Noi2012]美食节 Description CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M自然不愿意错过这场盛宴.他很快就尝遍了美食 ...

  6. 网络流小记(EK&dinic&当前弧优化&费用流)

    欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...

  7. 「SNOI2019」通信 分治优化费用流建图

    题意: n 个排成一列的哨站要进行通信.第 i 个哨站的频段为 ai. 每个哨站 ii 需要选择以下二者之一: 1.直接连接到控制中心,代价为 W:2.连接到前面的某个哨站 j(j<i),代价为 ...

  8. 洛谷$P2050\ [NOI2012]$美食节 网络流

    正解:网络流 解题报告: 传送门$QwQ$ 昂开始看到$jio$得,哇长得好像上一题嗷$QwQ$ 然后仔细康康数据范围,发现,哇好像要几万个点,,,显然就$GG$了 但感$jio$思路方向好对的亚子? ...

  9. 洛谷P2050 [NOI2012]美食节

    动态加边网络流 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring ...

随机推荐

  1. vue+ elementUI 打包发布到服务器后,element-icons.woff文件404

    vue项目打包部署到服务器,静态资源文件404 js文件404问题        原因:打包的项目静态资源的路径需要设置为绝对路径.如果是相对路径会出错 解决办法:修改config/index.js文 ...

  2. Extjs的文件上传问题

    最近做一个ExtJs4.0的文件上传.发现在没有添加 xtype:filefield,   时提交数据form的数据,修改form都能提交,而且返回正常.但是当加入xtype:filefield后,返 ...

  3. .net持续集成测试篇之Nunit文件断言、字符串断言及集合断言

    使用前面讲过的方法基本上能够完成工作中的大部分任务了,然而有些功能实现起来还是比较麻烦的,比如说字符串相等性比较不区分大小写,字符串是否匹配某一正则规则,集合中的每一个(某一个)元素是否符合特定规则等 ...

  4. .NET Core 3.0深入源码理解HttpClientFactory之实战

      写在前面 前面两篇文章透过源码角度,理解了HttpClientFactory的内部实现,当我们在项目中使用时,总会涉及以下几个问题: HttpClient超时处理以及重试机制 HttpClient ...

  5. 【Java例题】5.5 两个字符串中最长公共子串

    5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public st ...

  6. 有趣的RPC理解

    RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在,如TCP或UDP,为通 ...

  7. python3学习--文件读写

    这一篇我们来看文件读写操作. 打开和创建文件主要是open()函数: f = open('filename','r') # 读模式 f = open('filename','w') # 写模式 f = ...

  8. React Native-安卓环境的搭建

    最近公司做一个项目,项目中使用React Native(简称RN)来写安卓APP,现在我分享下安卓环境搭建的过程. 我参照https://reactnative.cn/docs/getting-sta ...

  9. 100天搞定机器学习|Day16 通过内核技巧实现SVM

    前情回顾 机器学习100天|Day1数据预处理100天搞定机器学习|Day2简单线性回归分析100天搞定机器学习|Day3多元线性回归100天搞定机器学习|Day4-6 逻辑回归100天搞定机器学习| ...

  10. Spark 系列(十六)—— Spark Streaming 整合 Kafka

    一.版本说明 Spark 针对 Kafka 的不同版本,提供了两套整合方案:spark-streaming-kafka-0-8 和 spark-streaming-kafka-0-10,其主要区别如下 ...