Pagini recente » Joc pe grid | Cod sursa (job #247070) | Istoria paginii monthly-2012/runda-3/solutii | Cod sursa (job #1881490) | Cod sursa (job #1114040)
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#define pb push_back
#define mp make_pair
#define INF (1<<31)-5
#define N_MAX 1010
using namespace std;
vector < pair<int,int> > G[N_MAX];
long long C[N_MAX][N_MAX],T[N_MAX],F[N_MAX][N_MAX];
int N;
long long Flux_Final=0;
inline void Load_Data(int &N)
{
int M,x,y,c;
scanf("%d%d",&N,&M);
for (int i=1;i<=M;++i)
{ scanf("%d%d%d",&x,&y,&c); G[x].pb(mp(y,c)); C[x][y]=c;}
memset(T,0,sizeof(T)); T[1]=-1;
for (int i=1;i<=N;++i)
memset(F[i],0,sizeof(F[i]));
}
inline bool BFS(int N)
{
queue <int> Q;
Q.push(1);
while (!Q.empty())
{
int Nod=Q.front();
Q.pop();
for (vector < pair<int,int> > :: iterator it=G[Nod].begin(); it!=G[Nod].end(); ++it)
{
int nod=(*it).first;
if (T[nod]==0 && C[Nod][nod] > F[Nod][nod])
{
Q.push(nod); T[nod]=Nod;
if (nod==N) return true;
}
}
}
return false;
}
inline long long minim(long long x,long long y)
{
if (x>y) return y;
else return x;
}
inline void MaxFlow(int N)
{
bool OK=BFS(N);
while (OK)
{
long long flux=INF;
int Nod=N;
while (Nod!=1)
flux=minim(flux,C[T[Nod]][Nod]-F[T[Nod]][Nod]), Nod=T[Nod];
if (flux>0)
{
Nod=N;
while (Nod!=1)
F[Nod][T[Nod]]-=flux, F[T[Nod]][Nod]+=flux, Nod=T[Nod];
Flux_Final+=flux;
}
memset(T,0,sizeof(T)); T[1]=-1;
OK=BFS(N);
}
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
Load_Data(N);
MaxFlow(N);
printf("%lld\n",Flux_Final);
fclose(stdin); fclose(stdout);
return 0;
}