Pagini recente » Cod sursa (job #331307) | Cod sursa (job #2447636) | Cod sursa (job #969813) | Cod sursa (job #2779084) | Cod sursa (job #1709412)
#include <fstream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
using namespace std;
const int MAX_N = 208;
const int INF = 10000000;
int F[MAX_N][MAX_N], C[MAX_N][MAX_N];
vector<int> G[MAX_N];
ifstream fin("tribut.in");
ofstream fout("tribut.out");
vector<int> bfs(int nodes, int source, int dest) {
queue<int> q;
vector<int> viz(nodes + 1, 0);
vector<int> p(nodes + 1, -1);
viz[source] = 1;
q.push(source);
while (!q.empty()) {
int node = q.front();
q.pop();
for (int vec : G[node]) {
if (!viz[vec] && C[node][vec] > F[node][vec]) {
viz[vec] = 1;
p[vec] = node;
q.push(vec);
}
}
}
vector<int> path;
int node = dest;
if (viz[dest] == 1) {
while (node != -1) {
path.push_back(node);
node = p[node];
}
}
reverse(path.begin(), path.end());
return path;
}
int pushFlow(const vector<int>& path) {
int maxFlow = INF;
for (int i = 1; i < path.size(); ++i) {
maxFlow = min(maxFlow, C[path[i - 1]][path[i]] - F[path[i - 1]][path[i]]);
}
for (int i = 1; i < path.size(); ++i) {
F[path[i - 1]][path[i]] += maxFlow;
F[path[i]][path[i - 1]] -= maxFlow;
}
return maxFlow;
}
int flow(int nodes, int source, int dest) {
int maxFlow = 0;
while (1) {
vector<int> path = move(bfs(nodes, source, dest));
if (path.size() == 0) {
break;
}
maxFlow += pushFlow(path);
}
return maxFlow;
}
void clear(int nodes) {
memset(F, 0, sizeof(F));
memset(C, 0, sizeof(F));
for (int i = 1; i <= nodes; ++i) {
G[i].clear();
}
}
int main() {
int T;
fin >> T;
while (T--) {
int N, M, s, d;
fin >> N >> M;
s = N + M + 1;
d = N + M + 2;
for (int i = 1; i <= N; ++i) {
fin >> C[s][i];
G[s].push_back(i);
G[i].push_back(s);
}
int cnt, vec;
for (int i = 1; i <= M; ++i) {
fin >> cnt;
fin >> C[N + i][d];
G[N + i].push_back(d);
G[d].push_back(N + i);
for (int j = 1; j <= cnt; ++j) {
fin >> vec;
G[N + i].push_back(vec);
G[vec].push_back(N + i);
C[vec][N + i] = INF;
}
}
fout << flow(N + M + 2, s, d) << '\n';
clear(N + M + 2);
}
return 0;
}