QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#526851#7185. Poor Studentsucup-team1766WA 739ms30316kbC++175.2kb2024-08-21 22:39:162024-08-21 22:39:16

Judging History

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

  • [2024-08-21 22:39:16]
  • 评测
  • 测评结果:WA
  • 用时:739ms
  • 内存:30316kb
  • [2024-08-21 22:39:16]
  • 提交

answer

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

struct Student {
    int cost, id;
    Student() : cost(-1), id(-1) {}
    Student(int c, int i) : cost(c), id(i) {}
    bool operator<(const Student &o) const {
        if (cost == o.cost) {
            return id < o.id;
        }
        return cost < o.cost;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k;
    cin >> n >> k;
    vector<vector<int>> 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];
    }

    // match[i] = exam student i is matched to, -1 if not matched yet
    vector<int> match(n, -1);
    // entry_sets[i] stores the set of unmatched students that could be matched to exam i,
    // sorted by their match costs.
    vector<set<Student>> entry_sets(k);
    // pair_sets[i][j] stores the set of students currently matched to exam i that can be
    // transferred to match exam j, sorted by their transfer costs.
    vector<vector<set<Student>>> pair_sets(k, vector<set<Student>>(k));

    // Initialize entry sets
    for (int i = 0; i < k; i++) {
        for (int j = 0; j < n; j++) {
            entry_sets[i].insert(Student(c[j][i], j));
        }
    }

    // Max flow min cost
    long long cost = 0;
    while (true) {
        // 0th node is a dummy node with edge weights to every exam that can be taken
        // by unmatched student. Edges in this graph represent a student that can be
        // transferred between the exams.
        vector<vector<Student>> graph(k + 1, vector<Student>(k + 1, Student(-1, -1)));
        for (int i = 0; i < k; i++) {
            if (!entry_sets[i].empty()) {
                graph[0][i + 1] = *entry_sets[i].begin();
            }
            for (int j = 0; j < k; j++) {
                if (i != j && !pair_sets[i][j].empty()) {
                    graph[i + 1][j + 1] = *pair_sets[i][j].begin();
                }
            }
        }

        // Bellman-Ford to find shortest path in this graph. This path corresponds to
        // an augmenting path in the flow network.
        // dp[i][j] = {min distance, previous node} of a path from node 0 to node i
        // of length <= j.
        vector<vector<pair<long long, int>>> dp(k + 1, vector<pair<long long, int>>(k + 1, {LONG_LONG_MAX, -1}));
        dp[0][0] = {0, -1};
        for (int i = 1; i <= k; i++) {
            for (int j = 0; j <= k; j++) {
                dp[j][i].first = dp[j][i - 1].first;
                dp[j][i].second = j;
            }
            for (int j = 0; j <= k; j++) {
                for (int x = 0; x <= k; x++) {
                    // j -> x
                    if (x == j || graph[j][x].id < 0 || dp[j][i - 1].first == LONG_LONG_MAX) {
                        continue;
                    }
                    long long cur_dist = dp[j][i - 1].first + graph[j][x].cost;
                    if (cur_dist < dp[x][i].first) {
                        dp[x][i].first = cur_dist;
                        dp[x][i].second = j;
                    }
                }
            }
        }

        int cur = -1;
        for (int i = 1; i <= k; i++) {
            if (a[i - 1] > 0 && (cur == -1 || dp[i - 1][k].first < dp[cur][k].first)) {
                cur = i;
            }
        }
        if (cur == -1) {
            break;
        }
        a[cur - 1]--;
        
        vector<Student> path;
        vector<int> exams;
        for (int i = k; i >= 0; i--) {
            int prev = dp[cur][i].second;
            if (prev == -1) {
                break;
            }
            if (prev != cur) {
                path.push_back(graph[prev][cur]);
                exams.push_back(cur - 1);
            }
            cur = prev;
        }

        reverse(path.begin(), path.end());
        reverse(exams.begin(), exams.end());
        for (int i = 0; i < path.size(); i++) {
            // Student will be unmatched from its current exam,
            // remove from transfer sets from that exam.
            if (i > 0) {
                int cur_exam = match[path[i].id];
                for (int j = 0; j < k; j++) {
                    if (j != cur_exam) {
                        pair_sets[cur_exam][j].erase(Student(c[path[i].id][j] - c[path[i].id][cur_exam], path[i].id));
                    }
                }
            } else {
                // Student is now matched to an exam, remove from all entry sets.
                for (int j = 0; j < k; j++) {
                    entry_sets[j].erase(Student(c[path[i].id][j], path[i].id));
                }
            }

            // Since student was just matched to a new exam, add student to transfer sets
            // from that exam.
            for (int j = 0; j < k; j++) {
                if (j != exams[i]) {
                    pair_sets[exams[i]][j].insert(Student(c[path[i].id][j] - c[path[i].id][exams[i]], path[i].id));
                }
            }

            match[path[i].id] = exams[i];
            cost += path[i].cost;
        }
    }
    cout << cost << "\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: 3620kb

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

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: 43ms
memory: 6512kb

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: 86ms
memory: 9032kb

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: 733ms
memory: 30316kb

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: 739ms
memory: 30244kb

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: -100
Wrong Answer
time: 0ms
memory: 3532kb

input:

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

output:

21

result:

wrong answer expected '12', found '21'