int main() { scanf("%d", &T); while (T--) { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; ++i) { scanf("%d %d %d", &a[i], &b[i], &c[i]); } int s = n * 2, t = n * 2 + 1; mcmf.init(n * 2 + 2, s, t); for (int i = 0; i < n; ++i) { mcmf.add(s, L(i), k, 0); mcmf.add(R(i), t, k, 0); } for (int i = 0; i < m; ++i) { mcmf.add(L(a[i]), R(b[i]), 1, c[i]); } std::pair<int, int> ans = mcmf.gao(); if (ans.first < k * n) { puts("-1"); } else { printf("%d\n", ans.second); } } return 0; }
int main() { int r, c, n, k; int t, x, y; int p[16][16]; while (scanf("%d%d%d%d", &r, &c, &n, &k) != EOF) { mcmf.init(N(), S(), T()); mcmf.addEdge(S(), SX(), k, 0); for (int i = 0; i < r; ++i) { for (int j = 0; j < c; ++j) { scanf("%d", &p[i][j]); if ((i + j) % 2 != 0) { mcmf.addEdge(ID(i, j), T(), 1, 0); } } } for (int i = 0; i < n; ++i) { scanf("%d%d%d", &t, &x, &y); --x; --y; mcmf.addEdge(SX(), ID(x, y), 1, 0); for (int k = 0; k < 8; ++k) { int xx = x + dx[k], yy = y + dy[k]; int cost = 0; switch (t) { case 1: cost = p[x][y] * p[xx][yy]; break; case 2: cost = p[x][y] + p[xx][yy]; break; case 3: cost = max(p[x][y], p[xx][yy]); break; } if (VALID(xx, yy)) { mcmf.addEdge(ID(x, y), ID(xx, yy), 1, cost); } } } pair<int, int> cw = mcmf.gao(); fprintf(stderr, "%d %d\n", cw.first, cw.second); if (cw.first != k) { puts("-1"); } else { printf("%d\n", cw.second); } } return 0; }