Cod sursa(job #2067724)

Utilizator Alexa2001Alexa Tudose Alexa2001 Data 16 noiembrie 2017 19:48:44
Problema Pixels Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.76 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("pixels.in");
ofstream fout ("pixels.out");

const int Nmax = 100 * 100 + 5;
int i, j, k, n, answer, Cost, S, D, C[5];

int code(int x, int y) { return (x-1) * n + y; }

class graph
{
    struct Edge
    {
        int x, y, c;
    } edge[8 * Nmax];

    vector<int> where[Nmax];
    int t[Nmax], cnt = 0;

    bool BFS()
    {
        int node;
        memset(t, -1, sizeof(t));

        queue<int> Q;
        Q.push(S); t[S] = 0;

        while(!Q.empty())
        {
            node = Q.front();
            Q.pop();

            for(auto it : where[node])
                if(edge[it].c && t[edge[it].y] == -1)
                {
                    if(edge[it].y == D) return 1;
                    t[edge[it].y] = it;
                    Q.push(edge[it].y);
                }
        }
        return 0;
    }

    public:
        void add_edge(int x, int y, int capacity)
        {
            where[x].push_back(cnt);
            edge[cnt++] = {x, y, capacity};

            where[y].push_back(cnt);
            edge[cnt++] = {y, x, capacity};
        }

        int max_flow()
        {
            int node, fmin, flow = 0;
            while(BFS())
            {
                for(auto it : where[D])
                if(t[edge[it].y] != -1 && edge[it^1].c)
                {
                    fmin = edge[it^1].c;
                    node = edge[it].y;

                    while(node != S)
                    {
                        fmin = min(fmin, edge[t[node]].c);
                        node = edge[t[node]].x;
                    }

                    flow += fmin;
                    edge[it^1].c -= fmin;
                    edge[it].c += fmin;

                    node = edge[it].y;

                    while(node != S)
                    {
                        edge[t[node]].c -= fmin;
                        edge[t[node]^1].c += fmin;
                        node = edge[t[node]].x;
                    }
                }
            }
            return flow;
        }
} graph;


int main()
{
    fin >> n;
    S = 0, D = n * n + 1;
    for(i=1; i<=n; ++i)
        for(j=1; j<=n; ++j)
        {
            fin >> Cost;
            graph.add_edge(S, code(i, j), Cost);
            answer += Cost;
        }

    for(i=1; i<=n; ++i)
        for(j=1; j<=n; ++j)
        {
            fin >> Cost;
            graph.add_edge(code(i, j), D, Cost);
            answer += Cost;
        }

    for(i=1; i<=n; ++i)
        for(j=1; j<=n; ++j)
        {
            for(k=0; k<4; ++k) fin >> C[k];
            if(j+1 <= n) graph.add_edge(code(i, j), code(i, j+1), C[1]);
            if(i+1 <= n) graph.add_edge(code(i, j), code(i+1, j), C[2]);
        }

    answer -= graph.max_flow();
    fout << answer << '\n';

    return 0;
}