Pagini recente » Cod sursa (job #3193087) | Cod sursa (job #2133814) | Cod sursa (job #1352517) | Cod sursa (job #208894)
Cod sursa(job #208894)
#include<fstream>
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)>0?x:-x)
using namespace std;
int f[50][50],cost[50][50],n,m,s,d,viz[50],q[50];
ifstream in("flux.in");
ofstream out("flux.out");
void citire()
{int i,x,y,c;
in>>n>>m>>s>>d;
for(i=0;i<m;i++)
{in>>x>>y>>c;
cost[x][y]=c;}}
int bfs()
{int p=0,u=0,i,x;
q[0]=s;
while(p<=u && !viz[d])
{x=q[p++];
for(i=1;i<=n;i++)
{if(!viz[i])
if(f[x][i]<cost[x][i])
{u++;q[u]=i;viz[i]=x;}
else if(f[i][x]>0)
{viz[i]=-x;q[++u]=i;}
}}
return !viz[d]; }
void edmond_karp()
{int i,a,b,lg,v;
int l[50];
do
{for(i=1;i<=n;i++) viz[i]=0;
if(bfs()) return;
l[0]=d; lg=0;
a=10000;b=10000;
while(l[lg]!=s)
{++lg;
l[lg]=abs(viz[l[lg-1]]);
if(viz[l[lg-1]]>0)
a=min(a,cost[l[lg]][l[lg-1]]-f[l[lg]][l[lg-1]]);
else if(viz[l[lg-1]]<0)
{b=min(b,cost[l[lg-1]][l[lg]]-f[l[lg-1]][l[lg]]);}}
v=min(a,b);
for(i=lg;i>0;i--)
if(viz[l[i-1]]>0)
f[l[i]][l[i-1]]+=v;
else f[l[i-1]][l[i]]+=v;
}
while(1);}
void afisare()
{int i,j,vf=0;
for(i=1;i<=n;i++)
vf+=f[i][d];
out<<vf;
}
int main()
{citire();
edmond_karp();
afisare();
return 0;}