Pagini recente » Cod sursa (job #1807263) | Cod sursa (job #2726849) | Cod sursa (job #22112) | Cod sursa (job #1873336) | Cod sursa (job #1468087)
#include <fstream>
#include <cstring>
#define Max 1000000000
#include <vector>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int co[55000];
int ant[55000];
int viz[5010];
vector<int> g[1010];
int cons[1010][1010],maxx[1010][1010];
int in,s,sf,nod,cost,i,n,m,j,x,y,c,last;
bool bfs()
{
in=1;
sf=0;
co[++sf]=1;
memset(viz,0,sizeof(viz));
// memset(ant,0,sizeof(ant));
viz[1]=1;
while(in<=sf)
{
nod=co[in];
if (nod==n) return true;
for(i=0; i<g[nod].size(); i++)
{
if(cons[nod][g[nod][i]]<maxx[nod][g[nod][i]] && viz[g[nod][i]]==0)
{
viz[g[nod][i]]=1;
co[++sf]=g[nod][i];
ant[g[nod][i]]=nod;
}
}
in++;
}
return false;
}
int main()
{
int prev;
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>x>>y>>c;
g[x].push_back(y);
g[y].push_back(x);
maxx[x][y]=c;
}
while(bfs())
{
for(int i=0; i<g[n].size(); i++)
{
if(viz[g[n][i]]==1 && maxx[g[n][i]][n]-cons[g[n][i]][n]!=0)
{
cost=Max;
last=n;
prev=g[n][i];
while(last!=1)
{
cost = cost < maxx[prev][last]-cons[prev][last] ? cost : maxx[prev][last]-cons[prev][last];
last=prev;
prev=ant[last];
}
last=n;
prev=g[n][i];
while(last!=1)
{
cons[prev][last]+=cost;
cons[last][prev]-=cost;
last=prev;
prev=ant[last];
}
s+=cost;
}
}
}
fout<<s;
return 0;
}