Pagini recente » Istoria paginii runda/pregatire_nationala | Cod sursa (job #1728487) | Cod sursa (job #2110239) | Profil vexxato | Cod sursa (job #2753691)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("t.in");
ofstream fout("t.out");
#define NMAX 1005
#define INF (1<<30)
vector<int>G[NMAX];
int F[NMAX][NMAX], C[NMAX][NMAX], TT[NMAX], viz[NMAX];
int n, m;
queue<int>Q;
int bfs() {
int nc;
memset(viz, 0, sizeof(viz));
Q.push(1);
viz[1] = 1;
for (; !Q.empty(); Q.pop()) {
nc = Q.front();
if (nc == n) continue;
for (int nu : G[nc]) {
if (!viz[nu] && F[nc][nu] < C[nc][nu]) {
viz[nu] = 1;
Q.push(nu);
TT[nu] = nc;
}
}
}
return viz[n];
}
int main() {
int i, x, y, z, flow=0, MIN, nc;
fin >> n >> m;
for (i = 0; i < m; i++) {
fin >> x >> y >> z;
G[x].push_back(y);
G[y].push_back(x);
C[x][y] = z;
}
while (bfs()) {
for (int np : G[n]) {
if (!viz[np] || F[np][n] == C[np][n]) continue;
TT[n] = np;
MIN = INF;
for (nc = n; nc != 1; nc = TT[nc])
MIN = min(MIN, C[TT[nc]][nc] - F[TT[nc]][nc]);
if (MIN == 0) continue;
for (nc = n; nc != 1; nc = TT[nc]) {
F[TT[nc]][nc] += MIN;
F[nc][TT[nc]] -= MIN;
}
flow += MIN;
}
}
fout << flow;
}