Pagini recente » Cod sursa (job #2774179) | Cod sursa (job #263365) | Profil UWUDavidUWU | Cod sursa (job #370389) | Cod sursa (job #2874350)
#include <fstream>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#define Nmax 1005
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
typedef vector <int> VI;
int n, m, x, y, c;
const int oo = 1 << 28;
bool Fr[Nmax];
int F[Nmax][Nmax], C[Nmax][Nmax], Dad[Nmax];
queue <int> Q;
VI V[Nmax];
bool BFS(int vertex){
while(Q.size())Q.pop();
Q.push(vertex);
memset(Fr, 0, sizeof(Fr));
Fr[vertex] = 1;
while(Q.size()){
vertex = Q.front();
if(vertex == n)
break;
for(int new_vertex : V[vertex])
if(!Fr[new_vertex] && F[vertex][new_vertex] != C[vertex][new_vertex]){
Fr[new_vertex] = 1;
Dad[new_vertex] = vertex;
Q.push(new_vertex);
}
Q.pop();
}
return Fr[n];
}
int main() {
fin >> n >> m;
for(int i = 1; i <= m; ++i){
fin >> x >> y >> c;
V[x].push_back(y);
V[y].push_back(x);
C[x][y] += c;
}
int flow = 0;
while( BFS(1) )
for(int vertex : V[n]){
if(C[vertex][n] == F[vertex][n] || !Fr[vertex])
continue;;
Dad[n] = vertex;
int fmin = oo;
for(int node = n; node > 1; node = Dad[node])
fmin = min(fmin, C[Dad[node]][node] - F[Dad[node]][node]);
if(fmin == 0)
continue;
for(int node = n; node > 1; node = Dad[node]){
F[Dad[node]][node] += fmin;
F[node][Dad[node]] -= fmin;
}
flow += fmin;
}
fout << flow;
return 0;
}