QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#309863#8136. Rebellious Edgeucup-team197#WA 60ms3588kbC++142.3kb2024-01-20 21:39:172024-01-20 21:39:17

Judging History

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

  • [2024-01-20 21:39:17]
  • 评测
  • 测评结果:WA
  • 用时:60ms
  • 内存:3588kb
  • [2024-01-20 21:39:17]
  • 提交

answer

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

struct edge {
  int u, v, w;

  bool operator<(const edge &o) const { return w < o.w; }
};

struct dsu {
  vector<int> par, subtree;

  dsu(int n) {
    par.resize(n + 1);
    subtree.resize(n + 1);

    for (int i = 1; i <= n; i++) {
      par[i] = i;
      subtree[i] = 1;
    }
  }

  int findParent(int x) {
    if (par[x] == x)
      return x;

    par[x] = findParent(par[x]);
    return par[x];
  }

  void unite(int x, int y) {
    if (subtree[x] <= subtree[y]) {
      par[x] = y;
      subtree[y] += subtree[x];
    } else {
      par[y] = x;
      subtree[x] += subtree[y];
    }
  }
};

int64_t getMST(const vector<vector<pair<int, int>>> &g) {
  int64_t res = 0;
  set<edge> currentEdges;

  for (int i = 0; i < g[1].size(); i++) {
    currentEdges.insert({1, g[1][i].first, g[1][i].second});
  }

  vector<bool> used(g.size(), false);
  used[1] = true;

  while (!currentEdges.empty()) {
    auto e = *currentEdges.begin();
    currentEdges.erase(currentEdges.begin());
    if (!used[e.v]) {
      used[e.v] = true;
      res += e.w;
      for (int i = 0; i < g[e.v].size(); i++) {
        currentEdges.insert({e.v, g[e.v][i].first, g[e.v][i].second});
      }
    }
  }

  return res;
}

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

  int t;
  cin >> t;

  while (t--) {
    int n, m;
    cin >> n >> m;

    int specialA, specialB, specialW;
    vector<vector<pair<int, int>>> g(n + 1);
    for (int i = 0; i < m; i++) {
      int a, b, w;
      cin >> a >> b >> w;

      if (a < b)
        g[a].push_back({b, w});
      else {
        specialA = a;
        specialB = b;
        specialW = w;
      }
    }

    int64_t ans = getMST(g);

    if (specialB == 1) {
      cout << ans << endl;
      continue;
    }

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < g[i].size(); j++) {
        if (g[i][j].first == specialA)
          g[i][j].first = specialB;
      }
    }

    for (int j = 0; j < g[specialA].size(); j++) {
      g[specialB].push_back(g[specialA][j]);
    }

    ans = min(ans, getMST(g) + specialW);

    cout << ans << endl;
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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
1159156124
480300722
1366795927
1541095843
2567005573
858095911
1034153425
688839791
605832428
1051598350
612891589
1265994009
517769091
1899616226
1556463491
93634961
960978736
984886788
1696503797
1002892611
1969660290
1431417780
1515267731
977157479
1937478556
654475526
1401...

result:

wrong answer 3rd numbers differ - expected: '1534956009', found: '1159156124'