社区讨论
费用流50pts求条
P4480[BJWC2018] 餐巾计划问题参与者 4已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mhjqajr2
- 此快照首次捕获于
- 2025/11/04 06:45 4 个月前
- 此快照最后确认于
- 2025/11/04 06:45 4 个月前
C
#include<bits/stdc++.h>
#define LL false
#define FST true
#define DEBUG false
#define FIO false
#define STD true
#define IOS false
#define unGet false
#define I128 false
#if STD
using namespace std;
#endif
#if LL
#define int long long
#endif
#if I128
#define int __int128
#endif
#if FST
#define flush() fwrite(obuf,1,O-obuf,stdout)
#define putchar(x) ((O==obuf+(1<<21))&&(flush(),O=obuf)),*O++=x
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read() {
register int x=0,f=1;
register char ch=getchar();
while(!(ch>='0'&&ch<='9'))
ch=getchar();
while(ch>='0'&&ch<='9')
x=x*10+(ch^48),ch=getchar();
return x*f;
}
inline void write(register int x){
if(x>9)
write(x/10);
putchar((x%10)^48);
}
struct Flush{
~Flush(){flush();}
}_;
#if unGet
#undef getchar
#endif
#else
inline int read(){
int x;
cin>>x;
return x;
}
inline void write(register long long x){
cout<<x;
}
#endif
#if FIO
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
#endif
bool st;
int n,s,t,now[400100],hd[400010],cnt=1;
long long Min;
bool book[400010];
int V[4000010],nxt[4000010];long long W[4000010],C[4000010];
inline void add(int u,int v,long long w,long long c){
++cnt;
V[cnt]=v,W[cnt]=w,C[cnt]=c,nxt[cnt]=hd[u];
hd[u]=cnt;
++cnt;
V[cnt]=u,W[cnt]=0,C[cnt]=-c,nxt[cnt]=hd[v];
hd[v]=cnt;
}
int dep[400010];
bool vis[400010];
deque<int>que;
inline bool spfa(){
for(int i=0;i<=t;i++) dep[i]=1e9,book[i]=0,now[i]=hd[i];
dep[s]=0;
vis[s]=1;
long long Sum=0;
que.push_front(s);
while(!que.empty()){
// if(que.size()>1&&dep[que.front()]>dep[que.back()]) swap(que.front(),que.back());
int u=que.front();
que.pop_front();
while(!que.empty()&&dep[u]*que.size()>Sum)
que.pop_front(),que.push_back(u),u=que.front();
vis[u]=0;
for(int i=hd[u];i;i=nxt[i]){
int v=V[i],w=C[i];
if(W[i]&&dep[v]>dep[u]+w){
dep[v]=dep[u]+w;
Sum+=dep[v];
if(vis[v]==0){
vis[v]=1;
if(que.size()&&dep[v]<dep[que.front()]) que.push_front(v);
else que.push_back(v);
}
}
}
}
return dep[t]!=1e9;
}
inline long long dfs(int x,long long limit){
book[x]=1;
if(x==t||!limit)
return limit;
int flow=0;
for(int i=now[x];i;i=nxt[i]){
now[x]=i;
if(!book[V[i]]&&W[i]&&dep[x]+C[i]==dep[V[i]]){
int res=dfs(V[i],min(limit,W[i]));
if(res){
Min+=res*C[i],W[i]-=res,W[i^1]+=res,flow+=res,limit-=res;
if(!limit)
break;
}
}
}
book[x]=0;
return flow;
}
int r[400010],p,m,f,N,S;
bool ed;
signed main(){
n=read();
m=read(),N=read(),f=read(),S=read(),p=read();
for(int i=1;i<=n;i++) r[i]=read();
s=0,t=n+n+1;
for(int i=1;i<=n;i++){
add(i,t,r[i],0),add(s,i,r[i],p);
}
for(int i=n+1;i<=n+n;i++){
add(s,i,r[i-n],0);
if(i-n+m<=n) add(i,i-n+m,1e18,f);
if(i-n+N<=n) add(i,i-n+N,1e18,S);
if(i+1<=n+n) add(i,i+1,1e18,0);
}
while(spfa()) dfs(s,1e18);
write(Min);
return 0;
}
自认为较优
回复
共 3 条回复,欢迎继续交流。
正在加载回复...