QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#306185#7185. Poor StudentsckisekiTL 1525ms82492kbC++234.8kb2024-01-16 14:56:112024-01-16 14:56:11

Judging History

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

  • [2024-01-16 14:56:11]
  • 评测
  • 测评结果:TL
  • 用时:1525ms
  • 内存:82492kb
  • [2024-01-16 14:56:11]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef local
#include <experimental/iterator>
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(const char *s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
void orange_(const char *s, auto L, auto R) {
  cerr << "\e[1;33m[ " << s << " ] = [ ";
  using namespace experimental;
  copy(L, R, make_ostream_joiner(cerr, ", "));
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

using lld = int64_t;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;

template <typename T>
bool chmin(T &x, T v) {
  if (x > v) return x = v, true;
  return false;
}

// const int N = 105;
// const int M = 1525;

const int maxn = 105;

template <typename F, typename C>
struct MinCostCirculation {
  struct ep { int to; F flow; C cost; };
  int n; vector<int> vis; int visc;
  vector<int> fa, fae; vector<vector<int>> g;
  vector<ep> e; vector<C> pi;
  MinCostCirculation(int n_) : n(n_), vis(n), visc(0), g(n), pi(n) {}
  void add_edge(int u, int v, F fl, C cs) {
    g[u].emplace_back((int)e.size());
    e.emplace_back(v, fl, cs);
    g[v].emplace_back((int)e.size());
    e.emplace_back(u, 0, -cs);
  }
  C phi(int x) {
    if (fa[x] == -1) return 0;
    if (vis[x] == visc) return pi[x];
    return vis[x] = visc, pi[x] = phi(fa[x]) - e[fae[x]].cost;
  }
  int lca(int u, int v) {
    for (; u != -1 || v != -1; swap(u, v)) if (u != -1) {
      if (vis[u] == visc) return u;
      vis[u] = visc;
      u = fa[u];
    }
    return -1;
  }
  void pushflow(int x, C &cost) {
    int v = e[x ^ 1].to, u = e[x].to;
    ++visc;
    if (int w = lca(u, v); w == -1) {
      while (v != -1)
        swap(x ^= 1, fae[v]), swap(u, fa[v]), swap(u, v);
    } else {
      int z = u, dir = 0; F f = e[x].flow;
      vector<int> cyc = {x};
      for (int d : {0, 1})
        for (int i = (d ? u : v); i != w; i = fa[i]) {
          cyc.push_back(fae[i] ^ d);
          if (chmin(f, e[fae[i] ^ d].flow)) z = i, dir = d;
        }
      for (int i : cyc) {
        e[i].flow -= f; e[i ^ 1].flow += f;
        cost += f * e[i].cost;
      }
      if (dir) x ^= 1, swap(u, v);
      while (u != z)
        swap(x ^= 1, fae[v]), swap(u, fa[v]), swap(u, v);
    }
  }
  void dfs(int u) {
    vis[u] = visc;
    for (int i : g[u])
      if (int v = e[i].to; vis[v] != visc and e[i].flow)
        fa[v] = u, fae[v] = i, dfs(v);
  }
  C simplex() {
    C cost = 0;
    fa.assign(g.size(), -1); fae.assign(e.size(), -1);
    ++visc; dfs(0);
    for (int fail = 0; fail < ssize(e); )
      for (int i = 0; i < ssize(e); i++)
        if (e[i].flow and e[i].cost < phi(e[i ^ 1].to) - phi(e[i].to))
          fail = 0, pushflow(i, cost), ++visc;
        else ++fail;
    return cost;
  }

  vector<F> get_cap() {
    vector<F> res;
    for (int i = 0; i < ssize(e); i += 2)
      res.push_back(e[i ^ 1].flow);
    return res;
  }
  vector<C> get_potential() {
    vector<C> d(n);
    vector<int> inq(n, true);
    queue<int> q;
    for (int i = 0; i < n; i++)
      q.push(i);

    while (!q.empty()) {
      int u = q.front(); q.pop();
      inq[u] = false;
      for (int i : g[u]) if (e[i].flow) {
        int v = e[i].to;
        if (d[v] > d[u] + e[i].cost) {
          d[v] = d[u] + e[i].cost;
          if (!inq[v]) {
            inq[v] = true;
            q.push(v);
          }
        }
      }
    }
    return d;
  }
};

using i128 = __int128;
using lld = int64_t;

namespace std {
  ostream &operator<<(ostream &os, const i128 &x) {
    if (x < 0) return os << '-' << -x;
    if (x < 10) return os << int(x % 10);
    return os << x / 10 << int(x % 10);
  }
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  auto c = vector(n, vector(k, 0));
  auto a = vector(k, 0);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++) {
      cin >> c[i][j];
    }
  for (int i = 0; i < k; i++) {
    cin >> a[i];
  }

  const int S = n + k, T = n + k + 1, N = T + 1;
  MinCostCirculation<lld, i128> flow(N);

  for (int i = 0; i < k; i++) {
    flow.add_edge(S, i, a[i], 0);
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++)
      flow.add_edge(j, i + k, 1, c[i][j]);
  for (int i = 0; i < n; i++)
    flow.add_edge(i + k, T, 1, 0);

  lld LARGE = 1e18;
  flow.add_edge(T, S, n, -LARGE);

  auto cost = flow.simplex();
  // auto [f, cost] = flow.simplex();
  auto cap = flow.get_cap();
  auto f = cap.back();
  assert(f == n);
  cout << cost + i128(LARGE) * f << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 2
1 2
1 3
1 4
1 5
1 6
1 7
3 4

output:

12

result:

ok answer is '12'

Test #2:

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

input:

3 3
1 2 3
2 4 6
6 5 4
1 1 1

output:

8

result:

ok answer is '8'

Test #3:

score: 0
Accepted
time: 4ms
memory: 4280kb

input:

1000 10
734 303 991 681 755 155 300 483 702 442
237 256 299 675 671 757 112 853 759 233
979 340 288 377 718 199 935 666 576 842
537 363 592 349 494 961 864 727 84 813
340 78 600 492 118 421 478 925 552 617
517 589 716 7 928 638 258 297 706 787
266 746 913 978 436 859 701 951 137 44
815 336 471 720 2...

output:

92039

result:

ok answer is '92039'

Test #4:

score: 0
Accepted
time: 46ms
memory: 9480kb

input:

5000 10
14 114 254 832 38 904 25 147 998 785
917 694 750 372 379 887 247 817 999 117
802 15 799 515 316 42 69 247 95 144
727 398 509 725 682 456 369 656 693 955
923 1 681 631 962 826 233 963 289 856
165 491 488 832 111 950 853 791 929 240
509 843 667 970 469 260 447 477 161 431
514 903 627 236 144 3...

output:

461878

result:

ok answer is '461878'

Test #5:

score: 0
Accepted
time: 185ms
memory: 16536kb

input:

10000 10
307 205 765 487 504 526 10 581 234 583
448 443 39 992 976 363 335 588 588 169
920 787 896 822 47 358 230 631 136 299
141 159 414 852 922 945 513 76 111 189
616 104 83 792 24 68 164 975 615 472
150 108 848 517 7 153 107 283 452 165
94 370 910 662 226 720 975 214 324 407
636 65 963 859 590 3 ...

output:

919745

result:

ok answer is '919745'

Test #6:

score: 0
Accepted
time: 1525ms
memory: 82488kb

input:

50000 10
819 49 278 985 747 872 146 129 898 569
929 427 54 846 136 475 448 304 591 428
238 844 664 991 990 863 308 571 867 958
775 690 792 697 557 325 824 654 303 833
542 942 262 534 501 575 273 60 701 488
733 855 810 405 294 909 638 975 801 836
382 265 818 765 240 69 980 889 472 211
629 434 128 389...

output:

4558242

result:

ok answer is '4558242'

Test #7:

score: 0
Accepted
time: 938ms
memory: 82492kb

input:

50000 10
381 642 238 598 634 432 828 277 275 239
963 771 114 457 411 717 85 260 527 664
138 832 923 332 197 371 30 412 47 568
266 38 327 563 564 14 943 698 881 747
627 788 567 438 371 524 490 674 809 839
322 680 178 515 376 355 928 880 827 446
702 107 650 811 360 226 283 138 357 489
121 364 656 377 ...

output:

4595976

result:

ok answer is '4595976'

Test #8:

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

input:

5 3
2 4 5
5 9 9
2 7 9
4 2 2
4 1 7
3 3 3

output:

12

result:

ok answer is '12'

Test #9:

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

input:

10 7
1 9 9 3 5 5 7
6 1 6 3 4 3 6
9 6 8 5 5 2 7
3 8 8 6 6 6 3
5 8 1 9 7 9 5
3 2 3 7 7 8 7
4 1 2 3 3 3 7
8 1 7 3 4 2 7
7 1 1 9 2 7 3
4 9 8 9 6 8 9
10 2 1 1 2 1 1

output:

21

result:

ok answer is '21'

Test #10:

score: 0
Accepted
time: 144ms
memory: 13748kb

input:

10000 7
6 5 9 8 5 5 5
2 4 5 2 7 8 9
7 3 7 2 6 8 8
8 1 6 4 8 6 9
2 3 8 1 3 5 5
1 5 6 1 3 1 6
2 7 7 3 5 9 5
1 9 9 6 8 5 5
1 4 2 4 6 7 7
8 4 1 5 2 2 1
7 9 9 5 5 1 2
9 7 1 3 9 5 9
6 7 3 6 3 8 3
7 7 2 4 2 4 5
5 5 9 8 2 4 9
9 5 8 4 7 2 9
4 3 4 8 4 3 3
8 3 7 9 6 6 6
5 4 5 2 6 3 9
4 9 5 6 1 3 2
4 1 2 6 6 5 ...

output:

44137

result:

ok answer is '44137'

Test #11:

score: 0
Accepted
time: 446ms
memory: 14352kb

input:

10000 6
3 4 1 4 5 4
4 4 3 7 7 2
6 6 8 1 9 9
2 5 5 1 7 3
9 7 7 3 3 8
5 7 1 2 6 3
2 8 9 4 9 1
4 8 3 2 1 7
4 9 2 3 8 5
1 6 2 2 9 1
1 4 9 8 9 6
3 8 3 7 6 1
3 1 5 7 9 5
5 3 8 1 2 8
5 1 8 3 9 4
1 5 4 5 5 4
9 4 1 8 8 4
5 6 7 5 8 2
3 1 6 2 3 1
2 7 4 8 5 6
5 4 3 2 5 1
8 5 4 7 3 2
7 5 2 3 1 1
3 1 1 7 3 1
2 6 ...

output:

21143

result:

ok answer is '21143'

Test #12:

score: 0
Accepted
time: 1125ms
memory: 14176kb

input:

10000 6
26621560 22574851 99124663 42644108 73831692 34062679
10875678 33632518 99379217 52587402 68258572 82863
6133022 1452838 27530175 15603746 10928055 64045100
4919237 15636901 89763 37033224 76358345 23420261
87262364 92257115 7193645 40262131 78897499 70538741
45451167 2937593 39330094 300263...

output:

176215561116

result:

ok answer is '176215561116'

Test #13:

score: 0
Accepted
time: 1054ms
memory: 14528kb

input:

10000 6
505488678 436228096 333558553 129070925 123808864 36937787
503324046 79831519 80269630 548781256 374673233 280839716
209008459 554326459 255308141 256669834 530478297 51026940
351489261 459988802 392737197 83890293 359338753 331620684
201060883 194683095 375867041 232603637 138654087 1929412...

output:

1034670171939

result:

ok answer is '1034670171939'

Test #14:

score: 0
Accepted
time: 1097ms
memory: 13776kb

input:

10000 6
89916134 29433813 59399087 464898320 558107935 422188143
547054926 559929858 728302681 5219270 834478116 259909510
816488311 368359373 194676880 330286055 245200722 87979527
63366579 585173909 706460949 49644677 770070184 329255152
314412303 288716719 333799370 614570900 406350296 696208263
...

output:

1773428571657

result:

ok answer is '1773428571657'

Test #15:

score: 0
Accepted
time: 7ms
memory: 14736kb

input:

10000 6
66237379 181806248 509510118 323698055 917981861 381020346
891370175 602465447 651904218 27588579 475265754 430666261
874613865 991962519 265069683 393546179 987679666 717041057
675429255 645133077 623980032 953549198 946201757 765785432
954715369 623518217 681467056 740740198 415802185 5827...

output:

1439875611641

result:

ok answer is '1439875611641'

Test #16:

score: 0
Accepted
time: 792ms
memory: 14524kb

input:

10000 6
109501946 925691998 114115135 829446594 173795627 891153669
264844500 481537403 422647594 964796147 386517450 581623444
921172582 375091327 237314301 608361127 357677517 595119843
119651751 659029470 938251974 210093064 369958476 821941442
411555569 328723790 979811779 137795697 512892726 39...

output:

1561994475072

result:

ok answer is '1561994475072'

Test #17:

score: 0
Accepted
time: 7ms
memory: 13484kb

input:

10000 6
214242852 862344228 444719752 969065314 468060368 232241544
786803413 801480781 666280058 298466884 28589081 984412665
436101075 178002287 220595950 936235752 308790537 65171840
853973125 4091452 854764838 888615836 554917488 238207448
499378894 673682464 358195819 455995859 52657992 5920135...

output:

1444521827648

result:

ok answer is '1444521827648'

Test #18:

score: 0
Accepted
time: 11ms
memory: 27344kb

input:

50000 2
3 8
8 3
4 9
4 7
6 6
9 1
5 6
2 1
8 6
4 1
2 5
2 7
8 4
1 9
2 6
6 5
9 8
2 2
8 8
4 1
3 2
5 8
9 7
5 4
8 6
9 4
3 8
7 3
4 3
6 4
1 1
5 5
1 6
2 8
8 1
3 2
7 6
3 7
5 2
6 3
6 2
1 2
1 4
3 2
8 1
9 4
4 8
6 9
5 7
4 2
5 1
1 7
4 9
9 9
4 3
4 1
9 9
1 4
7 5
7 2
5 1
3 2
7 7
6 7
7 9
1 2
9 1
5 2
7 6
9 9
3 4
9 6
8 4
...

output:

176124

result:

ok answer is '176124'

Test #19:

score: -100
Time Limit Exceeded

input:

50000 2
7 8
7 2
1 7
4 7
9 3
9 5
6 7
9 5
6 5
5 9
2 8
7 2
1 1
3 6
5 1
6 1
3 4
4 3
7 4
5 7
8 6
4 2
7 7
7 6
3 1
2 6
5 5
4 7
2 2
6 7
4 7
7 7
6 5
5 1
7 6
7 1
4 9
3 4
8 9
1 2
4 5
6 7
5 1
6 9
7 8
7 3
3 2
2 6
6 5
1 1
7 5
6 5
8 4
6 1
9 2
6 3
2 8
9 2
9 2
4 1
6 5
8 7
3 4
2 2
9 5
4 4
8 9
8 1
9 5
5 7
6 7
8 6
8 8
...

output:


result: