QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#210791 | #5034. >.< | DJ2006 | 0 | 0ms | 0kb | C++14 | 3.3kb | 2023-10-11 20:09:07 | 2023-10-11 20:09:07 |
answer
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
const long long INF = 1e18;
const int N = 2e5;
int n, m, k, cnt, cnt2;
std::vector<std::pair<int, int> > adj[N + 5];
int Root[N * 2 + 5];
struct node {
int ls, rs, to, dis;
} tree[N * 2 * 20 + 5];
void upd(int &u, int L, int R, int pos, int to, int dis = -1) {
tree[++cnt2] = tree[u]; u = cnt2;
if (L == R) {
tree[u].to = to;
if (dis != -1) {
tree[u].dis = dis;
}
return ;
}
int mid = (L + R) >> 1;
if (pos <= mid) {
upd(tree[u].ls, L, mid, pos, to, dis);
} else {
upd(tree[u].rs, mid + 1, R, pos, to, dis);
}
}
std::pair<int, int> ask(int u, int L, int R, int pos) {
if (u == 0) {
return {-1, -1};
} else if (L == R) {
return {tree[u].to, tree[u].dis};
}
int mid = (L + R) >> 1;
if (pos <= mid) {
return ask(tree[u].ls, L, mid, pos);
} else {
return ask(tree[u].rs, mid + 1, R, pos);
}
}
std::map<int, int> trans[N * 2 + 5];
int id[N * 2 + 5];
int end[N * 2 + 5];
int fail[N * 2 + 5];
void ins(const std::vector<int> &v) {
int u = 0;
for (int i = 0; i < (int)v.size(); i++) {
if (trans[u].count(v[i])) {
u = trans[u][v[i]];
} else {
u = trans[u][v[i]] = ++cnt;
id[cnt] = v[i];
}
}
end[u]++;
}
void build() {
std::queue<int> q;
for (int i = 1; i <= n; i++) {
q.push(fail[i] = i);
for (const auto &e : adj[i])
upd(Root[i], 1, n, e.first, e.first, e.second);
}
while (!q.empty()) {
int u = q.front(); q.pop();
Root[u] = Root[fail[u]];
for (const auto &e : trans[u]) {
fail[e.second] = ask(Root[u], 1, n, e.first).first;
if (fail[e.second] != -1) {
upd(Root[u], 1, n, e.first, e.second);
q.push(e.second);
}
}
}
}
long long dis[N * 2 + 5];
bool vis[N * 2 + 5];
std::priority_queue<std::pair<long long, int> > q;
void dfs(int &u, int L, int R, int from) {
if (u != 0) {
if (L == R) {
int to = tree[u].to, dist = tree[u].dis;
if (!end[to] && dis[from] + dist < dis[to]) {
dis[to] = dis[from] + dist;
q.push({-dis[to], to});
}
} else {
int mid = (L + R) >> 1;
dfs(tree[u].ls, L, mid, from);
dfs(tree[u].rs, mid + 1, R, from);
}
}
u = 0;
}
long long dijkstra(int start) {
if (end[start]) {
return -1;
}
for (int i = 1; i <= cnt; i++)
dis[i] = INF;
q.push({0LL, start});
dis[start] = 0;
while (!q.empty()) {
int u = q.top().second; q.pop();
if (id[u] == n) return dis[u];
else if (vis[u]) continue;
vis[u] = true;
dfs(Root[u], 1, n, u);
}
return -1;
}
void solve() {
std::cin >> n >> m >> k;
for (int i = 1, u, v, w; i <= m; i++) {
std::cin >> u >> v >> w;
adj[u].push_back({v, w});
}
for (int i = 1; i <= n; i++) {
trans[0][i] = ++cnt;
id[cnt] = i;
}
while (k--) {
int Len;
std::cin >> Len;
std::vector<int> v(Len);
for (int i = 0; i < Len; i++)
std::cin >> v[i];
ins(v);
}
build();
long long ans = dijkstra(1);
std::cout << ans << "\n";
}
int main() {
#ifdef ONLINE_JUDGE
freopen("path.in", "r", stdin);
freopen("path.out", "w", stdout);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cerr.tie(nullptr);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
return 0;
}
詳細信息
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%