Pagini recente » Cod sursa (job #2354722) | Cod sursa (job #464317) | Cod sursa (job #2566251) | Cod sursa (job #2745000) | Cod sursa (job #3201408)
#include <bits/stdc++.h>
#define N 104
using namespace std;
ifstream fin("data.in");
ofstream fout("data.out");
int n, m;
struct muchie{
int f, c;
int type;
} M[N][N];
vector<int> g[N];
void Citire()
{
int i;
int x, y, c;
fin >> n >> m;
n++;
for( i=1; i<=m; i++ ){
fin >> x >> y >> c;
g[x].push_back(y); M[x][y] = {0, c, +1};
g[y].push_back(x); M[y][x] = {0, c, -1};
}
}
bool ok;
int Dad[N];
bool viz[N];
queue<int> Q;
void BFS()
{
while(!Q.empty())Q.pop();
for( int i=0; i<=n; i++ )
Dad[i] = -1, viz[i] = 0;
Dad[0] = 0;
Q.push(0);
while( !Q.empty() )
{
int nod = Q.front();
Q.pop();
if( nod == n ) ok = 0;
for( auto next : g[nod] )if( !viz[next] ){
if( (M[nod][next].type < 0 && M[nod][next].f == 0) ||
(M[nod][next].type > 0 && M[nod][next].f == M[nod][next].c) )
continue;
//cout << next << " ";
viz[nod] = 1;
Dad[next] = nod;
Q.push(next);
}
}
}
void Revizuire( int x, int &mn )
{
if( x == 0 ) return;
int type = M[ Dad[x] ][x].type;
int f = M[ Dad[x] ][x].f;
int c = M[ Dad[x] ][x].c;
mn = min( mn, (type > 0) ? c - f : f );
Revizuire( Dad[x], mn );
M[ Dad[x] ][x].f += type*mn;
}
void Afisare( int x )
{
if( x == 0 ) return;
Afisare( Dad[x] );
cout << Dad[x] << "->" << x << ", ";
cout << M[ Dad[x] ][x].f << "/";
cout << M[ Dad[x] ][x].c << ", ";
cout << M[ Dad[x] ][x].type << "\n";
}
void Rezolvare()
{
int i;
///Ford_Fulkerson
ok = 0;
while(!ok)
{
ok = 1;
BFS();
//cout << "flori\n";
int mn = 2e9;
if(!ok)Revizuire( n, mn );
//if(!ok)Afisare( n );
//cout << "\n";
//cout << "\n";
}
int F = 0;
for( auto next : g[0] ) F += M[0][next].f;
fout << F;
}
int main()
{
Citire();
Rezolvare();
return 0;
}