Pagini recente » Cod sursa (job #183919) | Cod sursa (job #1217150) | Cod sursa (job #1741424) | Cod sursa (job #171473) | Cod sursa (job #2175944)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <ctime>
#include <unordered_map>
#include <iomanip>
#include <complex>
#include <cassert>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define deb(a) cerr<< #a << "= " << (a)<<"\n";
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;
typedef complex<double> base;
typedef vector<int> vi;
typedef pair<int,int> pii;
template<class T> ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream; }
ll fpow(ll x, ll p, ll m){ll r=1; for (;p;p>>=1){ if (p&1) r=r*x%m; x=x*x%m; } return r;}
ll inv(ll a, ll b){ return a<2 ? a : ((a-inv(b%a,a))*b+1)/a%b; }
int gcd(int a, int b){ if (!b) return a; return gcd(b,a%b);}
ll gcd(ll a, ll b){ if (!b) return a; return gcd(b,a%b);}
int N,M,S,D,C[1010][1010],F[1010][1010],d[1010],ptr[1010];
vi g[1010];
bool bfs(){
vi q;
memset(d,0,sizeof(d));
q.pb(S);
d[S]=1;
for (int i=0; i<q.size(); i++){
int nod=q[i];
for (int nxt : g[nod])
if (C[nod][nxt]-F[nod][nxt]>0 && !d[nxt]){
d[nxt]=d[nod]+1;
q.pb(nxt);
}
}
return d[D]!=0;
}
int dfs(int nod, int val){
if (val==0) return 0;
if (nod==D) return val;
for (int &i=ptr[nod]; i<g[nod].size(); i++){
int nxt=g[nod][i];
if (C[nod][nxt]-F[nod][nxt]>0 && d[nxt]==d[nod]+1){
int nval=dfs(nxt,min(val,C[nod][nxt]-F[nod][nxt]));
if (nval>0){
F[nod][nxt]+=nval;
F[nxt][nod]-=nval;
return nval;
}
}
}
return 0;
}
int dinic(){
int flow=0;
while (bfs()){
memset(ptr,0,sizeof(ptr));
while (int v=dfs(S,(1<<30))){
flow+=v;
}
}
return flow;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
fin >> N >> M;
S=1,D=N;
for (int i=1; i<=M; i++){
int x,y,c;
fin >> x >> y >> c;
g[x].pb(y);
g[y].pb(x);
C[x][y]=c;
}
fout << dinic() << "\n";
return 0;
}