Pagini recente » Cod sursa (job #2295360) | Cod sursa (job #1115192) | Cod sursa (job #1737629) | Cod sursa (job #2593646) | Cod sursa (job #2685807)
#include <iostream>
#include <fstream>
#include <queue>
#include <cstring>
#define NMAX 1002
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
const int INF=7000000;
struct nod
{
int first, second;
nod*third;
nod*urm;
//first= nodul; second=cate am valabile; third=latura din care provine
}*v[NMAX];
struct tata
{
int first;
nod*second;
}tt[NMAX];
int n, m;
bool viz[NMAX];
int c[NMAX];
nod*pr[NMAX];
int BF()
{
int p, u, nd;
queue<int>cd;
cd.push(1); viz[1]=1;
while(!cd.empty())
{
nd=cd.front();
nod*t=pr[nd];
while(t!=NULL)
{
if(viz[t->first] || t->second==0)
{
t=t->urm;
continue;
}
//adaug in coada
cd.push(t->first); viz[t->first]=1;
tt[t->first].first=nd; //actualizez tt
tt[t->first].second=t; //prin ce muchie a fost adus t->first de nd
//cout<<"t->first= "<<t->first<<"\nt->second= "<<t->second<<"\ntt["<<t->first<<"].first= "<<tt[t->first].first<<"\n\n";
if(t->first==n) return 1;
t=t->urm;
}
cd.pop();
}
return 0;
}
int main()
{
//1=sursa, n=destinatie
fin>>n>>m;
int x, y, z, flow=0, nd, fmin, i;
for(i=1; i<=m; i++)
{
fin>>x>>y>>z;
nod*aux1= new nod;
aux1->first=y;
aux1->second=z;
aux1->urm=pr[x];
pr[x]=aux1;
nod*aux2= new nod;
aux2->first=x;
aux2->second=0;
aux2->third=aux1;
aux2->urm=pr[y];
pr[y]=aux2;
aux1->third=aux2;
}
flow=0;
while(BF())
{
fmin=INF;
//for(i=1; i<=n; i++) cout<<tt[i].first<<' ';
for(nd=n; nd!=1; nd=tt[nd].first)
{
fmin=min(fmin, tt[nd].second->second);
}
//cout<<fmin<<": ";
for(nd=n; nd!=1; nd=tt[nd].first)
{
//cout<<nd<<' ';
tt[nd].second->second -= fmin; //numarul de valabile
tt[nd].second->third->second += fmin;
}
//cout<<"\n";
memset(tt, 0, sizeof(tt)); //resetez tt;
memset(viz, 0, sizeof(viz));
flow+=fmin;
}
fout<<flow;
}