Pagini recente » Cod sursa (job #2493341) | Cod sursa (job #1389489) | Cod sursa (job #1823693) | Cod sursa (job #2641941) | Cod sursa (job #2419014)
#include <bits/stdc++.h>
#define nmax 1005
#define inf 2000000000
using namespace std;
vector<int>g[nmax];
int cost[nmax][nmax], currFlow[nmax][nmax];
int drum[nmax];
int n, m, x, y, fl, flow, add, act;
bool vz[nmax];
bool bfs()
{
memset(vz, 0, sizeof(vz));
queue<int>q;
vz[1] = true;
q.push(1);
while(!q.empty())
{
act = q.front();
q.pop();
for (auto x:g[act])
{
if (cost[act][x] == currFlow[act][x]) continue;
if (vz[x]) continue;
vz[x] = true;
drum[x] = act;
q.push(x);
if (x == n) return true;
}
}
return false;
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d %d",&n,&m);
for (int i=1;i<=m;++i)
{
scanf("%d %d %d",&x,&y,&fl);
g[x].push_back(y);
g[y].push_back(x);
cost[x][y] = fl;
}
for (flow = 0; bfs(); flow+=add)
{
add = inf;
for (int nod = n; nod > 1; nod = drum[nod])
add = min(add, cost[drum[nod]][nod] - currFlow[drum[nod]][nod]);
for (int nod = n; nod > 1; nod = drum[nod])
{
currFlow[drum[nod]][nod] += add;
currFlow[nod][drum[nod]] -= add;
}
}
printf("%d\n",flow);
fclose(stdin);
fclose(stdout);
return 0;
}