QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#186580#7185. Poor Studentsucup-team1430#TL 2332ms9488kbC++203.1kb2023-09-24 04:26:552023-09-24 04:26:56

Judging History

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

  • [2023-09-24 04:26:56]
  • 评测
  • 测评结果:TL
  • 用时:2332ms
  • 内存:9488kb
  • [2023-09-24 04:26:55]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define ld long double
#define pb push_back
#define sws cin.tie(0)->sync_with_stdio(false);
#define endl '\n'

using namespace std;

const int N = 1e5+10;
const ll MOD = 998244353;
// const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

struct Dinitz {
  struct Edge {
    ll v, u, cap, flow=0, cost;
    Edge(ll v, ll u, ll cap, ll cost) : v(v), u(u), cap(cap), cost(cost) {}
  };

  int n, s, t;
  Dinitz(int n, int s, int t) : n(n), s(s), t(t) {
    adj.resize(n);
  }

  vector<Edge> edges;
  vector<vector<int>> adj;
  void add_edge(int v, int u, ll cap, ll cost) {
    edges.emplace_back(v, u, cap, cost);
    adj[v].pb((int)edges.size()-1);
    edges.emplace_back(u, v, 0, -cost);
    adj[u].pb((int)edges.size()-1);
  }

  vector<ll> dist;
  bool spfa() {
    dist.assign(n, LLINF);

    queue<int> Q;
    vector<bool> inqueue(n, false);

    dist[s] = 0;
    Q.push(s);
    inqueue[s] = true;

    while (!Q.empty()) {
      int v = Q.front(); Q.pop();
      inqueue[v] = false;

      for (auto eid : adj[v]) {
        auto const& e = edges[eid];
        if (e.cap - e.flow <= 0) continue;
        if (dist[e.u] > dist[e.v] + e.cost) {
          dist[e.u] = dist[e.v] + e.cost;
          if (!inqueue[e.u]) {
            Q.push(e.u);
            inqueue[e.u] = true;
          }
        }
      }
    }

    return dist[t] != LLINF;
  }

  ll cost = 0;
  vector<ll> ptr;
  ll dfs(int v, ll f) {
    if (v == t || f == 0) return f;
    for (auto &cid = ptr[v]; cid < ((int)adj[v].size());) {
      auto eid = adj[v][cid];
      auto &e = edges[eid];
      cid++;
      if (e.cap - e.flow <= 0) continue;
      if (dist[e.v] + e.cost != dist[e.u]) continue;
      ll newf = dfs(e.u, min(f, e.cap-e.flow));
      if (newf == 0) continue;
      e.flow += newf;
      edges[eid^1].flow -= newf;
      cost += e.cost * newf;
      return newf;
    }
    return 0;
  }

  ll total_flow = 0;
  pair<ll, ll> flow() {
    while (spfa()) {
      ptr.assign(n, 0);
      while (ll newf = dfs(s, LLINF))
        total_flow += newf;
    }
    return {total_flow, cost};
  }
};


int32_t main()
{
    #ifndef LOCAL
    sws;
    #endif

    int n, k; cin >> n >> k;
    int limit = n + k + 10;
    int source = limit - 1, sink = limit - 2;
    Dinitz dnc(limit, source, sink);

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


    for (int i=0;i<n;i++) {
        dnc.add_edge(source, i, 1, 0);
    }
    for (int i=0;i<n;i++) {
        for (int j=0;j<k;j++) {
            dnc.add_edge(i, j+n, 1, cost[i][j]);
        }
    }

    for (int j=0;j<k;j++) {
        dnc.add_edge(j+n, sink, a[j], 0);
    }
    
    auto [f, c] = dnc.flow();

    cout << c << endl;

    

    return 0;
}

详细

Test #1:

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

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: 3448kb

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: 56ms
memory: 4588kb

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: 2332ms
memory: 9488kb

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: -100
Time Limit Exceeded

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:


result: