Pagini recente » Cod sursa (job #2294384) | Cod sursa (job #2370405) | Cod sursa (job #2354918) | Cod sursa (job #1204520) | Cod sursa (job #1913656)
#include <fstream>
#include<climits>
using namespace std;
ifstream cin("maxflow.in");
ofstream cout ("maxflow.out");
int cost[1010][1010];
int start[1010];
int n,m,nr,flux;
int used[1010];
int coada[1010];
int tata[1010];
struct bla
{
int nod,urm;
} t[10010];
void read ()
{ int a,b,c,k=0;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>a>>b>>c;
cost[a][b]=c;
t[++k].nod=b; t[k].urm=start[a]; start[a]=k;
t[++k].nod=a; t[k].urm=start[b]; start[b]=k;
}
}
bool bfs ()
{
int pi=1,ps=1;
coada[1]=1;
used[1]=nr;
while(ps<=pi)
{
for(int x=start[coada[ps]];x;x=t[x].urm)
{
if(used[t[x].nod]<nr && cost[coada[ps]][t[x].nod]>0)
{
used[t[x].nod]=nr;
tata[t[x].nod]=coada[ps];
coada[++pi]=t[x].nod;
}
} ++ps;
}
if(used[n]<nr) return false;
return true;
}
void fluxx ()
{ nr++;
while(bfs())
{
for(int x=start[n];x;x=t[x].urm)
{
if( used[t[x].nod]==nr && cost[t[x].nod][n]>0 )
{
tata[n]=t[x].nod;
int maxim=INT_MAX;
for(int y=n;y!=1;y=tata[y]) maxim=min(maxim,cost[tata[y]][y]);
for(int y=n;y!=1;y=tata[y])
{
cost[tata[y]][y]-=maxim;
cost[y][tata[y]]+=maxim;
}
flux+=maxim;
}
} ++nr;
}
}
void write ()
{
cout<<flux;
}
int main()
{
read();
fluxx();
write();
cin.close();
cout.close();
return 0;
}