萌新求调网络流
查看原帖
萌新求调网络流
665801
h_rains楼主2023/2/26 20:51

RT

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1010,maxm=20010,INF=0x3f3f3f3f;
int n,m,S,T;
int h[maxn],e[maxm],f[maxm],ne[maxm],idx;
int q[maxn],d[maxn],pre[maxn];
bool st[maxn];
void add(int u,int v,int w)
{
	e[idx]=v,f[idx]=w,ne[idx]=h[u],h[u]=idx++;
	e[idx]=u,f[idx]=w,ne[idx]=h[v],h[v]=idx++;
}
bool bfs()
{
	int hh=0,tt=0;
	memset(st,false,sizeof(st));
	q[0]=S,st[S]=true,d[S]=INF;
	while(hh<=tt)
	{
		int t=q[hh++];
		for(int i=h[t];~i;i=ne[i])
		{
			int v=e[i];
			if(!st[v]&&f[i])
			{
				st[v]=1;
				d[v]=min(d[t],f[i]);
				pre[v]=i;
				if(v==T) return 1;
				q[++tt]=v;
			}
		}
	}
	return 0;
}
int EK()
{
	int ans=0;
	while(bfs())
	{
		ans+=d[T];
		for(int i=T;i!=S;i=e[pre[i]^1]) f[pre[i]]-=d[T],f[pre[i]^1]+=d[T];
	}
	return ans;
}
signed main()
{
	cin>>n>>m>>S>>T;
	memset(h,-1,sizeof(h));
	while(m--)
	{
		int u,v,w;
		cin>>u>>v>>w;
		add(u,v,w);
	}
	cout<<EK()<<endl;
}

只能过后3个点其余全WA/ll

2023/2/26 20:51
加载中...