Pagini recente » Cod sursa (job #2539681) | Cod sursa (job #1564782) | Cod sursa (job #463177) | Cod sursa (job #1975342) | Cod sursa (job #2701323)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int N = 1005;
int n, m;
int cap[N][N], flow[N][N], t[N];
vector<bool> vis;
vector<int> g[N];
void bfs(int startNode)
{
vis.assign(n + 1, false);
queue<int> q;
q.push(startNode);
vis[startNode] = true;
t[startNode] = 0;
while(!q.empty())
{
int node = q.front();
q.pop();
if(node == n)
continue;
for(int y : g[node])
if(cap[node][y] != flow[node][y])
{
if(!vis[y])
{
vis[y] = true;
t[y] = node;
q.push(y);
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 0; i < m; i++)
{
int x, y, c;
fin >> x >> y >> c;
g[x].push_back(y);
g[y].push_back(x);
cap[x][y] = c;
}
int fmax = 0;
do
{
bfs(1);
if(vis[n] == false)
break;
for(int y : g[n])
{
if(vis[y] == false || cap[y][n] == flow[y][n])
continue;
t[n] = y;
int fmin = 1 << 30;
for(int node = n; node != 1; node = t[node])
fmin = min(fmin, cap[t[node]][node] - flow[t[node]][node]);
if(fmin == 0)
continue;
fmax += fmin;
for(int node = n; node != 1; node = t[node])
{
flow[t[node]][node] += fmin;
flow[node][t[node]] -= fmin;
}
}
} while(vis[n]);
fout << fmax << '\n';
fin.close();
fout.close();
return 0;
}
/*
*/