社区讨论

请求加强数据

P3376【模板】网络最大流参与者 7已保存回复 8

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
8 条
当前快照
1 份
快照标识符
@locgfinq
此快照首次捕获于
2023/10/30 13:23
2 年前
此快照最后确认于
2023/11/05 00:56
2 年前
查看原帖
蒟蒻反向边没加都 A 了。
Code:
CPP
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<cmath>
using namespace std;

typedef long long LL;
const LL MAXN=505,MAXM=1e4+50,INF=0x3f3f3f3f3f3f3f3f;

struct edge{
	LL nxt;
	LL to;
	LL cap;
	LL flow;
};

LL N,M,s,t,dis[MAXN],Ans=0;
edge e[MAXM<<1];
LL head[MAXN],now[MAXN],Cnte=1;
queue<LL> q;

void adde(LL u,LL v,LL w){
	++Cnte;
	e[Cnte].to=v;
	e[Cnte].cap=w;
	e[Cnte].nxt=head[u];
	head[u]=Cnte;
}

bool bfs(){
	for(int i=1;i<=N;++i) dis[i]=INF;
	while(!q.empty()) q.pop();
	q.push(s);
	dis[s]=0;
	while(!q.empty()){
		LL u=q.front();
		q.pop();
		now[u]=head[u];
		for(int i=head[u];i;i=e[i].nxt){
			LL v=e[i].to;
			if(e[i].cap>e[i].flow&&dis[u]+1<dis[v]){
				dis[v]=dis[u]+1;
				q.push(v);
			}
		}
	}
	return dis[t]!=INF;
}

LL dfs(LL u,LL flow){
	if(flow==0||u==t) return flow;
	LL res=0;
	for(int i=now[u];i;i=e[i].nxt){
		now[u]=i;
		LL v=e[i].to;
		if(dis[v]==dis[u]+1&&e[i].cap>e[i].flow){
			LL t=dfs(v,min(e[i].cap-e[i].flow,flow));
			if(!t) continue;
			e[i].flow+=t;
			//e[i^1].flow-=t;
			res+=t;
			flow-=t;
			if(flow==0) break;
		}
	}
	if(res==0) dis[u]=INF;
	return res;
}

LL Dinic(){
	LL res=0;
	while(bfs()) res+=dfs(s,INF);
	return res;
}

void Init(){
	scanf("%lld%lld%lld%lld",&N,&M,&s,&t);
	for(int i=1;i<=M;++i){
		LL u,v,w;
		scanf("%lld%lld%lld",&u,&v,&w);
		adde(u,v,w);//adde(v,u,0);
	}
}

void Solve(){
	Ans=Dinic();
}

void Print(){
	printf("%lld\n",Ans);
}

int main(){
	Init();
	Solve();
	Print();
	return 0;
}

回复

8 条回复,欢迎继续交流。

正在加载回复...