照着oi-wiki上写的,91pts,TLE on 9
code:
#include <bits/stdc++.h>
using namespace std;
#define F(i,j,k) for (signed i=signed(j);i<=signed(k);i++)
#define endl '\n'
#define ckmax(a,b) a=max(a,b)
#define ckmin(a,b) a=min(a,b)
#define DEBUG
#ifdef DEBUG
template<typename T>void dbg(const T& t){cerr<<t<<endl;}
template<typename T,typename... Args>void dbg(const T& t,const Args&...r){cerr<<t<<",";dbg(r...);}
#define debug(...) {cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<" = ";dbg(__VA_ARGS__);}
#else
#define debug(...)
#endif
#define int long long
const int maxn=250;
struct Edge{
int x,y;
long long c,f;
};
int n,m,s,t,x,y,z;
struct Dinic{
vector<Edge> edge;
vector<int> v[maxn];
long long d[maxn],c[maxn];
bool vis[maxn];
void add(int x,int y,int c){
edge.push_back({x,y,c,0});
edge.push_back({y,x,0,0});
v[x].push_back(edge.size()-2);
v[y].push_back(edge.size()-1);
}
bool bfs(){
memset(vis,0,sizeof vis);
queue<int> q;
q.push(s);
d[s]=0,vis[s]=1;
while(!q.empty()){
int f=q.front();
q.pop();
for(auto i:v[f]){
Edge& e=edge[i];
if(!vis[e.y]&&e.c>e.f){
vis[e.y]=1;
d[e.y]=d[f]+1;
q.push(e.y);
}
}
}
return vis[t];
}
long long dfs(int x,long long a){
if(x==t||a==0) return a;
long long flow=0,f=0;
F(i,c[x],v[x].size()-1){
Edge& e=edge[v[x][i]];
if(d[x]+1==d[e.y]&&(f=dfs(e.y,min(a,e.c-e.f)))>0){
e.f+=f;
edge[v[x][i]^1].f-=f;
flow+=f;
a-=f;
if(!a) break;
}
}
return flow;
}
long long maxflow(){
long long f=0;
while(bfs()){
memset(c,0,sizeof c);
f+=dfs(s,LLONG_MAX/3);
}
return f;
}
}dinic;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>s>>t;
F(i,1,m) cin>>x>>y>>z,dinic.add(x,y,z);
cout<<dinic.maxflow()<<endl;
return 0;
}