P2050 [NOI2012]美食节 动态连边优化费用流
题意
类似的一道排队等候,算最小总等待时间的题目。
思路
但是这道题的边数很多,直接跑会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]美食节 动态连边优化费用流的更多相关文章
- P2050 [NOI2012]美食节 动态加边加点
修车数据加强版 需要动态加边加点 #include<bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; , MA ...
- P2050 [NOI2012]美食节(费用流)
P2050 [NOI2012]美食节 P2053 [SCOI2007]修车的加强版 因为数据较大,一次性把所有边都加完会T 于是我们每次只连需要的边跑费用流 就是开始先连所有厨师做倒数第1道菜 跑费用 ...
- P2050 [NOI2012]美食节
题目地址:P2050 [NOI2012]美食节 先来讲一下P2053 [SCOI2007]修车(如果会做请跳过) 同一时刻有 \(N\) 位车主带着他们的爱车来到了汽车维修中心.维修中心共有 \(M\ ...
- HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解
题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...
- 【BZOJ2879】[Noi2012]美食节 动态加边网络流
[BZOJ2879][Noi2012]美食节 Description CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M自然不愿意错过这场盛宴.他很快就尝遍了美食 ...
- 网络流小记(EK&dinic&当前弧优化&费用流)
欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...
- 「SNOI2019」通信 分治优化费用流建图
题意: n 个排成一列的哨站要进行通信.第 i 个哨站的频段为 ai. 每个哨站 ii 需要选择以下二者之一: 1.直接连接到控制中心,代价为 W:2.连接到前面的某个哨站 j(j<i),代价为 ...
- 洛谷$P2050\ [NOI2012]$美食节 网络流
正解:网络流 解题报告: 传送门$QwQ$ 昂开始看到$jio$得,哇长得好像上一题嗷$QwQ$ 然后仔细康康数据范围,发现,哇好像要几万个点,,,显然就$GG$了 但感$jio$思路方向好对的亚子? ...
- 洛谷P2050 [NOI2012]美食节
动态加边网络流 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring ...
随机推荐
- python3 读取文件-2
1.脚本 from sys import argv script,filename = argv#以读的模式打开txt文件txt = open(filename,'r+')print ("t ...
- unc路径
1.什么是UNC路径?UNC路径就是类似\\softer这样的形式的网络路径.UNC为网络(主要指局域网)上资源的完整 Windows 2000 名称.格式: \\servername\sharena ...
- 【iOS】更新 CocoaPods 后 Podfile 报错
更新了 CocoaPods 后,再执行 "pod install" 时报了如下错误: [!] The dependency `AFOnoResponseSerializer` is ...
- PHP验证身份证格式
互联网公司对身份证验证的需求越来越多,然而普通的小公司是无法对接公安部门的身份认证系统的.几乎都是在网上买一些大的互联网公司的一些认证服务.即使是便宜一些的认证价格也达到了10万次/万元.也就是一角钱 ...
- 编写自定义 .NET Core 主机以从本机代码控制 .NET 运行时
自定义 .Net Core 主机运行.Net Core代码,以及控制运行时运行状态,是在.Net Core 高级运行环境以及定制.Net Host ,CLR 等必不可少的. 这些设置包括为 1 ...
- Postgresql部署及简单操作
PostgreSQL是一个功能强大的开源对象关系数据库管理系统(ORDBMS),在开源数据库使用上与MySQL各领风骚.但也有不少人质疑postgresql的未来,正所谓,赞扬或批判一种数据库都必须先 ...
- gRPC的简单使用
目录 前言 gRPC的简单介绍 基本用法 服务的定义 服务端代码编写 客户端代码编写 运行效果 服务治理(注册与发现) .NET Core 2.x 和 .NET Core 3.0的细微区别 扩展阅读 ...
- Zookeeper的命令行操作(三)
Zookeeper的命令行操作 1. ZooKeeper服务命令 在准备好相应的配置之后,可以直接通过zkServer.sh 这个脚本进行服务的相关操作 1. 启动ZK服务: sh bin/zkSer ...
- CSS3 Flex 布局教程
网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂 ...
- struts的上传下载
文件上传 添加jar包 commons-io-1.3.2.jar commons-fileupload-1.2.1.jar 前台页面 form表单 method值为post 添加"encty ...