QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#173464#7185. Poor Studentsucup-team133#TL 1ms3640kbC++232.8kb2023-09-09 23:42:442023-09-09 23:42:44

Judging History

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

  • [2023-09-09 23:42:44]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3640kb
  • [2023-09-09 23:42:44]
  • 提交

answer

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector c(n, vector<int>(k));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < k; j++) {
            cin >> c[i][j];
        }
    }
    vector<int> a(k);
    for (int i = 0; i < k; i++) {
        cin >> a[i];
    }
    long long ans = 0;
    multiset<pair<int, int>> g[11][11] = {};
    for (int i = 0; i < k; i++) {
        for (int j = 0; j < a[i]; j++) {
            g[i][k].emplace(0, -1);
        }
    }
    const long long inf = 1e18;
    auto Flow = [&](int s) {
        vector<vector<long long>> d(k + 1, vector<long long>(k + 1, inf));
        for (int i = 0; i < k + 1; i++) {
            d[i][i] = 0;
        }
        for (int i = 0; i < k + 1; i++) {
            for (int j = 0; j < k + 1; j++) {
                if (i != j && !g[i][j].empty()) {
                    d[i][j] = g[i][j].begin()->first;
                }
            }
        }
        for (int z = 0; z < k + 1; z++) {
            for (int x = 0; x < k + 1; x++) {
                for (int y = 0; y < k + 1; y++) {
                    d[x][y] = min(d[x][y], d[x][z] + d[z][y]);
                }
            }
        }
        ans += d[s][k];
        int t = k;
        set<int> st;
        for (int i = 0; i < k; i++) {
            st.emplace(i);
        }
        while (s != t) {
            for (int i : st) {
                if (!g[i][t].empty() && d[s][t] == d[s][i] + g[i][t].begin()->first) {
                    auto p = *g[i][t].begin();
                    int z = p.second;
                    if (z == -1) {
                        g[i][t].erase(g[i][t].begin());
                        g[t][i].emplace(0, -1);
                    } else {
                        for (int j = 0; j < k; j++) {
                            if (i != j) {
                                g[i][j].erase(make_pair(c[z][j] - c[z][i], z));
                            }
                            if (t != j) {
                                g[t][j].emplace(make_pair(c[z][j] - c[z][t], z));
                            }
                        }
                    }
                    t = i;
                    st.erase(t);
                    break;
                }
            }
        }
    };
    for (int i = 0; i < n; i++) {
        int s = (int) (min_element(c[i].begin(), c[i].end()) - c[i].begin());
        for (int x = 0; x < k; x++) {
            if (x != s) {
                g[s][x].emplace(c[i][x] - c[i][s], i);
            }
        }
        ans += c[i][s];
    }
    for (int i = 0; i < n; i++) {
        int s = (int) (min_element(c[i].begin(), c[i].end()) - c[i].begin());
        Flow(s);
    }
    cout << ans << '\n';
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3580kb

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: 1ms
memory: 3640kb

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

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:


result: