QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#520336#6455. Fancy AntiquesAndycipationTL 0ms3828kbC++202.2kb2024-08-15 13:02:242024-08-15 13:02:25

Judging History

你现在查看的是最新测评结果

  • [2024-08-15 13:02:25]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3828kb
  • [2024-08-15 13:02:24]
  • 提交

answer

/*
 * author:  ADMathNoob
 * created: 08/14/24 22:28:48
 * problem: https://qoj.ac/problem/6455
 */

/*
Comments about problem:


*/

#include <bits/stdc++.h>

using namespace std;

#ifdef _DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif

const int M = 40;

int n, m, k;
vector<int> g[M];

int bonus[M];
bool must[M];
int state[M];
vector<int> order;

int Solve(int z, int used, int cur) {
  assert(used <= k);
  if (z == m) {
    return cur;
  }
  int v = order[z];
  if (state[v] != -1) {
    if (state[v] == 1) {
      cur += bonus[v];
    }
    return Solve(z + 1, used, cur);
  }
  int res = -1;
  if (!must[v]) {
    state[v] = 0;
    vector<int> changed;
    for (int u : g[v]) {
      assert(state[u] != 0);
      if (state[u] == -1) {
        state[u] = 1;
        changed.push_back(u);
      }
    }
    int new_used = used + changed.size();
    if (new_used <= k) {
      res = max(res, Solve(z + 1, new_used, cur));
    }
    for (int u : changed) {
      state[u] = -1;
    }
    changed.clear();
  }
  if (used < k) {
    state[v] = 1;
    res = max(res, Solve(z + 1, used + 1, cur + bonus[v]));
  }
  state[v] = -1;
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  int base = 0;
  vector<int> deg(m);
  for (int i = 0; i < n; i++) {
    int x, px, y, py;
    cin >> x >> px >> y >> py;
    --x; --y;
    if (x == y) {
      must[x] = true;
    } else {
      if (x < y) {
        g[x].push_back(y);
      } else {
        g[y].push_back(x);
      }
      // g[x].push_back(y);
      // g[y].push_back(x);
      deg[x] += 1;
      deg[y] += 1;
    }
    base += max(px, py);
    if (px < py) {
      bonus[x] += py - px;
    } else {
      bonus[y] += px - py;
    }
  }
  order.resize(m);
  iota(order.begin(), order.end(), 0);
  // sort(order.begin(), order.end(), [&](int x, int y) {
  //   return deg[x] > deg[y];
  // });
  fill(state, state + m, -1);
  int max_save = Solve(0, 0, 0);
  if (max_save == -1) {
    cout << -1 << '\n';
  } else {
    int ans = base - max_save;
    cout << ans << '\n';
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3828kb

input:

3 3 2
1 30 2 50
2 70 3 10
3 20 1 80

output:

60

result:

ok single line: '60'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3816kb

input:

3 3 1
1 30 2 50
2 70 3 10
3 20 1 80

output:

-1

result:

ok single line: '-1'

Test #3:

score: -100
Time Limit Exceeded

input:

3 40 20
1 30 2 50
2 70 3 10
3 20 1 80

output:


result: