建图:本题拆点比较特殊,由于某天有新旧餐巾的区分,且统计当天新餐巾数量时不能算上上次的旧餐巾数量,故可以将当天餐巾拆分为旧餐巾和新餐巾两部分,旧餐巾的来源只有一种,即当天的新餐巾,其去向有三种:流向下一天,通过慢洗店流向某一天,通过快洗店流向某一天。对于新餐巾,其来源有三种:买的新餐巾,通过前面的慢洗店流来的,通过前面的快洗店流来的。其流出即变成旧餐巾。即可以据此建立流网络,建立源点 s 和汇点 t,s 向所有的旧餐巾连边,容量为当天的需求量,费用为 0,向所有的新餐巾连边,容量足够大,费用为餐巾的单价,旧餐巾向下一天的旧餐巾连边,容量足够大,费用为 0,同时相对应的天的新餐巾连边,容量足够大,费用为对应的单价,新餐巾向 t 连边,容量为当天需求量,费用为 0,由于 s 到所有新餐巾表示的点容量可以足够大,故所有新餐巾表示的点到 t 的流量都满流,故最小费用最大流即为所求
时间复杂度:O(knf)
代码
// Problem: 餐巾计划问题// Contest: AcWing// URL: https://www.acwing.com/problem/content/2186/// Memory Limit: 64 MB// Time Limit: 1000 ms// // Powered by CP Editor (https://cpeditor.org)// %%%Skyqwq#include<bits/stdc++.h>//#define int long long#define help {cin.tie(NULL); cout.tie(NULL);}#define pb push_back#define fi first#define se second#define mkp make_pairusingnamespace std;
typedeflonglong LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> boolchkMax(T &x, T y){ return (y > x) ? x = y, 1 : 0; }
template <typename T> boolchkMin(T &x, T y){ return (y < x) ? x = y, 1 : 0; }
template <typename T> voidinlineread(T &x){
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
constint N=1605,M=10005,inf=1e9;
int n,p,d1,f1,d2,f2,S,T;
int h[N],f[M],w[M],ne[M],e[M],idx;
int d[N],incf[N],q[N],pre[N];
bool st[N];
voidadd(int a,int b,int c,int d){
e[idx]=b,f[idx]=c,w[idx]=d,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,w[idx]=-d,ne[idx]=h[b],h[b]=idx++;
}
boolspfa(){
memset(d,0x3f,sizeof d);
memset(incf,0,sizeof incf);
d[S]=0,q[0]=S,incf[S]=inf;
int hh=0,tt=1;
while(hh!=tt)
{
int x=q[hh++];
if(hh==N)hh=0;
st[x]=false;
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]>d[x]+w[i]&&f[i])
{
d[y]=d[x]+w[i];
pre[y]=i;
incf[y]=min(incf[x],f[i]);
if(!st[y])
{
q[tt++]=y;
if(tt==N)tt=0;
st[y]=true;
}
}
}
}
return incf[T]>0;
}
intEK(){
int cost=0;
while(spfa())
{
int t=incf[T];
cost+=t*d[T];
for(int i=T;i!=S;i=e[pre[i]^1])
f[pre[i]]-=t,f[pre[i]^1]+=t;
}
return cost;
}
intmain(){
memset(h,-1,sizeof h);
scanf("%d%d%d%d%d%d",&n,&p,&d1,&f1,&d2,&f2);
S=0,T=2*n+1;
for(int i=1;i<=n;i++)
{
add(S,i,inf,p);
if(i+d1<=n)add(i+n,i+d1,inf,f1);
if(i+d2<=n)add(i+n,i+d2,inf,f2);
if(i+1<=n)add(i+n,i+1+n,inf,0);
int c;
scanf("%d",&c);
add(S,i+n,c,0);
add(i,T,c,0);
}
printf("%d",EK());
return0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!