题意

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

思路

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

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const ll mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = ;
int n,m;
int p[maxn],mp[maxn][maxn]; struct E
{
int v,val,cost;
int nxt;
}edge[];
int head[maxn*maxn],gtot;
void addedge(int u,int v,int val, int cost){
edge[gtot].v = v;
edge[gtot].val = val;
edge[gtot].cost = cost;
edge[gtot].nxt = head[u];
head[u] = gtot++; edge[gtot].v = u;
edge[gtot].val = ;
edge[gtot].cost = -cost;
edge[gtot].nxt = head[v];
head[v] = gtot++;
} int vis[maxn*maxn],pre[maxn*maxn],path[maxn*maxn];
ll dis[maxn*maxn];
bool spfa(int s,int t){ for(int i=s; i<=t; i++) dis[i] = inff,pre[i] = -,vis[i] = ; queue<int>que; que.push(s);
vis[s] = ;
dis[s] = ;
while(!que.empty()){
int u = que.front(); que.pop(); vis[u] = ; for(int i=head[u]; ~i; i =edge[i].nxt){
int v = edge[i].v,val = edge[i].val, cost = edge[i].cost;
if(val > && dis[v] > dis[u] + cost){
dis[v] = dis[u] + cost;
pre[v] = u; path[v] = i;
if(vis[v] == ) {
vis[v] = ;
que.push(v);
}
}
}
}
return pre[t] != -; }
int sp = ;
ll mcmf(int s,int t){ ll flow = , cost = ;
while(spfa(s, t)){
int f = inf;
for(int i=t; i!=s; i=pre[i]){
f = min(f, edge[path[i]].val);
}
flow += f;
cost += 1ll*f * dis[t];
for(int i=t; i!=s; i=pre[i]){
edge[path[i]].val -= f;
edge[path[i]^].val += f;
} int la = edge[path[t]^].v + ; int p = (la - - n)/sp + ;
int b = (la - - n)% sp + ; addedge(la, t, , );
for(int i=; i<=n; i++){
addedge(i, la, , mp[i][p] * b);
}
}
return cost;
}
int main(){
memset(head, -, sizeof(head));
scanf("%d%d", &n, &m);
rep(i, , n) scanf("%d", &p[i]),sp += p[i];
rep(i, , n) rep(j, , m) scanf("%d", &mp[i][j]);
int s = , t = n+m*sp+; for(int i=; i<=n; i++) addedge(s, i, p[i], );
for(int i=; i<=m; i++){
addedge(n + (i-)*sp + , t, , );
}
for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
addedge(i, n + (j-)*sp + , , mp[i][j]);
}
} printf("%lld\n", mcmf(s, t));
return ;
}

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. 深入理解Java中的AQS

    AQS概述 ​ AbstractQueuedSynchronizer抽象队列同步器简称AQS,它是实现同步器的基础组件,juc下面Lock的实现以及一些并发工具类就是通过AQS来实现的,这里我们通过A ...

  2. PHP 防范xss攻击(转载)

    XSS 全称为 Cross Site Scripting,用户在表单中有意或无意输入一些恶意字符,从而破坏页面的表现! 看看常见的恶意字符XSS 输入: 1.XSS 输入通常包含 JavaScript ...

  3. 【NOI 2015】程序自动分析 并查集与离散化处理

    题目描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,-代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变量 ...

  4. Redis优化建议

    优化的一些建议 1.尽量使用短的key 当然在精简的同时,不要完了key的"见名知意".对于value有些也可精简,比如性别使用0.1. 2.避免使用keys * keys *, ...

  5. JavaFX 选择文件 导入Excel文件并解析

    FXML 控制器 : @FXML public void selectExcel(MouseEvent event) { FileChooser fileChooser = new FileChoos ...

  6. LeetCode :2.两数相加 解题报告及算法优化思路

    题目连接:2.两数相加 题意 题目难度标为 中等, 因为题意上有一部分理解难度,以及需要数据结构的链表基础. 还不知道到链表的童鞋可以粗略的看下百度百科或者是翻出数据结构的书看一看,通俗一点的语言来解 ...

  7. Netty学习(五)-DelimiterBasedFrameDecoder

    上一节我们说了LineBasedframeDecoder来解决粘包拆包的问题,TCP以流的方式进行数据传输,上层应用协议为了对消息进行区分,一般采用如下4种方式: 消息长度固定,累计读取到消息长度总和 ...

  8. Netty学习(一)-为什么选择Netty

    前面我们简单学习了NIO.我们知道java的I/O模型一共有四种,分别是:传统的BIO,伪异步I/O,NIO和AIO.为了澄清概念和分清区别,我们还是先简单的介绍一下他们的概念,然后再去比较优劣.以及 ...

  9. UVA - 1152 --- 4 Values whose Sum is 0(二分)

    问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...

  10. wordpress修改登录密码

    wordpress忘记密码更改 网上搜到的方法: 1.后台邮件重置: 2,phpmyadmin登录数据库,执行mysql语句或者在wp_users表中重置密码: 3,利用php文件重置. 这是提供一种 ...