Cod sursa(job #783346)

Utilizator batistaUPB-Oprea-Cosmin-Dumitru batista Data 2 septembrie 2012 16:29:50
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include<fstream>
#include<cstring>
using namespace std;
typedef struct celula{
           short nod;
           celula *next;
           }*lista;
lista graf[1001],v,q,aux;
int cost[1001][1001],tata[1001],n,m,flow;
bool ok,viz[1001];

inline void update(){
     int nod=n,min=100000;
     while (tata[nod]!=-1){
           if (cost[tata[nod]][nod]<min) min=cost[tata[nod]][nod];
           nod=tata[nod];
           }
     nod=n; flow+=min;
     while (tata[nod]!=-1){
           cost[tata[nod]][nod]-=min;
           cost[nod][tata[nod]]+=min;
           nod=tata[nod];
           }
}

inline void bfs(){
     v=new celula; v->nod=1; v->next=0; q=v; tata[1]=-1; ok=0;
      memset(viz,0,sizeof(viz)); viz[1]=1;
     while (v!=0) {
           for (lista p=graf[v->nod];p; p=p->next){
               if (viz[p->nod]==0&&cost[v->nod][p->nod]>0) { if (p->nod!=n) {aux=new celula; aux->nod=p->nod; q->next=aux; q=aux; q->next=0;} tata[p->nod]=v->nod; viz[p->nod]=1;}
                if (viz[n]==1){ok=1; update(); viz[n]=0; }
                }
           v=v->next;
     }
}
           
int main(void){
    ifstream fin("maxflow.in");
    ofstream fout("maxflow.out");
    int i,x,c,y; fin>>n>>m;
    for (i=1; i<=m; ++i) {
        fin>>x>>y>>c; cost[x][y]=c;
        v=new celula; v->nod=y; v->next=graf[x]; graf[x]=v;
        v=new celula; v->nod=x; v->next=graf[y]; graf[y]=v;
        }
    ok=1;
    while (ok==1) bfs();
    fout<<flow;
 return(0);
}