Cod sursa(job #1948209)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 31 martie 2017 21:07:49
Problema A+B Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.93 kb
#include <bits/stdc++.h>
using namespace std;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
} in( "adunare.in" );

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
} out( "adunare.out" );

const int DIM = 355;
const int INF = 0x3f3f3f3f;

int dp[DIM], fth[DIM], dst[DIM], aux[DIM];
int cap[DIM][DIM], cst[DIM][DIM];

bitset<DIM> mrk; vector<int> edg[DIM]; deque<int> que;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> prq;

inline bool bf( int s, int d, int n ) {
    memset( dst, 0x3f, sizeof( dst ) );

    dst[s] = 0; mrk.reset(); mrk[s] = 1;
    for( que.push_back( s ); que.empty() == false; que.pop_front() ) {
        int x = que.front(); mrk[x] = false;

        for( int y : edg[x] ) {
            if( cap[x][y] != 0 && dst[y] > dst[x] + cst[x][y] ) {
                dst[y] = dst[x] + cst[x][y]; fth[y] = x;

                if( y != d && mrk[y] == false ) {
                    mrk[y] = true;
                    que.push_back( y );
                }
            }
        }
    }

    return ( dst[d] != INF );
}

inline bool dj( int s, int d, int n ) {
    memset(  dp, 0x3f, sizeof(  dp ) );
    memset( aux, 0x3f, sizeof( aux ) );

    dp[s] = aux[s] = 0;
    for( prq.push( make_pair( 0, s ) ); prq.empty() == false; ) {
        pair<int, int> x = prq.top(); prq.pop();

        if( dp[x.second] != x.first )
            continue;

        for( int y : edg[x.second] ) {
            if( cap[x.second][y] != 0 && dp[y] > dp[x.second] + dst[x.second] - dst[y] + cst[x.second][y] ) {
                dp[y] = dp[x.second] + dst[x.second] - dst[y] + cst[x.second][y];
                aux[y] = aux[x.second] + cst[x.second][y]; fth[y] = x.second;

                if( y != d )
                    prq.push( make_pair( dp[y], y ) );
            }
        }
    }

    memcpy( dst, aux, sizeof( aux ) );
    return ( dp[d] != INF );
}

int main( void ) {
    ios::sync_with_stdio( false );

    int a, b;
    in >> a >> b;

    int s = 1, d = 3, n = 3;

    cap[1][2] = cap[2][3] = 1;
    cst[1][2] = a; cst[2][1] = -a;
    cst[2][3] = b; cst[3][2] = -b;

    edg[1].push_back( 2 ); edg[2].push_back( 1 );
    edg[2].push_back( 3 ); edg[3].push_back( 2 );

    bf( s, d, n );

    int cmn = 0;
    while( dj( s, d, n ) ) {
        int mnm = INF;

        for( int i = d; i != s; i = fth[i] )
            mnm = min( mnm, cap[fth[i]][i] );

        cmn += dst[d] * mnm;
        for( int i = d; i != s; i = fth[i] ) {
            cap[fth[i]][i] -= mnm;
            cap[i][fth[i]] += mnm;
        }
    }

    out << cmn << "\n";
    return 0;
}