Pagini recente » Cod sursa (job #814609) | Cod sursa (job #275207) | Cod sursa (job #858885) | Cod sursa (job #2204935) | Cod sursa (job #3351458)
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("tribut.in");
ofstream fout("tribut.out");
int solarSystems, unions, t, systemID, currentUnionSystems;
vector<int> adj[201];
int residualCap[205][205], tributes[101];
int tata[205];
int augmentingPath(int source, int sink) {
fill(tata, tata + solarSystems + unions + 2, -1);
tata[source] = -2;
queue<pair<int, int>> q;
q.push({ source, 1e9 });
while (!q.empty())
{
int current = q.front().first;
int flow = q.front().second;
q.pop();
for (int next : adj[current])
{
if (tata[next] == -1 && residualCap[current][next])
{
tata[next] = current;
int nextFlow = min(flow, residualCap[current][next]);
if (next == sink)
return nextFlow;
q.push({ next, nextFlow });
}
}
}
return 0;
}
int main() {
fin >> t;
while (t--)
{
fin >> solarSystems >> unions;
for (int i = 0; i <= solarSystems + unions; i++)
adj[i].clear();
for (int i = 1; i <= solarSystems; i++)
{
fin >> tributes[i];
adj[0].push_back(i);
adj[i].push_back(0);
residualCap[0][i] = tributes[i];
}
for (int i = 1; i <= unions; i++)
{
int maxTribute;
fin >> currentUnionSystems >> maxTribute;
residualCap[solarSystems + i][solarSystems + unions + 1] = maxTribute;
adj[solarSystems + i].push_back(solarSystems + unions + 1);
adj[solarSystems + unions + 1].push_back(solarSystems + i);
for (int j = 1; j <= currentUnionSystems; j++)
{
fin >> systemID;
adj[systemID].push_back(solarSystems + i);
adj[solarSystems + i].push_back(systemID);
residualCap[systemID][solarSystems + i] = tributes[systemID];
}
}
int source = 0, sink = solarSystems + unions + 1, res = 0;
int nextFlow;
while (nextFlow = augmentingPath(source, sink))
{
res += nextFlow;
int current = sink;
while (current != source)
{
int prev = tata[current];
residualCap[prev][current] -= nextFlow;
residualCap[current][prev] += nextFlow;
current = prev;
}
}
fout << res << '\n';
}
return 0;
}