Pagini recente » Cod sursa (job #1543598) | Cod sursa (job #1873530) | Cod sursa (job #1884441) | Cod sursa (job #711274) | Cod sursa (job #2297744)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
int n, m, answer;
int previous[1005];
int flux[1005][1005];
int capacity[1005][1005];
bitset <1005> f;
vector <int> graph[1005];
void reset()
{
f.reset();
for ( int i = 1; i <= n; ++i )
previous[i] = 0;
}
bool maximumFlux ( int source )
{
queue <int> solutions;
f[source] = 1;
solutions.push(source);
while ( solutions.size() )
{
int node = solutions.front();
solutions.pop();
if ( node == n )
continue;
for ( auto x:graph[node] )
{
if ( !f[x] && flux[node][x] < capacity[node][x] )
{
previous[x] = node;
f[x] = 1;
solutions.push(x);
}
}
}
return f[n];
}
int main()
{
ios::sync_with_stdio(false);
fin.tie(0); fout.tie(0);
fin>>n>>m;
for ( int i = 1; i <= m; ++i )
{
int x, y, flucsuwu;
fin>>x>>y>>flucsuwu;
graph[x].push_back(y);
graph[y].push_back(x);
capacity[x][y] += flucsuwu;
}
while ( maximumFlux(1) )
{
for ( auto x:graph[n] )
{
if ( f[x] && capacity[x][n] > flux[x][n] )
{
previous[n] = x;
int aux = n;
int minimum = 1e9;
while ( aux != 1 )
{
minimum = min(minimum, capacity[previous[aux]][aux]-flux[previous[aux]][aux]);
aux = previous[aux];
}
aux = n;
while ( aux != 1 )
{
flux[previous[aux]][aux] += minimum;
flux[aux][previous[aux]] -= minimum;
aux = previous[aux];
}
answer += minimum;
}
}
reset();
}
fout<<answer<<'\n';
}