QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#273126#7883. Takeout Deliveringucup-team484#WA 131ms49512kbC++171.4kb2023-12-02 21:26:432023-12-02 21:26:43

Judging History

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

  • [2023-12-02 21:26:43]
  • 评测
  • 测评结果:WA
  • 用时:131ms
  • 内存:49512kb
  • [2023-12-02 21:26:43]
  • 提交

answer

#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define st first
#define nd second
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int pre[N], suf[N], par[N];
vector<int> adj[N];

int qry(int x) { return x == par[x] ? x : par[x] = qry(par[x]); }

void join(int x, int y) {
  if (qry(x) != qry(y))
    adj[x].push_back(y), adj[y].push_back(x);
  par[qry(x)] = qry(y);
}

void dfs(int u, int f, int w) {
  (f == 1 ? pre : suf)[u] = w;
  for (int v: adj[u]) {
    if (f == 1 && pre[v] != 0 || f == 2 && suf[v] != 0)
      continue;
    dfs(v, f, w);
  }
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  int n, m; cin >> n >> m;
  vector<array<int, 3>> arr;
  for (int i = 0; i < m; i++) {
    int u, v, w; cin >> u >> v >> w;
    arr.push_back({w, u, v});
  }
  iota(par, par + n + 1, 0);
  sort(all(arr));
  for (auto [w, u, v]: arr) {
    if (qry(u) == 1 || qry(v) == 1)
      dfs(qry(u) == 1 ? v : u, 1, w);
    if (qry(u) == n || qry(v) == n)
      dfs(qry(u) == n ? v : u, 2, w);
    join(u, v);
  }
  int ans = 2e9;
  for (auto [w, u, v]: arr) {
    vector<int> tmp = {pre[u], suf[v], w};
    sort(all(tmp));
    ans = min(ans, tmp[1] + tmp[2]);
    tmp = {pre[v], suf[u], w};
    sort(all(tmp));
    ans = min(ans, tmp[1] + tmp[2]);
  }
  cout << ans << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 33192kb

input:

4 6
1 2 2
1 3 4
1 4 7
2 3 1
2 4 3
3 4 9

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: -100
Wrong Answer
time: 131ms
memory: 49512kb

input:

300000 299999
80516 80517 597830404
110190 110191 82173886
218008 218009 954561262
250110 250111 942489774
66540 66541 156425292
34947 34948 239499776
273789 273790 453201232
84428 84429 439418398
98599 98600 326095035
55636 55637 355015760
158611 158612 684292473
43331 43332 43265001
171621 171622 ...

output:

966659373

result:

wrong answer 1st numbers differ - expected: '1999991697', found: '966659373'