Pagini recente » Cod sursa (job #2477078) | Cod sursa (job #2460543) | Cod sursa (job #1285354) | Cod sursa (job #1521443) | Cod sursa (job #2272548)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
const int maxn = 1005;
vector <int> v[maxn];
bitset <maxn> viz;
int M[maxn][maxn];
int drum[maxn];
queue <int> q;
int minim;
void bfs(int nod)
{
for(int i = 1; i < maxn; i++)
drum[i] = 0;
viz.reset();
while(!q.empty())
q.pop();
q.push(nod);
viz[nod] = 1;
while(!q.empty())
{
int p = q.front();
q.pop();
for(auto it : v[p])
{
if(viz[it] || M[p][it] == 0)
continue;
viz[it] = 1;
drum[it] = p;
q.push(it);
}
}
}
int main()
{
int n, m;
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
in >> x >> y >> c;
v[x].push_back(y);
v[y].push_back(x);
M[x][y] = c;
}
int sol = 0;
viz[n] = 1;
while(viz[n] == 1)
{
viz[n] = 0;
bfs(1);
if(viz[n] == 1)
{
//cerr << "intra";
for(auto it : v[n])
{
if(viz[it] == 1 && M[it][n] > 0)
{
drum[n] = it;
minim = (1 << 30);
int last = n;
while(last != 1) {
minim = min(minim, M[drum[last]][last]);
last = drum[last];
}
last = n;
while(last != 1)
{
M[drum[last]][last] -= minim;
M[last][drum[last]] += minim;
last = drum[last];
}
sol = sol + minim;
}
}
}
}
out << sol << "\n";
return 0;
}