Pagini recente » Cod sursa (job #1925089) | Cod sursa (job #1365150) | Cod sursa (job #519703) | Cod sursa (job #2364149) | Cod sursa (job #1412884)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#define INF 0x3f3f3f3f
using namespace std;
ifstream is("maxflow.in");
ofstream os("maxflow.out");
using VI = vector<int>;
using VVI = vector<VI>;
int n, m;
bitset<1001> ok;
VI t;
VVI g, c;
void Read();
bool Bfs();
int main()
{
Read();
t = VI(n + 1);
int answ = 0, flow;
while ( Bfs() )
for ( const auto &nodv : g[n] )
{
if ( !ok[nodv] || c[nodv][n] <= 0 )
continue;
flow = INF;
t[n] = nodv;
for ( int x = n; t[x]; x = t[x] )
flow = min(flow, c[t[x]][x]);
if ( !flow )
continue;
answ += flow;
for ( int x = n; t[x]; x = t[x] )
{
c[t[x]][x] -= flow;
c[x][t[x]] += flow;
}
}
os << answ;
is.close();
os.close();
}
bool Bfs()
{
ok.reset();
queue<int> q;
q.push(1);
ok[1] = 1;
int nod;
while ( !q.empty() )
{
nod = q.front();
q.pop();
if ( nod == n )
continue;
for ( const auto &nodv : g[nod] )
if ( !ok[nodv] && c[nod][nodv] > 0 )
{
ok[nodv] = 1;
t[nodv] = nod;
q.push(nodv);
}
}
return ok[n];
}
void Read()
{
is >> n >> m;
g = VVI(n + 1);
c = VVI(n + 1, VI(n + 1));
int n1, n2;
while ( m-- )
{
is >> n1 >> n2;
g[n1].push_back(n2);
g[n2].push_back(n1);
is >> c[n1][n2];
}
}