Pagini recente » Cod sursa (job #2772752) | Cod sursa (job #635171) | Cod sursa (job #2683605) | Cod sursa (job #832626) | Cod sursa (job #2243236)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
std::ifstream in("maxflow.in");
std::ofstream out("maxflow.out");
using namespace std;
const int LIM = 1024;
vector < int > myVector[LIM+1];
queue < int > myQueue;
int theFlux[LIM+1][LIM+1];
int theAmount[LIM+1][LIM+1];
bool beenThere[LIM+1];
int theRoad[LIM+1];
int N,M,theAnswer;
bool BFS()
{
memset(beenThere,false,sizeof(beenThere));
beenThere[1] = true;
myQueue.push(1);
while(!myQueue.empty())
{
int node = myQueue.front();
myQueue.pop();
if(node == N) continue;
for ( auto V : myVector[node])
{
if(theAmount[node][V] > theFlux[node][V] && !beenThere[V])
{
beenThere[V] = true;
myQueue.push(V);
theRoad[V] = node;
}
}
}
return beenThere[N];
}
int main()
{
in >> N >> M;
for ( int i = 1; i <= M ; ++i)
{
int x,y,z;
in >> x >> y >> z;
theAmount[x][y]=z;
myVector[x].push_back(y);
myVector[y].push_back(x);
}
while(BFS())
{
for ( int i = 0 ; i < myVector[N].size() ; i++)
{
int V = myVector[N][i];
if(theAmount[V][N] > theFlux[V][N] && beenThere[V])
{
theRoad[N] = V;
int fMin = INF;
for ( int k = N ; k!= 1 ; k = theRoad[k])
fMin = min(fMin , theAmount[theRoad[k]][k] - theFlux[theRoad[k]][k]);
for( int k = N ; k != 1 ; k = theRoad[k])
{
theFlux[theRoad[k]][k] += fMin;
theFlux[k][theRoad[k]] -= fMin;
}
theAnswer+=fMin;
}
}
}
out << theAnswer;
}