Cod sursa(job #1709428)

Utilizator DEFINEtelyEngineersUPB Pirtoaca Vasilescu Zamfiratos DEFINEtelyEngineers Data 28 mai 2016 12:11:55
Problema Tribut Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 2.56 kb
#include<fstream>
#include<vector>
#include<queue>
#include<cstring>

#define Nmax 220

std::ifstream in("tribut.in");
std::ofstream out("tribut.out");

std::vector<int> G[Nmax];
int F[Nmax][Nmax], C[Nmax][Nmax];
int value[Nmax];
int ok[Nmax];
int T[Nmax];

int BF(int destination) {

    std::queue<int> Q;
    Q.push(0);
    memset(ok,0,sizeof(ok));

    while (!Q.empty()) {
        int nod=Q.front();
        Q.pop();
        for (std::vector<int>::iterator it=G[nod].begin();it!=G[nod].end();it++) {
            int nod2=*it;
            if (!ok[nod2] && (F[nod][nod2] < C[nod][nod2])) {
                T[nod2] = nod;
                ok[nod2] = 1;

                if (nod2 == destination) continue;
                Q.push(nod2);
            }
        }
    }

    return ok[destination];
}

int main()
{
    int t;
    in >> t;
    while (t--) {

        int n, m;
        in >> n >> m;
        memset(C, 0, sizeof(C));
        memset(F, 0, sizeof(F));
        for (int i = 1; i <= n; i++) {
            in >> value[i];
            G[0].push_back(i);
            G[i].push_back(0);
            C[0][i] = value[i];
            C[i][0] = 0;
        }

        for (int i = 1; i <= m; i++) {
            int nr, k;
            in >> nr >> k;
            for (int j = 1; j <= nr; j++) {
                int node;
                in >> node;
                G[node].push_back(i + n);
                G[i + n].push_back(node);

                G[i + n].push_back(n + m + 1);
                G[n + m + 1].push_back(i + n);

                C[node][i + n] = value[node];
                C[i + n][node] = 0;

                C[i + n][n + m + 1] = k;
                C[n + m + 1][i + n] = 0;
            }

        }

        int FLUX = 0;
        while (BF(n + m + 1))
            for (std::vector<int>::iterator it=G[n + m + 1].begin();it!=G[n + m + 1].end();it++)
            {
                int nod=*it;
                int maxf=C[nod][n + m + 1] - F[nod][n + m + 1];

                while (nod != 0 && maxf) {
                    maxf=std::min(maxf,C[T[nod]][nod]-F[T[nod]][nod]);
                    nod=T[nod];
                }
                if (!maxf) continue;

                nod=*it;
                F[nod][n]+=maxf;
                F[n][nod]-=maxf;

                while (nod != 0) {
                    F[T[nod]][nod]+=maxf;
                    F[nod][T[nod]]-=maxf;

                    nod=T[nod];
                }
                FLUX+=maxf;
            }
        out << FLUX << "\n";
    }
}