Pagini recente » Cod sursa (job #409534) | Cod sursa (job #233771) | Cod sursa (job #1203892) | Cod sursa (job #3229925) | Cod sursa (job #2396159)
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=1e3+50;
const int mod=1e9+7;
using namespace std;
int x,y,flow,nw,pr[nmax],cp[nmax][nmax],n,i,m,z;
vector<int>a[nmax];
int bfs()
{
int i,x,y,fl,f;
for(i=1;i<=n;i++)pr[i]=-1;
pr[1]=-2;
queue<pair<int,int> >q;
q.push({1,1e9});
while(!q.empty())
{
fl=q.front().sc;
x=q.front().fr;
q.pop();
for(i=0;i<a[x].size();i++)
{
y=a[x][i];
if(pr[y]==-1 && cp[x][y])
{
f=min(fl,cp[x][y]);
pr[y]=x;
if(y==n)return f;
q.push({y,f});
}
}
}
return 0;
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>m;
while(m--)
{
cin>>x>>y>>z;
a[x].pb(y);
a[y].pb(x);
cp[x][y]=z;
}
while(nw=bfs())
{
flow+=nw;
x=n;
while(x!=1)
{
cp[pr[x]][x]-=nw;
cp[x][pr[x]]+=nw;
x=pr[x];
}
}
cout<<flow<<endl;
return 0;
}