Cod sursa(job #1709226)

Utilizator echipa_BoSSilorUNIBUC Harsan Bicsi Baltatu echipa_BoSSilor Data 28 mai 2016 11:22:15
Problema Tribut Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 3.15 kb
/*************************************************************\
~*********************ENJOY THE SILENCE***********************~
\*************************************************************/

#include <bits/stdc++.h>
using namespace std;

/*******************Debugging defines*************************/

#define ok_dump() cerr<<"OK\n"
#define var_dump(x) cerr<<#x": "<<x<<'\n'
#define arr_dump(x, n) {cerr<<#x"[]: ";\
	for(int _=0;_<n;++_) cerr<<x[_]<<" ";cerr<<'\n';}

/*************************************************************/

const int INF = 1e9 + 10;

namespace MaxFlow {
    const int MAX_NODES = 1005;
 
    int SRC = 0, DEST = 1;
    int nodes = 2;
 
    int F[MAX_NODES][MAX_NODES], C[MAX_NODES][MAX_NODES];
    int Parent[MAX_NODES];
    queue<int> Q;
    vector<int> G[MAX_NODES];
 
    void init(int n, int s = -1, int d = -1) {
        if(s == -1) s = ++n;
        if(d == -1) d = ++n;
 
        SRC = s; DEST = d;
        nodes = n;
 
        for(int i=1; i<=nodes; i++) {
            for(auto vec : G[i]) {
                C[i][vec] = F[i][vec] = 0;
            }
            G[i].clear();
        }
    }
 
    void addEdge(int a, int b, int cap) {
        G[a].push_back(b);
        G[b].push_back(a);
        C[a][b] += cap;
    }
 
    bool bfs() {
        memset(Parent, 0, sizeof(Parent));
        Parent[SRC] = -1;
        Q.push(SRC);
 
        while(!Q.empty()) {
            int node = Q.front();
            Q.pop();
 
            for(auto vec : G[node]) {
                if(!Parent[vec] && F[node][vec] < C[node][vec]) {
                    Parent[vec] = node;
                    Q.push(vec);
                }
            }
        }
 
        return Parent[DEST] != 0;
    }
 
 
    int maxFlow() {
        int flow = 0;
        while(bfs()) {
            for(auto x : G[DEST]) {
                if(Parent[x] == 0) continue;
 
                int M = C[x][DEST] - F[x][DEST];
                for(int node = x; node != SRC; node = Parent[node]) {
                    M = min(M, C[Parent[node]][node] - F[Parent[node]][node]);
                }
 
                F[x][DEST] += M; F[DEST][x] -= M;
                for(int node = x; node != SRC; node = Parent[node]) {
                    F[Parent[node]][node] += M;
                    F[node][Parent[node]] -= M;
                }
 
                flow += M;
            }
        }
 
        return flow;
    }
};

int main() {	
//	assert(freopen("input.txt", "r", stdin));
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	ifstream fin("tribut.in");
	ofstream fout("tribut.out");

	int t;
	fin >> t;

	while(t--) {
		int n, m;
		fin >> n >> m;
		int nodes = n + m + 2;
		MaxFlow::init(nodes + 5, 1, 2);

		for(int i = 1; i <= n; ++i) {
			int t;
			fin >> t;
			MaxFlow::addEdge(1, i + 2, t);
		}

		for(int i = 1; i <= m; ++i) {
			int p, x, a;
			fin >> p >> x;
			MaxFlow::addEdge(i + n + 2, 2, x);

			while(p--) {
				fin >> a;
				MaxFlow::addEdge(a + 2, i + n + 2, INF);
			}
		}

		fout << MaxFlow::maxFlow() << '\n';
	}

	return 0;
}

/*************************************************************\
~*********************ENJOY THE SILENCE***********************~
\*************************************************************/