Cod sursa(job #1517950)

Utilizator DeltaMTP Dragos DeltaM Data 5 noiembrie 2015 01:34:11
Problema Flux maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.44 kb
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#define INF 2000000000
using namespace std;
vector<int>L[400];
queue<int>q;
int n,m,s,d,a,b,aa,bb,i,j,ss,dh,ih,P[400],y[400],h[400],r[400],c[400][400],z[400][400],v[400],t[400],D[400],x[400][400];
FILE *f,*g;
int bf(){
    for(int i = 1 ; i <= n ; i++){
        D[i] = INF;
        v[i] = 0;
    }
    q.push(s);
    D[s] = 0;
    while( ! q.empty() ){
        a = q.front();
        q.pop();
        v[a] = 0;
        for(int i = 0 ; i < L[a].size() ; i++){
            b = L[a][i];
            if( D[b] > D[a] + z[a][b] && c[a][b] > x[a][b] ){
                D[b] = D[a] + z[a][b];
                t[b] = a;
                if( v[b] == 0 ){
                    q.push(b);
                    v[b] = 1;
                }
            }
        }
    }
    return D[d] != INF;
}
void chg(int &a,int &b){
    int aux = a;
    a = b;
    b = aux;
}
void del(){
    h[1] = h[ dh-- ];
    int p = 1;
    int c = 2;
    P[ h[1] ] = 1;
    while( c <= dh ){
        if( y[ h[c] ] > y[ h[c+1] ] && c < dh )
            c ++;
        if( y[ h[c] ] < y[ h[p] ] ){
            chg( h[c] , h[p] );
            P[ h[c] ] = c;
            P[ h[p] ] = p;
            p = c;
            c *= 2;
        }
        else
            break;
    }
}
void upd(int poz){
    int c = poz;
    int p = poz / 2;
    while( p > 0 ){
        if( y[ h[c] ] < y[ h[p] ] ){
            chg( h[c] , h[p] );
            P[ h[c] ] = c;
            P[ h[p] ] = p;
            c = p;
            p /= 2;
        }
        else
            break;
    }
}
int dijkstra() {
    memset(t,0,sizeof(t));
    for(int i = 1 ; i <= n ; i++ )
        y[i] = INF;
    y[s] = 0;
    r[s] = 0;
    h[1] = s;
    dh = 1;
    P[s] = 1;
    int ok = 0;
    while(dh){
        int poz = h[1];
        del();
        for(int i = 0 ; i < L[poz].size() ; i++){
            a = L[poz][i];
            if ( y[a] > y[poz] + z[poz][a] + D[poz] - D[a] && c[poz][a] > x[poz][a]) {
                t[a] = poz;
                if( a == d )
                    ok = 1;
                if( y[a] ==INF )
                    ih = 0;
                else
                    ih = 1;
                y[a] = y[poz] + z[poz][a] + D[poz] - D[a];
                r[a] = r[poz] + z[poz][a];
                if( ih == 0 ) {
                    h[++dh] = a;
                    P[ h[dh] ] = dh;
                    upd(dh);
                } else
                    upd(P[a]);
            }
        }
    }
    memcpy(D,r,sizeof(D));
    return ok;
}
int main(){
    f = fopen("fmcm.in","r");
    g = fopen("fmcm.out","w");
    fscanf( f , "%d%d%d%d", &n , &m , &s , &d );
    for( i = 1 ; i <= m ; i++ ){
        fscanf( f , "%d%d%d%d" , &a , &b , &aa , &bb );
        L[a].push_back(b);
        L[b].push_back(a);
        c[a][b] = aa;
        z[a][b] = bb;
        z[b][a] = - bb;
    }
    bf();
    while(dijkstra()){
        aa = INF;
        a = d;
        while( t[a] != 0 ){
            if( aa > c[ t[a] ][a] - x[ t[a] ][a] )
                aa = c[ t[a] ][a] - x[ t[a] ][a];
            a=t[a];
        }
        a = d;
        while( t[a] != 0 ){
            ss += aa * z[ t[a] ][a];
            x[ t[a] ][a] += aa;
            x[a][ t[a] ] -= aa;
            a = t[a];
        }
    }
    fprintf( g , "%d" , ss );




    fclose(f);
    fclose(g);
    return 0;
}