Pagini recente » Cod sursa (job #1244653) | Cod sursa (job #918123) | Cod sursa (job #3144845) | Cod sursa (job #2913279) | Cod sursa (job #2243205)
#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];
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;
if(V == N) return true;
}
}
return false;
}
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(); fLow += fMin)
{
fMin = INF;
for( nod = N ; nod != 1 ; nod = TT[nod])
fMin = min(fMin , theAmount[TT[nod]][nod] - theFlux[TT[nod]][nod]);
for ( nod = N ; nod != 1 ; nod = TT[nod])
{
theFlux[TT[nod]][nod] += fMin;
theFlux[nod][TT[nod]] -= fMin;
}
}
out << fLow;
}