Pagini recente » Cod sursa (job #654486) | Cod sursa (job #70726) | Cod sursa (job #458763) | Cod sursa (job #2601103) | Cod sursa (job #2243216)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
std::ifstream in("maxflow.in");
std::ofstream out("maxflow.out");
using namespace std;
const int LIM = 1024;
int theAmount[LIM + 1][LIM + 1];
int theFlux[LIM + 1][LIM + 1];
int cd[LIM + 1];
int TT[LIM+1];
bool beenThere[LIM+1];
vector < int > myVector[LIM + 1];
int N, M, fLow, fMin, x, y, z, nod;
bool BFS()
{
int i , j , nod , V;
cd[0] = 1;
cd[1] = 1;
memset( beenThere , false , sizeof(beenThere));
beenThere[1] = true;
for ( i = 1 ; i <= cd[0] ; i++)
{
nod = cd[i];
if(nod == N) continue;
for ( j = 0 ; j < myVector[nod].size() ; j++)
{
V = myVector[nod][j];
if(theAmount[nod][V] == theFlux[nod][V] || beenThere[V]) continue;
beenThere[V] = true;
cd[++cd[0]] = V;
TT[V] = nod;
}
}
return beenThere[N];
}
int main()
{
in >> N >> M;
for ( int i = 1; i <= M ; ++i)
{
in >> x >> y >> z;
theAmount[x][y] += z;
myVector[x].push_back(y);
myVector[y].push_back(x);
}
for(fLow = 0 ; BFS();)
{
for ( int i = 0 ; i < myVector[N].size() ; ++i)
{
nod = myVector[N][i];
if(theFlux[nod][N] == theAmount[nod][N] || !beenThere[nod]) continue;
TT[N] = nod;
fMin = INF;
for ( nod = N ; nod != 1 ; nod = TT[nod])
fMin = min(fMin , theAmount[TT[nod]][nod] - theFlux[TT[nod]][nod]);
if(fMin == 0) continue;
for ( nod = N ; nod != 1 ; nod = TT[nod])
{
theFlux[TT[nod]][nod] +=fMin;
theFlux[nod][TT[nod]] -= fMin;
}
fLow+=fMin;
}
}
out << fLow;
}