Pagini recente » Cod sursa (job #2018926) | Cod sursa (job #3191769) | Cod sursa (job #2076377) | Cod sursa (job #1100841) | Cod sursa (job #2386948)
#include<bits/stdc++.h>
#define N 1010
using namespace std;
//without pointers //test complexity //
const int inf=1e9;
struct edge {
int x,c,op;
};
vector<edge>V[N];
int n,m,d[N], rs;
queue<int>Q;
bool BFS(int x) {
for (int i=0; i<=n; ++i) d[i]=-1;
Q.push(x); d[x]=0;
while (Q.size()) {
int x=Q.front(); Q.pop();
for (auto it:V[x]) {
int y=it.x, c=it.c, op=it.op;
if (d[y]==-1 && c>0) {
d[y]=d[x]+1;
if (y!=n) Q.push(y);
}
}
}
return (d[n]!=-1);
}
int DFS(int x,int flow) {
if (x==n) return flow;
int sentflow=0;
for (int i=0; i<V[x].size(); ++i) {
int y=V[x][i].x;
int c=V[x][i].c;
int op=V[x][i].op;
if (d[y]==d[x]+1 && c>0) {
int f2=DFS(y,min(c,flow));
if (f2) {
V[x][i].c-=f2;
V[y][op].c+=f2;
flow-=f2;
sentflow+=f2;
if (!V[x][i].c) return sentflow;
}
}
}
return sentflow;
}
void Dinic(int s, int t) {
int blockflow=0;
while (BFS(s)) {
while ((blockflow=DFS(s,inf))) {
rs+=blockflow;
}
}
}
int main() {
ifstream cin("maxflow.in");
ofstream cout("maxflow.out");
cin>>n>>m;
for (int i=1; i<=m; ++i) {
int x,y,c; cin>>x>>y>>c;
V[x].push_back({y,c,(int)V[y].size()});
V[y].push_back({x,0,(int)V[x].size()-1});
}
Dinic(1,n);
cout<<rs;
return 0;
}