Pagini recente » Cod sursa (job #179455) | Cod sursa (job #1823206) | Cod sursa (job #1262231) | Cod sursa (job #1318198) | Cod sursa (job #1626682)
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector<int> g[1002];
int c[1002][1002], f[1002][1002];
int q[1002], parent[1002];
int n;
bool bfs()
{
bool vis[n+1];
memset(vis, 0, sizeof(vis));
vis[1] = 1;
q[1] = 1;
int qsize = 1;
for (int i = 1; i <= qsize; i++)
{
int node = q[i];
for (unsigned j = 0; j < g[node].size(); j++)
if (!vis[g[node][j]] && c[node][g[node][j]] > f[node][g[node][j]])
{
vis[g[node][j]] = true;
q[++qsize] = g[node][j];
parent[g[node][j]] = node;
}
}
return vis[n];
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int m;
scanf("%d%d", &n, &m);
for (int i = 1, x, y, z; i <= m; i++)
{
scanf("%d%d%d", &x, &y, &z);
g[x].push_back(y);
g[y].push_back(x);
c[x][y] = z;
}
int flow = 0;
while (bfs())
{
int fmax = 0x3f3f3f3f;
for (int i = n; i != 1; i = parent[i])
fmax = min(fmax, c[parent[i]][i] - f[parent[i]][i]);
for (int i = n; i != 1; i = parent[i])
{
f[parent[i]][i] += fmax;
f[i][parent[i]] -= fmax;
}
flow += fmax;
}
printf("%d\n", flow);
return 0;
}