Pagini recente » Cod sursa (job #2763028) | Cod sursa (job #1787107) | Cod sursa (job #549889) | Cod sursa (job #1982533) | Cod sursa (job #1439073)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int NMax = 1010;
const int INF = 1e9;
int n,m;
int TT[NMax];
int C[NMax][NMax],F[NMax][NMax],cd[NMax],viz[NMax];
vector < int > G[NMax];
int bfs(){
int node,V;
cd[0] = cd[1] = 1;
memset(viz, 0, sizeof(viz));
viz[1] = 1;
for(int i = 1; i <= cd[0]; i++){
node = cd[i];
for(int j = 0; j < G[node].size(); j++){
V = G[node][j];
if(C[node][V] != F[node][V] && !viz[V]){
viz[V] = 1;
cd[++cd[0]] = V;
TT[V] = node;
if(V == n){
return 1;
}
}
}
}
return 0;
}
int main()
{
int x,y,z,fmin,flow;
fin >> n >> m;
for(int i = 1; i <= m; i++){
fin >> x >> y >> z;
C[x][y] += z;
G[x].push_back(y);
G[y].push_back(x);
}
for(flow = 0; bfs(); flow += fmin){
fmin = INF;
for(int node = n; node != 1; node = TT[node]){
fmin = min(fmin, C[TT[node]][node] - F[TT[node]][node]);
}
for(int node = n; node != 1; node = TT[node]){
F[TT[node]][node] += fmin;
F[node][TT[node]] -= fmin;
}
}
fout << flow;
return 0;
}