1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #define R register #define INF 1<<30 using namespace std; template <typename T> inline T read() { T x=0;int w=0;char c=getchar(); while(!isdigit(c))w|=c=='-',c=getchar(); while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar(); return w?-x:x; } namespace ysr { typedef long long ll; const int maxn=100000; struct Edge{ int to,nxt; ll dis; }e[200000]; int cur[maxn],head[maxn],ecnt=1; int n,m,s,end,dep[maxn]; inline void addedge(int from,int to,ll dis){ e[++ecnt].to=to,e[ecnt].nxt=head[from],e[ecnt].dis=dis,head[from]=ecnt; } inline void add(int from,int to,ll dis){ addedge(from,to,dis);addedge(to,from,0); } ll DFS(int x,ll flow) { if(x==end) return flow; ll used=0; for(R int i=cur[x];i;i=e[i].nxt) { cur[x]=i; int u=e[i].to; if(e[i].dis and dep[u]==dep[x]+1) { long long w=DFS(u,min(e[i].dis,flow-used)); used+=w; e[i].dis-=w;e[i^1].dis+=w; if(used==flow)return flow; } } if(!used)dep[x]=-1; return used; } queue<int>q; inline bool BFS() { for(R int i=0;i<=n;i++)cur[i]=head[i],dep[i]=-1; dep[s]=0; q.push(s); while(!q.empty()) { int u=q.front();q.pop(); for(R int i=head[u];i;i=e[i].nxt) if(e[i].dis and dep[e[i].to]==-1) dep[e[i].to]=dep[u]+1,q.push(e[i].to); } if(dep[end]==-1)return 0; return 1; } inline void work() { n=read<int>(),m=read<int>(),s=read<int>(),end=read<int>(); ll ans=0; int a,b; ll c; for(R int i=0;i<m;i++)a=read<int>(),b=read<int>(),c=read<ll>(),add(a,b,c); while(BFS()) ans+=DFS(s,INF); printf("%lld\n",ans); } } signed main() { ysr::work(); return 0; }
|