Cod sursa(job #1709033)

Utilizator UNIBUC_Costan_Iordache_MagureanuGangster Teddy Bears Trio UNIBUC_Costan_Iordache_Magureanu Data 28 mai 2016 10:38:47
Problema Tribut Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.97 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>

using namespace std;

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

const int inf = 1000000000;

int n, m, dest;
int C[205][205], F[205][205];
vector<int> g[205];

bool vis[205];
int parent[205];

queue<int> que;

bool bfs() {

	memset(vis, 0, sizeof vis);
	que.push(0);
	vis[0] = true;

	while (!que.empty()) {

		int x = que.front();
		que.pop();

		if (x == dest)
			continue;

		for (int it : g[x]) {
			if (vis[it] || F[x][it] == C[x][it])
				continue;
			vis[it] = true;
			parent[it] = x;
			que.push(it);
		}

	}

	return vis[dest];

}

int mF() {

	int ret = 0;
	while (bfs()) {

		for (int it : g[dest]) {

			if (!vis[it] || C[it][dest] == F[it][dest])
				continue;

			parent[dest] = it;

			int addF = inf;
			for (int i = dest; i; i = parent[i])
				addF = min(addF, C[parent[i]][i] - F[parent[i]][i]);
			
			ret += addF;
			for (int i = dest; i; i = parent[i]) {
				F[parent[i]][i] += addF;
				F[i][parent[i]] -= addF;
			}

		}

	}

	return ret;

}

int main() {

	int testCount;
	fin >> testCount;

	while (testCount--) {

		memset(C, 0, sizeof C);
		memset(F, 0, sizeof F);
		
		fin >> n >> m;
		g[0].clear();
		for (int i = 1; i <= n; ++i)
			g[i].clear(); 
		for (int i = 1; i <= n; ++i) {

			int x;
			fin >> x;

			C[0][i] = x;
			g[i].push_back(0);
			g[0].push_back(i);

		}

		for (int j = n + 1; j <= n + m; ++j) {

			g[j].clear();

		}

		g[n + m + 1].clear();
		dest = n + m + 1;

		for (int j = n + 1; j <= n + m; ++j) {

			int p, x;
			fin >> p >> x;

			g[j].push_back(dest);
			g[dest].push_back(j);
			C[j][dest] = x;

			for (int i = 1; i <= p; ++i) {

				int k;
				fin >> k;

				g[k].push_back(j);
				g[j].push_back(k);
				C[k][j] = inf;

			}

		}

		fout << mF() << '\n';

	}

	return 0;

}

//Trust me, I'm the Doctor!