Pagini recente » Cod sursa (job #2488296) | Cod sursa (job #1204485) | Cod sursa (job #1171744) | Cod sursa (job #1413247) | Cod sursa (job #1161918)
#include <cstdio>
#include <vector>
#include <queue>
#define nmax 10005
using namespace std;
vector <int> g[nmax];
queue <int> q;
int cap[nmax][nmax];
int flow[nmax][nmax];
int t[nmax];
int n,m;
int maxflow;
bool bfs() {
bool found = false;
memset(t,0,nmax*4);
t[1] = -1;
q.push(1);
while (!q.empty()) {
int ct = q.front(); q.pop();
for (vector <int> :: iterator it = g[ct].begin();it != g[ct].end();it++) {
if (cap[ct][*it] - flow[ct][*it] > 0 && t[*it] == 0) {
if (*it != n) {
q.push(*it);
t[*it] = ct;
} else {
found = true;
}
}
}
}
return found;
}
int getMin(int x) {
if (t[x] != -1) {
int newMin = getMin(t[x]);
return minim(newMin,cap[t[x]][x] - flow[t[x]][x]);
}
}
void update(int x,int value) {
if (t[x] != -1) {
flow[t[x]][x] += value;
flow[x][t[x]] -= value;
update(t[x],value);
}
}
int main() {
freopen("maxflow.in","r",stdin);
froepen("maxflow.out","w",stdout);
scanf("%d %d",&n,&m);
for (int i=1;i<=m;i++) {
int a,b,c;
scanf("%d %d %d\n",&a,&b,&c);
g[a].push_back(b);
g[b].push_back(a);
cap[a][b] = c;
}
while (bfs()) {
for (vector <int> :: iterator it = g[n].begin(); it != g[n].end();it++) {
int min = getMin(*it);
maxflow += min;
update(*it,min);
}
}
printf("%d\n",maxflow);
return 0;
}