Pagini recente » Cod sursa (job #3217345) | Cod sursa (job #1166448) | Cod sursa (job #328205) | Cod sursa (job #2600579) | Cod sursa (job #1016761)
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int nmax = int(1e3) + 2;
int N, M;
int F[nmax][nmax];
int cap[nmax][nmax];
int T[nmax];
vector<int> G[nmax];
bool visited[nmax];
int Q[nmax];
void readData() {
scanf("%d %d",&N,&M);
int a, b, c;
for(int i = 0;i < M;i++) {
scanf("%d %d %d",&a,&b,&c);
cap[a][b] += c;
G[a].push_back(b);
G[b].push_back(a);
}
}
inline bool bfs(const int &source,const int &destination) {
int L, R;
L = R = 0;
memset(visited,0,sizeof(visited));
T[source] = source;
visited[source] = true;
Q[R++] = source;
while(L < R) {
int v = Q[L++];
if(v == destination) continue;
for(const int &w : G[v]) {
if(!visited[w] && F[v][w] < cap[v][w]) {
visited[w] = true;
T[w] = v;
Q[R++] = w;
}
}
}
return visited[destination];
}
int getFlow() {
int ret = 0;
while(bfs(1,N)) {
for(const int &v : G[N]) {
if(!visited[v] || cap[v][N] == F[v][N]) continue;
T[N] = v;
int fmax = int(1e9);
for(int w = v;w != T[w];w = T[w]) {
fmax = min(fmax,cap[T[w]][w] - F[T[w]][w]);
}
if(!fmax) continue;
ret += fmax;
for(int w = v;w != T[w];w = T[w]) {
F[T[w]][w] += fmax;
F[w][T[w]] -= fmax;
}
}
}
return ret;
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
readData();
printf("%d\n",getFlow());
return 0;
}