Pagini recente » Cod sursa (job #3229507) | Cod sursa (job #826902) | Cod sursa (job #1109360) | Cod sursa (job #1183173) | Cod sursa (job #2242323)
#include <fstream>
#include <cstring>
#include <vector>
std::ifstream cin("maxflow.in");
std::ofstream cout("maxflow.out");
using namespace std;
#define maxn 1005
#define inf 120000
#define min(a,b) a<b?a:b
vector <int> adj[maxn];
int C[maxn][maxn]; //capacitate, in F flux folosit
int F[maxn][maxn];
int viz[maxn], N,M, ant[maxn],maxflow;
void citire(){
int x,y,z;
cin>>N>>M;
for(;M--;){
cin>>x>>y>>z;
adj[x].push_back(y);
adj[y].push_back(x);
C[x][y] +=z;
}
}
int BF_flux(){
int u[maxn],plc, nod;
u[0]=1; u[1]=1; //initializam lista
memset(viz,0,sizeof(viz));
viz[1]=1;
for(int i=1;i<=u[0];i++){
nod=u[i];
for(unsigned int j=0;j<adj[nod].size();j++){
plc=adj[nod][j];
if(viz[plc] || F[nod][plc]==C[nod][plc] )
continue;
viz[plc]=1;
ant[plc]=nod;
u[++u[0]]=plc;
if(N==plc) return 1;
}
}
return 0;
}
int main()
{
citire();
int minflow;
for(maxflow=0;BF_flux();maxflow+=minflow){
minflow=inf;
for(int i=N;i!=1;i=ant[i])
minflow=min(minflow,C[ant[i]][i] - F[ant[i]][i]);
for(int i=N;i!=1;i=ant[i]){
F[ant[i]][i] += minflow;
F[i][ant[i]] -= minflow;
}
}
cout<<maxflow;
return 0;
}