/* -*- coding: utf-8 -*- * * 2477.cc: No.2477 Drifting - yukicoder */ #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int MAX_M = 200000; const int MAX_GN = MAX_N + MAX_M; const int MAX_K = 200000; const long long LINF = 1LL << 62; /* typedef */ using ll = long long; using pii = pair; using vpii = vector; using si = set; using mpii = map; using pli = pair; /* global variables */ vpii es[MAX_N], nbrs[MAX_GN]; int orgs[MAX_GN]; si exs[MAX_GN]; ll ds[MAX_GN]; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); u--, v--; es[u].push_back({v, w}); } for (int i = 0; i < n; i++) orgs[i] = i; int gn = n; mpii evs; int k; scanf("%d", &k); while (k--) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--, b--, c--; auto mit = evs.find({a, b}); int u = -1; if (mit != evs.end()) u = mit->second; else { u = evs[{a, b}] = gn++; orgs[u] = b; } exs[u].insert(c); } //printf(" gn=%d\n", gn); for (int u = 0; u < gn; u++) { int ou = orgs[u]; for (auto [v, w]: es[ou]) { if (! exs[u].empty() && exs[u].find(v) != exs[u].end()) continue; auto mit = evs.find({ou, v}); if (mit != evs.end()) v = mit->second; nbrs[u].push_back({v, w}); } } evs.clear(); fill(ds, ds + gn, LINF); ds[0] = 0; priority_queue q; q.push({0, 0}); ll mind = LINF; while (! q.empty()) { auto [ud, u] = q.top(); q.pop(); ud = -ud; if (ds[u] != ud) continue; if (u == n - 1) { mind = ud; break; } for (auto [v, w]: nbrs[u]) { ll vd = ud + w; if (ds[v] > vd) { ds[v] = vd; q.push({-vd, v}); } } } printf("%lld\n", (mind < LINF) ? mind : -1LL); return 0; }