Pagini recente » Cod sursa (job #3289141) | Cod sursa (job #716967) | Cod sursa (job #3174184) | Cod sursa (job #1945285) | Cod sursa (job #2749705)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
using VI = vector<int>;
const int Inf = 0x3f3f3f3f;
const int MAX = 1005;
class Graph{
public:
int getMaxFlow()
{
int ft = 0;
for ( ; BF(); )
for ( const auto& x : G[N] )
{
if ( viz[x] == 0 || C[x][N] == f[x][N] )
continue;
int flow = Inf;
t[N] = x;
for ( int i = N; i != 1; i = t[i] )
flow = min( flow, C[t[i]][i] - f[t[i]][i] );
ft += flow;
if ( flow == 0 ) continue;
for ( int i = N; i != 1; i = t[i] )
{
f[t[i]][i] += flow;
f[i][t[i]] -= flow;
}
}
return ft;
}
bool BF()
{
queue<int> Q;
t = viz = vector<int>(N + 1, 0);
viz[1] = 1;
Q.push(1);
while ( !Q.empty() )
{
int nod = Q.front();
Q.pop();
if ( nod == N ) continue;
for ( const auto& v : G[nod] )
{
if ( C[nod][v] == f[nod][v] || viz[v] ) continue;
viz[v] = true;
t[v] = nod;
Q.push(v);
}
}
return viz[N];
}
public:
int N;
int C[MAX][MAX];
int f[MAX][MAX];
vector<vector<int>> G;
VI viz, t;
};
int M;
VI viz, t;
Graph graph;
void Read();
bool BF();
int main()
{
Read();
fout << graph.getMaxFlow() << '\n';
fin.close();
fout.close();
return 0;
}
void Read()
{
int x, y, z;
fin >> graph.N >> M;
graph.G = vector<vector<int>>(graph.N + 1);
while ( M-- )
{
fin >> x >> y >> z;
graph.G[x].push_back(y);
graph.G[y].push_back(x);
graph.C[x][y] = z;
}
}