Pagini recente » Cod sursa (job #1569771) | Cod sursa (job #2507769) | Cod sursa (job #2149567) | Cod sursa (job #2147515) | Cod sursa (job #1757261)
#include <bits/stdc++.h>
#define for0(i,n) for(int i=0; i<n; i++)
#define for1(i,n) for(int i=1; i<=n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define clr(A,x) memset(A, x, sizeof(A))
#define cpy(A,B) memcpy(A, B, sizeof(B))
#define g(s) getline(cin, s) ///ai grija la fin/cin ///
#define FASTIO ios_base::sync_with_stdio(0)
const long long INFLL = (1LL<<62);
const int INFINT = 2000000000;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
/*template <typename T>
string to_string(const T& n){
ostringstream os;
os << n;
return os.str();
}
*/
/*struct coord
{
int x,y;
};
bool operator<(const coord &l, const coord &r)
{
return (l.x<r.x || (l.x==r.x && l.y<r.y));
}*/
/*void invers_modular(int a, int b, int& d, int& x, int & y)
{
if(!b)
{
d=a;
x=1;
y=0;
return ;
}
int x0, y0;
invers_modular(b, a%b, d, x0, y0);
x=y0;
y=x0-a/b*y0;
} // daca x<0 se aduna cu mod pana e mai mare, x fiind rezultatul*/
/*ull p(int baze, int exponent)
{
if(exponent==0)
return 1;
if(exponent%2==0)
return p(baze*baze, exponent/2);
else return baze*p(baze, exponent-1);
}*/
ifstream fin("maxflow.in"); /// modifica cu numele corespunzator
ofstream fout("maxflow.out"); /// modifica cu numele corespunzator
const int NMAX = 1001;
int n, m;
vector<vector<int> > graf(NMAX);
int cap[NMAX][NMAX];
int flx[NMAX][NMAX];
int tata[NMAX];
void citire();
bool bfs();
queue<int> coada;
bool viz[NMAX];
void rezolvare();
int main()
{
citire();
rezolvare();
return 0;
}
void citire()
{
fin>>n>>m;
for1(i,m)
{
int a, b;
fin>>a>>b;
graf[a].pb(b);
graf[b].pb(a);
fin>>cap[a][b];
}
}
bool bfs()
{
coada.push(1);
clr(viz, 0);
viz[1] = 1;
while(!coada.empty())
{
int prev = coada.front();
coada.pop();
for(auto i: graf[prev])
if(!viz[i] && cap[prev][i] > flx[prev][i])
{
tata[i] = prev;
coada.push(i);
viz[i] = 1;
}
}
return viz[n];
}
void rezolvare()
{
int sol = 0;
while(bfs())
for(auto i: graf[n])
if(viz[i] && cap[i][n] > flx[i][n])
{
int fmin = INFINT;
tata[n] = i;
for(int j = n; j != 1; j = tata[j])
fmin = min(cap[tata[j]][j] - flx[tata[j]][j], fmin);
if(fmin > 0)
for(int j = n; j != 1; j = tata[j])
{
flx[tata[j]][j] += fmin;
flx[j][tata[j]] -= fmin;
}
sol += fmin;
}
fout<<sol<<'\n';
}