Cod sursa(job #2181777)

Utilizator cris90robert@yahoo.comseretan cristian [email protected] Data 21 martie 2018 20:41:05
Problema Flux maxim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include<iostream>
#include<fstream>

using namespace std;
int cost[1001][1001]={},viz[1001]={},tata[1001];
void BF(int p,int n,int &flux)
{
	
	int i,ok,j,min;
	if(p==n)
	{
			
				min=2000000000;
				j=p;
			while(tata[j]!=0)
			{
				if(cost[tata[j]][j]<min)
				{
					min=cost[tata[j]][j];
				}
				j=tata[j];
			}
			j=p;
			flux+=min;
			while(tata[j]!=0)
			{
				cost[tata[j]][j]-=min;
				cost[j][tata[j]]+=min;
				j=tata[j];
			}
			
			viz[p]=0;
	}
	else
	{
	for(i=1;i<=n;i++)
	{
		
	
		
		if(viz[i]==0&&cost[p][i]!=0)
		{
		
		
			
				viz[i]=1;
				tata[i]=p;
				BF(i,n,flux);
			
		}
	}
	viz[p]=0;
	}
}
		
		
int main()
{
	int a,b,c,i,m,n,flux=0;
	ifstream f("maxflow.in");
	ofstream g("maxflow.out");
	f>>n>>m;
	for(i=1;i<=m;i++)
	{
		f>>a>>b>>c;
		cost[a][b]=c;
		
	}
	viz[1]=1;
	tata[1]=0;
	BF(1,n,flux);
	
	g<<flux;
	
	f.close();
	g.close();

}