QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#397241#8232. Yet Another Shortest Path QueryqawszxWA 933ms411940kbC++144.0kb2024-04-23 20:17:222024-04-23 20:17:22

Judging History

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

  • [2024-04-23 20:17:22]
  • 评测
  • 测评结果:WA
  • 用时:933ms
  • 内存:411940kb
  • [2024-04-23 20:17:22]
  • 提交

answer

#include <bits/stdc++.h>
#define upd(x, y) x = min(x, y)
using namespace std;
int scan() {
  int x = 0; char ch = getchar();
  for (; !isdigit(ch); ch = getchar());
  for (; isdigit(ch); ch = getchar()) x = 10 * x + ch - '0';
  return x;
}
int top, stk[11];
void print(int x) {
  if (x < 0) putchar('-'), x = -x;
  do stk[++top] = x % 10, x /= 10; while (x);
  while (top) putchar('0' + stk[top--]);
}
const int N = 1e6 + 5;
int n, m, q, deg[N], s[N], t[N], ans[N], rec[N];
queue<int> Q;
unordered_map<int, int> E[N];
vector<int> tmp, ask3[N], ask4[N];
vector<pair<int, int>> ver[N], Rv[N], Lu[N];
vector<pair<int, pair<int, int>>> ask1[N], ask2[N];
int main() {
  n = scan(), m = scan();
  while (m--) {
    int u = scan(), v = scan(), w = scan();
    ++deg[u], ++deg[v];
    if (E[u].find(v) == E[u].end() || w < E[u][v]) E[u][v] = E[v][u] = w;
    ver[u].emplace_back(v, w), ver[v].emplace_back(u, w);
  }
  for (int u = 1; u <= n; ++u) if (deg[u] <= 5) Q.emplace(u), deg[u] = -1;
  while (!Q.empty()) {
    int u = Q.front(); Q.pop(), deg[u] = -2;
    for (auto it: ver[u]) {
      int v = it.first, w = it.second;
      if (deg[v] > -1 && --deg[v] <= 5) Q.emplace(v), deg[v] = -1;
      if (deg[v] > -2) Rv[u].emplace_back(v, w), Lu[u].emplace_back(v, w);
    }
  }
  q = scan(), memset(ans, 0x3f, sizeof ans);
  for (int i = 1; i <= q; ++i) {
    s[i] = scan(), t[i] = scan();
    if (E[s[i]].find(t[i]) != E[s[i]].end()) ans[i] = E[s[i]][t[i]];
    for (auto it: Rv[s[i]]) {
      int v = it.first, w = it.second;
      ask1[v].emplace_back(i, make_pair(t[i], w)),
      ask2[t[i]].emplace_back(i, make_pair(v, w));
      if (E[v].find(t[i]) != E[v].end()) upd(ans[i], w + E[v][t[i]]);
    }
    for (auto it: Lu[t[i]]) {
      int u = it.first, w = it.second;
      ask1[s[i]].emplace_back(i, make_pair(u, w)),
      ask2[u].emplace_back(i, make_pair(s[i], w));
      if (E[u].find(s[i]) != E[u].end()) upd(ans[i], E[s[i]][u] + w);
    }
    ask3[s[i]].emplace_back(i), ask4[t[i]].emplace_back(i);
  }
  memset(rec, 0x3f, sizeof rec);
  for (int u = 1; u <= n; ++u) {
    if (!ask1[u].empty()) {
      for (auto it: ver[u]) {
        int v = it.first, w = it.second;
        for (auto it2: Rv[v]) {
          int v2 = it2.first, w2 = it2.second;
          upd(rec[v2], w + w2), tmp.emplace_back(v2);
        }
      }
      for (auto it: ask1[u])
        upd(ans[it.first], rec[it.second.first] + it.second.second);
      for (int v: tmp) rec[v] = rec[0];
      tmp.clear();
    }
    if (!ask2[u].empty()) {
      for (auto it: ver[u]) {
        int v = it.first, w = it.second;
        for (auto it2: Lu[v]) {
          int v2 = it2.first, w2 = it2.second;
          upd(rec[v2], w + w2), tmp.emplace_back(v2);
        }
      }
      for (auto it: ask2[u])
        upd(ans[it.first], rec[it.second.first] + it.second.second);
      for (int v: tmp) rec[v] = rec[0];
      tmp.clear();
    }
    if (!ask3[u].empty()) {
      for (auto it: ver[u]) {
        int v = it.first, w = it.second;
        for (auto it2: Rv[v]) {
          int v2 = it2.first, w2 = it2.second;
          upd(rec[v2], w + w2);
          for (auto it3: Rv[v2]) {
            int v3 = it3.first, w3 = it3.second;
            upd(rec[v3], w + w2 + w3), tmp.emplace_back(v3);
          }
        }
      }
      for (auto i: ask3[u]) upd(ans[i], rec[t[i]]);
      for (int v: tmp) rec[v] = rec[0];
      tmp.clear();
    }
    if (!ask4[u].empty()) {
      for (auto it: ver[u]) {
        int v = it.first, w = it.second;
        for (auto it2: Lu[v]) {
          int v2 = it2.first, w2 = it2.second;
          for (auto it3: Lu[v2]) {
            int v3 = it3.first, w3 = it3.second;
            upd(rec[v3], w + w2 + w3), tmp.emplace_back(v3);
          }
        }
      }
      for (auto i: ask4[u]) upd(ans[i], rec[s[i]]);
      for (int v: tmp) rec[v] = rec[0];
      tmp.clear();
    }
  }
  for (int i = 1; i <= q; ++i)
    print(ans[i] == ans[0] ? -1 : ans[i]), putchar('\n');
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 39ms
memory: 230176kb

input:

6 9
1 2 4
2 3 6
3 6 5
6 5 3
5 4 2
4 1 3
3 4 9
1 3 100
5 3 1
5
1 3
1 6
3 4
3 5
2 5

output:

6
8
3
1
7

result:

ok 5 number(s): "6 8 3 1 7"

Test #2:

score: 0
Accepted
time: 27ms
memory: 230356kb

input:

6 4
1 2 1
2 3 1
3 4 1
4 5 1
3
1 4
1 5
1 6

output:

3
-1
-1

result:

ok 3 number(s): "3 -1 -1"

Test #3:

score: -100
Wrong Answer
time: 933ms
memory: 411940kb

input:

40005 79608
1 2 70031203
1 3 99924845
1 4 61645659
1 5 9324967
2 3 15761918
3 4 62534796
4 5 35260314
5 2 35948540
6 2 23727405
6 7 83302920
7 3 31010426
7 8 75060393
8 4 94275932
8 9 99663793
9 5 81701979
9 6 439297
10 6 46955645
10 11 89514237
11 7 21257310
11 12 53896253
12 8 67933315
12 13 26161...

output:

67939740
98626153
87468872
118079530
87231838
33540000
39077540
28890287
71499276
109666599
35540030
86594058
109139178
69992586
74759546
61826120
60273579
42180224
20780937
101841239
66254498
53162468
26773053
145820980
97737294
113632191
146521607
77975961
90997182
134837183
50611485
59036058
1017...

result:

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