社区讨论
萌新妹子费用流模板T了8个月www
题目总版参与者 3已保存回复 8
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 8 条
- 当前快照
- 1 份
- 快照标识符
- @lo7irjty
- 此快照首次捕获于
- 2023/10/27 02:30 2 年前
- 此快照最后确认于
- 2023/10/27 02:30 2 年前
同机房EK全都过了……
CPP#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 5005
#define maxm 150050
#define INF 0x3f3f3f3f
#define int long long
using namespace std;
inline int read(){
int x=0; char c=getchar();
while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48); c=getchar();}
return x;
}
int n,m,s,t,fa[maxn],flow[maxn],fare[maxn];
int Head[maxm],Next[maxm],to[maxm],w[maxm],Cost[maxn],cnt=0;
bool vis[maxn];
inline void add(int u,int v,int c,int cst){
Next[cnt]=Head[u]; to[cnt]=v; w[cnt]=c; Cost[cnt]=cst; Head[u]=cnt++;
}
inline bool spfa(){
memset(fare,0x3f,sizeof(fare));
memset(flow,0,sizeof(flow));
int q[1000050],top=0,tail=-1;
q[++tail]=s; vis[s]=true; flow[s]=INF; fare[s]=0;
while(top<=tail){
int now=q[top]; top++;
vis[now]=false;
for(int i=Head[now];i!=-1;i=Next[i]){
if(w[i]<=0) continue;
if(fare[to[i]]<=fare[now]+Cost[i]) continue;
fare[to[i]]=fare[now]+Cost[i];
flow[to[i]]=min(flow[now],w[i]);
fa[to[i]]=i;
if(!vis[to[i]]){
if(tail+1>=maxn) top=0,tail=-1;
q[++tail]=to[i];
vis[to[i]]=true;
}
}
}
return flow[t]>0;
}
inline void EK(){
int res=0,cost=0;
while(spfa()){
res+=flow[t]; cost+=flow[t]*fare[t];
for(int i=t;i!=s;i=to[fa[i]^1]) w[fa[i]]-=flow[t],w[fa[i]^1]+=flow[t];
}
printf("%lld %lld",res,cost);
}
signed main(){
memset(Head,-1,sizeof(Head));
n=read(); m=read(); s=read(); t=read();
for(int i=1;i<=m;i++){
int u=read(),v=read(),c=read(),w=read();
add(u,v,c,w); add(v,u,0,-w);
}
EK();
return 0;
}
回复
共 8 条回复,欢迎继续交流。
正在加载回复...