Pagini recente » Cod sursa (job #1837857) | Cod sursa (job #1316950) | Cod sursa (job #3295502) | Cod sursa (job #1273294) | Cod sursa (job #1859558)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int NMax=1005, MMax=5005;
int N,M,A[NMax],f,Use[NMax];
vector < pair <int,int> > G[NMax];
void Read()
{
fin>>N>>M;
for(int i=1;i<=M;++i)
{
int x,y,z;
fin>>x>>y>>z;
G[x].push_back(make_pair(y,z));
}
A[1]=200000000;
}
void DFS(int Nod)
{
if(Use[Nod]==0)
{
f++;
Use[Nod]=1;
}
for(unsigned j=0;j<G[Nod].size();++j)
{
int Vecin=G[Nod][j].first;
if(G[Nod][j].second>=A[Nod])
{
A[Vecin]=A[Vecin]+A[Nod];
G[Nod][j].second=G[Nod][j].second-A[Nod];
A[Nod]=0;
}
else
{
A[Vecin]=A[Vecin]+G[Nod][j].second;
A[Nod]=A[Nod]-G[Nod][j].second;
G[Nod][j].second=0;
}
if(f<=N)
{
DFS(Vecin);
}
}
}
int main()
{
Read();
DFS(1);
fout<<A[N]<<"\n";
return 0;
}