Pagini recente » Cod sursa (job #1640986) | Cod sursa (job #1107032) | Cod sursa (job #2106596) | Cod sursa (job #1246525) | Cod sursa (job #2174580)
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
#define nmax 1002
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int capacity[nmax][nmax],full[nmax][nmax],father[nmax],n,m,val,x,y,flow;
queue <int> coada;
vector <int> graph[nmax];
bitset <nmax> viz;
int BF()
{
for(int i=2;i<=n;i++)
viz[i]=0;
coada.push(1);
while(!coada.empty())
{
int V=coada.front();
coada.pop();
if(V==n)
continue;
for(int i=0;i<graph[V].size();i++)
{
int z=graph[V][i];
if(capacity[V][z]==full[V][z]||viz[z])
continue;
father[z]=V;
viz[z]=1;
coada.push(z);
}
}
return viz[n];
}
int read_int()
{
char c=fin.get();
int nr=0;
while(isdigit(c))
{
nr=nr*10+c-'0';
c=fin.get();
}
return nr;
}
int main()
{
fin>>n>>m;
fin.get();
for(int i=1;i<=m;i++)
{
x=read_int();
y=read_int();
val=read_int();
capacity[x][y]=val;
graph[x].push_back(y);
graph[y].push_back(x);
}
viz[1]=1;
for(;BF();)
{
for(int i=0;i<graph[n].size();i++)
{
int nod=graph[n][i];
if(!viz[nod]||capacity[nod][n]==full[nod][n])
continue;
father[n]=nod;
int mini=10000000002;
for(int n2=n;father[n2];n2=father[n2])
mini=min(mini,capacity[father[n2]][n2]-full[father[n2]][n2]);
for(int n2=n;father[n2];n2=father[n2])
{
full[father[n2]][n2]+=mini;
full[n2][father[n2]]-=mini;
}
flow+=mini;
}
}
fout<<flow;
return 0;
}