QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#588796#5034. >.<SunsetGlow950 0ms0kbC++143.4kb2024-09-25 14:35:502024-09-25 14:35:52

Judging History

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

  • [2024-09-25 14:35:52]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-09-25 14:35:50]
  • 提交

answer

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

using ll = long long;
const int MXN = 200005;
const int MXV = MXN * 2;
const int MXD = MXN * 20;
const ll INF = 1e18;
int N, M, K, ndsz(1), lstp[MXN], V;
vector<pair<int, int>> to[MXN];
map<int, int> tto[MXN];
bool beend[MXN];
int fail[MXN], que[MXN], qh, qt;
int srt[MXN], sson[MXD][2], sval[MXD], ssz(1); // segment tree
ll dis[MXV], ans(INF);

int read() {
  int x(0), c(getchar());
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + (c - '0'), c = getchar();
  return x;
}
void write(long long x) {
  static char buf[30];
  if (!x) {
    putchar('0');
    return;
  }
  int d(0);
  while (x) buf[d++] = x % 10 + '0', x /= 10;
  while (d) putchar(buf[--d]);
}

inline int copynode(int p) {
  int q(ssz++);
  sson[q][0] = sson[p][0], sson[q][1] = sson[p][1];
  sval[q] = sval[p];
  return q;
}

void insert(int &p, int l, int r, int k, int v) {
  p = copynode(p);
  if (r - l == 1) {
    sval[p] = v;
    return;
  }
  int m((l + r) >> 1);
  if (k < m) insert(sson[p][0], l, m, k, v);
  else insert(sson[p][1], m, r, k, v);
}
int query(int p, int l, int r, int k) {
  if (!p || r - l == 1) return sval[p];
  int m((l + r) >> 1);
  if (k < m) return query(sson[p][0], l, m, k);
  return query(sson[p][1], m, r, k);
}

int main() {
  freopen("path.in", "r", stdin);
  freopen("path.out", "w", stdout);
  // input
  N = read(), M = read(), K = read();
  for (int u(0), v(0), w(0); M--;) {
    u = read() - 1, v = read() - 1, w = read();
    to[u].push_back(make_pair(v, w));
  }
  lstp[0] = -1;
  for (int plen(0); K--;) {
    plen = read();
    int p(0);
    for (int n(0); plen--;) {
      n = read() - 1;
      if (tto[p].find(n) == tto[p].end())
        lstp[ndsz] = n, tto[p][n] = ndsz++;
//    cout << p << '-' << n << '-';
      p = tto[p][n];
//    cout << p << endl;
    }
    beend[p] = true;
  }
  // build aho-corasick automaton
  fail[0] = -1;
  for (auto i : tto[0])
    insert(srt[0], 0, N, i.first, i.second),
    que[qt++] = i.second;
  while (qh != qt) {
    int c(que[qh++]);
//  cout << c << ':' << fail[c] << endl;
    srt[c] = srt[fail[c]];
    for (auto i : tto[c]) {
      insert(srt[c], 0, N, i.first, i.second);
      fail[i.second] = query(srt[fail[c]], 0, N, i.first);
      que[qt++] = i.second;
    }
  }
  // find shortest path
  // format (p on automaton, q on the graph) ->
  // q if p == 0; otherwise p + n - 1.
  V = N + ndsz - 1;
  fill(dis, dis + V, INF);
  int bg(query(srt[0], 0, N, 0));
  if (bg) bg += N - 1;
  dis[bg] = 0;
  priority_queue<pair<ll, int>> pq;
  pq.push(make_pair(0, bg));
  while (!pq.empty()) {
    ll d(-pq.top().first);
    int c(pq.top().second);
    pq.pop();
    if (d != dis[c]) continue;
    int p(0), q(0);
    if (c >= N) p = c - (N - 1), q = lstp[p];
    else p = 0, q = c;
//  cout << c << ' ' << p << ' ' << q << ' ' << d << endl;
    if (beend[p]) continue;
    if (q == N - 1) ans = min(ans, d);
    for (auto e : to[q]) {
      int n(e.first);
      int np(query(srt[p], 0, N, n));
      if (!np) np = n;
      else np += N - 1;
      ll nd(d + e.second);
      if (nd < dis[np])
//      cout << c << "->" << np << ':' << nd << endl,
        dis[np] = nd, pq.push(make_pair(-nd, np));
    }
  }
  printf("%lld\n", ans == INF ? -1 : ans);
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Dangerous Syscalls

Test #1:

score: 0
Dangerous Syscalls

input:

35 100 0
34 7 447733879
24 20 187005344
14 34 654502303
2 31 865194349
20 33 9517055
33 15 991889891
24 33 395599993
13 16 237525328
9 5 373850826
30 34 391470240
10 7 650077565
26 10 400825980
34 27 189924713
19 27 907609573
20 10 614945312
10 5 960007605
1 7 984076202
32 25 539699728
24 31 2553027...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Dangerous Syscalls

Test #11:

score: 0
Dangerous Syscalls

input:

50000 200000 1
7542 37166 116613326
3581 43961 629220971
12873 42953 440313807
31744 5286 697832848
25882 12748 106667302
34760 29676 181570340
41570 9240 885513989
22227 35688 63657981
43180 29194 174058940
8977 41899 48262624
7465 18291 600002514
46925 9281 951474878
2115 31162 373758881
5386 3798...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #2:

0%