Pagini recente » Cod sursa (job #1270487) | Cod sursa (job #897632) | Cod sursa (job #2973656) | Cod sursa (job #1149686) | Cod sursa (job #1656146)
#include <fstream>
#include <algorithm>
#include <cstring>
#include <climits>
using namespace std;
int n,m;
int st[1005];
struct cost{int v,cap,fx,sens;bool trec;};
cost a[1005][1005];
bool viz[1005];
ofstream g("flux.out");
int flux(int p)
{
int ep1,ep2,ep,i,j,x,y;
ep1=ep2=INT_MAX;
for (i=1;i<p;i++)
{
x=st[i];j=1;
while (a[x][j].v!=st[i+1]) j++;
if (a[x][j].sens==1)
{
ep1=min(ep1,a[x][j].cap-a[x][j].fx);
}
else ep2=min(ep1,a[x][j].fx);
}
if (ep2==INT_MAX) ep=ep1;
else ep=min(ep1,ep2);
for (i=1;i<p;i++)
{
x=st[i];j=1;
while (a[x][j].v!=st[i+1]) j++;
if (a[x][j].sens==1)
{
a[x][j].fx+=ep;
}
else a[x][j].fx-=ep;
}
return ep;
}
int main()
{
ifstream f("flux.in");
int i,x,y,costt,ok,fl=0;
f>>n>>m;
for (i=1;i<=m;i++)
{
f>>x>>y>>costt;
a[x][++a[x][0].v].v=y;
a[x][a[x][0].v].sens=1;
a[x][a[x][0].v].cap=costt;
a[y][++a[y][0].v].v=x;
a[y][a[y][0].v].sens=-1;
a[y][a[y][0].v].cap=costt;
}
//sursa = 1 , destinatie = n
st[1]=1;int p=1;viz[1]=1;
bool finall=1;
while (finall)
{
while (p>0)
{
x=st[p];ok=1;
for (i=1;i<=a[x][0].v&&ok;i++)
{
y=a[x][i].v;
if (!viz[y])
{
if (a[x][i].sens==1)
{
if ((a[x][i].cap-a[x][i].fx!=0)) ok=0;
}
else if (a[x][i].fx!=0) ok=0;
}
}
if (!ok)
{
st[++p]=y,viz[y]=1;
if (st[p]==n) break;
}
else p--;
}
if (!viz[n]) finall=0;
else
{
fl+=flux(p);
memset(viz,0,(n+1)*sizeof(bool));
p=1;st[1]=1;
}
}
g<<fl<<'\n';
f.close();
g.close();
return 0;
}