Dinic+当前弧优化,复杂度上界为O(V^2*E),NOIP应该还不至于卡这个而支持ISAP吧。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1e4+4,M=1e5+4,INF=0x3f3f3f3f;
int n,m,source,sink;
int head[N],etot=0;
struct EDGE {
int v,nxt,w,r;
}e[M<<1];
int dis[N],cur[N];
inline int read() {
int x=0;char c=getchar();
while (c<'0'||c>'9') c=getchar();
while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x;
}
inline void adde(int u,int v,int r) {
e[etot].nxt=head[u],e[etot].v=v,e[etot].r=r,head[u]=etot++;
e[etot].nxt=head[v],e[etot].v=u,e[etot].r=0,head[v]=etot++;
}
inline bool bfs() {
queue<int > q;
memset(dis,-1,sizeof(dis));
q.push(source),dis[source]=0;
while (!q.empty()) {
int p=q.front();
q.pop();
for (int i=head[p];~i;i=e[i].nxt) {
int v=e[i].v;
if (e[i].r>0&&dis[v]==-1)
q.push(v),dis[v]=dis[p]+1;
}
}
return ~dis[sink];
}
int dfs(int p,int low) {
if (p==sink||low==0) return low;
int cost=0,flow;
for (int &i=cur[p];~i;i=e[i].nxt) {
int v=e[i].v;
if (e[i].r>0&&dis[v]==dis[p]+1&&(flow=dfs(v,min(low,e[i].r)))) {
e[i].r-=flow;
e[i^1].r-=flow;
cost+=flow;
low-=flow;
if (low==0) return cost;
}
}
if (low>0) dis[p]=-1;
return cost;
}
int main() {
// freopen("P3376.in","r",stdin);
memset(head,-1,sizeof(head));
n=read(),m=read(),source=read(),sink=read();
for (register int i=0;i<m;++i) {
int u=read(),v=read(),w=read();
adde(u,v,w);
}
int maxflow=0;
while (bfs()) {
for (int i=1;i<=n;++i) cur[i]=head[i];
int a;
while (a=dfs(source,INF)) maxflow+=a;
}
cout<<maxflow<<endl;
return 0;
}