建图:对于 1∼n+1 的点,其中 i−>i+1 的边当作第 i 天,每条边有上下界,即 [Ai,inf],每名工作者工作的时间为第 Si 到 Ti,费用为 Ci,对应建立的流网络中的节点即 Si 到 Ti+1,不妨从 Ti+1 向 Si 两边,容量足够大,费用为 Ci,现在的流网络即无源汇上下界可行流,2188. 无源汇上下界可行流 给出了解决方法,即建立源点 s 和汇点 t,从 s 向所有 ∑c入−∑c出 为正的点连边,容量为 ∑c入−∑c出,费用为 0,从所有 ∑c出−∑c入 为正的点向 t 连边,容量为 ∑c出−∑c入,费用为 0,计算从 s 到 t 的最小费用最大流即为所求
时间复杂度:O(k×(n+m)×f)
代码
// Problem: 志愿者招募// Contest: AcWing// URL: https://www.acwing.com/problem/content/971/// Memory Limit: 64 MB// Time Limit: 2000 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=1005,M=24005,inf=1e9;
int n,m,S,T;
int h[N],f[M],w[M],ne[M],e[M],idx;
int d[N],incf[N],pre[N],q[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,incf[S]=inf,q[0]=S;
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",&n,&m);
S=0,T=n+2;
int lst=0;
for(int i=1;i<=n;i++)
{
int c;
scanf("%d",&c);
lst-=c;
if(lst<0)add(i,T,-lst,0);
elseif(lst>0)
add(S,i,lst,0);
add(i,i+1,inf-c,0);
lst=c;
}
add(S,n+1,lst,0);
for(int i=1;i<=m;i++)
{
int s,t,c;
scanf("%d%d%d",&s,&t,&c);
add(t+1,s,inf,c);
}
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框架的用法!