QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#340915#8136. Rebellious EdgeduckierWA 36ms14204kbC++141.8kb2024-02-29 14:06:032024-02-29 14:06:04

Judging History

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

  • [2024-02-29 14:06:04]
  • 评测
  • 测评结果:WA
  • 用时:36ms
  • 内存:14204kb
  • [2024-02-29 14:06:03]
  • 提交

answer

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

const int MAXN = 2e5 + 5;
int t, n, m, u, v, from[MAXN], in[MAXN];
pair<int, int> dist[MAXN];
vector<pair<int, int>> to[MAXN];

void reset() {
    for (int i = 1; i <= n; i++) {
        in[i] = 0;
        from[i] = 1e18;
        dist[i] = { 1e18, 0 };
        to[i].clear();
    }
}

signed main() {
    cin.tie(NULL)->sync_with_stdio(0);
    cin >> t;
    while (t--) {
        cin >> n >> m;
        reset();
        for (int i = 0; i < m; i++) {
            int a, b, w;
            cin >> a >> b >> w;
            if (a > b) {
                u = b;
                v = a;
                continue;
            }
            to[a].push_back({ b, w });
            from[b] = min(from[b], w);
        }
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            if(from[i] != 1e18) ans += from[i];
        }
        int tot = 0;
        if(u==1){
            cout<<ans<<"\n";
            continue;
        }
        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
        pq.push({ 0, 1, 0 });
        while (pq.size()) {
            int d = get<0>(pq.top()), c = get<1>(pq.top()), p = get<2>(pq.top());
            pq.pop();
            if (dist[c].first <= d) continue;
            dist[c] = { d, p };
            for (auto& nxt : to[c]) {
                int v = nxt.first, w = nxt.second;
                if (d + w < dist[v].first && v != u) pq.push({ w, v, c });
            }
        }
        int c = v;
        while (c) {
            in[c] = 1;
            c = dist[c].second;
        }
        tot = dist[v].first;
        for (int i = 1; i <= n; i++) {
            if (!in[i]) tot += from[i];
        }
        cout << min(ans, tot) << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
5 6
1 2 4
1 3 2
2 3 0
3 4 1
4 5 1
5 2 1
4 4
1 2 4
1 3 6
1 4 8
4 2 1000000
3 3
1 2 100
2 1 10
2 3 1000

output:

5
18
1100

result:

ok 3 number(s): "5 18 1100"

Test #2:

score: -100
Wrong Answer
time: 36ms
memory: 10612kb

input:

50000
4 5
2 4 998973548
2 3 501271695
4 1 778395982
1 4 32454891
1 2 757288934
4 5
1 4 720856669
2 3 665098951
3 4 407461517
2 1 866914401
1 2 457859826
4 5
1 2 75288664
1 4 624893594
3 2 458973866
2 4 769074655
2 3 834773751
4 5
2 3 237060634
2 4 297307882
3 4 200383586
1 2 42856502
3 1 16574713
4 ...

output:

1291015520
1530420294
1534956009
480300722
1366795927
1541095843
2493849488
858095911
1039553235
793861088
635606317
1051598350
612891589
1265994009
564107126
1678219738
1556463491
93634961
960978736
984886788
1696503797
783703820
1969660290
1431417780
1515267731
977157479
1937478556
654475526
13213...

result:

wrong answer 9th numbers differ - expected: '1034153425', found: '1039553235'