/* -*- coding: utf-8 -*- * * 2477.cc: No.2477 Drifting - yukicoder */ #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 long long LINF = 1LL << 62; /* typedef */ using ll = long long; using vl = vector; using pii = pair; using vpii = vector; using si = set; using vsi = vector; using mii = map; /* global variables */ vpii nbrs[MAX_N]; mii exis[MAX_N]; vsi excs[MAX_N]; vl dp[MAX_N]; /* 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--; nbrs[u].push_back({v, w}); } int k; scanf("%d", &k); for (int i = 0; i < k; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--, b--, c--; auto mit = exis[b].find(a); int j = 0; if (mit != exis[b].end()) j = mit->second; else j = exis[b][a] = exis[b].size(); if (j < excs[b].size()) excs[b][j].insert(c); else { si cs; cs.insert(c); excs[b].push_back(cs); } } for (int u = 0; u < n; u++) { excs[u].push_back(si()); dp[u].assign((int)excs[u].size(), LINF); } dp[0][0] = 0; for (int u = 0; u < n; u++) for (int i = 0; i < excs[u].size(); i++) if (dp[u][i] < LINF) { for (auto [v, w]: nbrs[u]) { if (! excs[u][i].empty() && excs[u][i].find(v) != excs[u][i].end()) continue; auto mit = exis[v].find(u); int j = (mit != exis[v].end()) ? mit->second : exis[v].size(); dp[v][j] = min(dp[v][j], dp[u][i] + w); } } ll mind = *min_element(dp[n - 1].begin(), dp[n - 1].end()); printf("%lld\n", (mind < LINF) ? mind : -1LL); return 0; }