Henry_he @ 2018-10-26 09:47:44
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,s,t;
int d[100005];
int e[1000005],v[1000005],lst[100005],nxt[1000005],cnt;
int read()
{
int ans=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar());
for(;'0'<=c&&c<='9';c=getchar())
ans=ans*10+c-'0';
return ans;
}
void Build(int x,int y,int z)
{
e[++cnt]=y;
nxt[cnt]=lst[x];
lst[x]=cnt;
v[cnt]=z;
e[++cnt]=x;
nxt[cnt]=lst[y];
lst[y]=cnt;
v[cnt]=0;
}
int ans=0;
bool bfs(int s,int t)
{
queue<int> q;
memset(d,0,sizeof(d));
q.push(s);
d[s]=1;
while(!q.empty())
{
int x=q.front();
for(int u=lst[x];u;u=nxt[u])
if(!d[e[u]]&&v[u])
{
d[e[u]]=d[x]+1;
if(e[u]==t) return true;
q.push(e[u]);
}
q.pop();
}
return false;
}
int dfs(int x,int c)
{
if(c==0||x==t) return c;
int ans=0;
for(int u=lst[x];u;u=nxt[u])
{
int y=e[u];
if(d[x]+1==d[y]&&v[u])
{
int p=dfs(y,min(c-ans,v[u]));
v[u]-=p;
v[u^1]+=p;
ans+=p;
}
if(ans==c) break;
}
return ans;
}
int main()
{
n=read(),m=read(),s=read(),t=read();
for(int i=1;i<=m;i++)
{
int x=read(),y=read(),z=read();
Build(x,y,z);
}
while(bfs(s,t)) ans+=dfs(s,(1<<30)-1+(1<<30));
printf("%d\n",ans);
return 0;
}
by Henry_he @ 2018-10-26 10:56:06
@quhengyi11
by HomuraCat @ 2018-10-26 16:36:06
@Henry_he 你bfs那个地方,构建分层图的时候,你不能找到汇点t就return true呀,你要把每个点的d值跑出来才行