Pagini recente » Cod sursa (job #1331373) | Cod sursa (job #2136176) | Cod sursa (job #995255) | Sandbox (cutiuţa cu năsip) | Cod sursa (job #2430649)
#include <iostream>
#include <list>
#include <queue>
#include <fstream>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int NMAX = 1005, MMAX = 5005;
const int inf = 2147000000;
int n, m, c[NMAX][NMAX], f[NMAX][NMAX], a[NMAX], viz[NMAX];
list<int> g[NMAX];
list<int>::iterator it;
bool bfs()
{
int x, y;
queue<int> q; q.push(1);
for(int i = 1; i <= n; i++) viz[i] = 0;
while(!q.empty()){
x = q.front(); q.pop();
if(x == n) continue;
for(it = g[x].begin(); it != g[x].end(); it++){
y = *it;
if(!viz[y] && c[x][y] > f[x][y]){
viz[y] = 1; q.push(y);
a[y] = x;
}
}
}
return viz[n];
}
int main()
{
int i, x, y, cs, flow = 0, plusflow;
fin >> n >> m;
for(i = 1; i <= m; i++){
fin >> x >> y >> cs;
g[x].push_back(y);
g[y].push_back(x);
c[x][y] = cs;
}
while(bfs()){
for(it = g[n].begin(); it != g[n].end(); it++){
x = *it;
if(!viz[x] || c[x][n] == f[x][n]) continue;
a[n] = x;
plusflow = inf;
for(x = n; x != 1; x = a[x])
plusflow = min(plusflow, c[a[x]][x] - f[a[x]][x]);
if(plusflow == 0) continue;
for(x = n; x != 1; x = a[x])
f[a[x]][x] += plusflow,
f[x][a[x]] -= plusflow;
flow += plusflow;
}
}
fout << flow;
return 0;
}