Cod sursa(job #1739530)

Utilizator akaprosAna Kapros akapros Data 9 august 2016 16:56:17
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 4.2 kb
#include <bits/stdc++.h>
#define maxN 352
#define inf 2000000000
using namespace std;
int n, m, s, d, D[maxN], prv[maxN], cost[maxN][maxN], c[maxN][maxN], Dist[maxN], Real_Dist[maxN];
vector < int > V[maxN];
struct cmp
{
    bool operator() (const int &x, const int &y)
    {
        return (Dist[x] > Dist[y]);
    }
};
priority_queue < int, vector < int >, cmp > H;
queue < int > q;
bool inq[maxN];
bool vis[maxN];
int ans;
class InputReader
{
public:
    InputReader() {}
    InputReader(const char *file_name)
    {
        input_file = fopen(file_name, "r");
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }
    inline InputReader &operator >>(int &n)
    {
        int ok = 0;
        if (buffer[cursor] == '-')
        {
            ok = 1;
            advance();
        }
        while ((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-')
        {
            advance();
        }
        n = 0;
        if (buffer[cursor] == '-')
        {
            ok = 1;
            advance();
        }
        while ((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-')
        {
            advance();
        }
        while('0' <= buffer[cursor] && buffer[cursor] <= '9')
        {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        if (ok)
            n = - n;
        return *this;
    }
private:
    FILE *input_file;
    static const int SIZE = 1 << 17;
    int cursor;
    char buffer[SIZE];
    inline void advance()
    {
        ++ cursor;
        if(cursor == SIZE)
        {
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
    }
};
void read()
{
    int i;
    InputReader cin("fmcm.in");
    cin >> n >> m >> s >> d;
    for (i = 1; i <= m; ++ i)
    {
        int x, y, z, C;
        cin >> x >> y >> C >> z;
        V[x].push_back(y);
        V[y].push_back(x);
        cost[x][y] = z;
        cost[y][x] = -z;
        c[x][y] = C;
    }
}
void BellmanFord()
{
    int i, j;
    for (i = 1; i <= n; ++ i)
        D[i] = inf;

    memset(inq, 0, sizeof(inq));
    D[s] = 0;
    inq[s] = 1;
    q.push(s);
    while (!q.empty())
    {
        int nod, x = q.front();
        inq[x] = 0;
        q.pop();
        for (i = 0; i < V[x].size(); ++ i)
            if (c[x][nod = V[x][i]] > 0 && D[nod] > D[x] + cost[x][nod])
            {
                D[nod] = D[x] + cost[x][nod];
                prv[nod] = x;
                if (!inq[nod])
                {
                    inq[nod] = 1;
                    q.push(nod);
                }
            }
    }
}
bool Dijkstra()
{
    int i;
    for (i = 1; i <= n; ++ i)
    {
        Dist[i] = inf;
        vis[i] = 0;
        prv[i] = -1;
    }
    Dist[s] = Real_Dist[s] = 0;
    H.push(s);
    vis[s] = 1;
    while (!H.empty())
    {
        int i, x = H.top();
        H.pop();
        vis[x] = 0;
        vector<int> :: iterator nod;
        for (nod = V[x].begin(); nod != V[x].end(); ++ nod)
            if (c[x][*nod])
            {
                int newd = Dist[x] + cost[x][*nod] + D[x] - D[*nod];
                if (Dist[*nod] > newd)
                {
                    Dist[*nod] = Dist[x] + cost[x][*nod] + D[x] - D[*nod];
                    Real_Dist[*nod] = Real_Dist[x] + cost[x][*nod];
                    if (!vis[*nod])
                    {
                        H.push(*nod);
                        vis[*nod] = 1;
                    }
                    prv[*nod] = x;
                }
            }
    }
    if (Dist[d] == inf)
        return 0;
    memcpy(D, Real_Dist, sizeof(Real_Dist));
    int vmin = inf;
    for (i = d; i != s; i = prv[i])
        if (vmin > c[prv[i]][i])
            vmin = c[prv[i]][i];
    for (i = d; i != s; i = prv[i])
    {
        c[i][prv[i]] += vmin;
        c[prv[i]][i] -= vmin;
    }
    ans += vmin * Real_Dist[d];
    return 1;
}
void solve()
{
    BellmanFord();
    while (Dijkstra());
}
void write()
{
    freopen("fmcm.out", "w", stdout);
    printf("%d\n", ans);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}