1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e5 + 5; int N, M, C, P, U, V, L; int Head[Maxn], Next[Maxn], E[Maxn], W[Maxn], Cur; int Bead[Maxn], Bext[Maxn], B[Maxn]; int Dist[Maxn], Bist[Maxn], Ans(-1); bool Vis[Maxn], Plus[Maxn];
inline void AddEdge(int U, int V, int L) { Next[++Cur] = Head[U], Head[U] = Cur; E[Cur] = V, W[Cur] = L; Bext[Cur] = Bead[V], Bead[V] = Cur; B[Cur] = U; }
inline void SPFA(int S) { memset (Dist, 0x3f, sizeof Dist); memset (Vis, 0, sizeof Vis); Dist[S] = 0; queue <int> Q; Q.push(S); while (!Q.empty()) { int Now = Q.front(); Vis[Now] = 0; Q.pop(); if (Plus[Now]) Dist[Now] = 0; for (int i = Head[Now]; i; i = Next[i]) { if (Dist[E[i]] <= Dist[Now] + W[i]) continue; if (Dist[Now] + W[i] > C) continue; Dist[E[i]] = Dist[Now] + W[i]; if (!Vis[E[i]]) Q.push(E[i]), Vis[E[i]] = 1; } } }
inline void BSPFA(int S) { memset (Bist, 0x3f, sizeof Bist); memset (Vis, 0, sizeof Vis); Bist[S] = 0; queue <int> Q; Q.push(S); while (!Q.empty()) { int Now = Q.front(); Vis[Now] = 0; Q.pop(); if (Plus[Now]) Bist[Now] = 0; for (int i = Bead[Now]; i; i = Bext[i]) { if (Bist[B[i]] <= Bist[Now] + W[i]) continue; if (Bist[Now] + W[i] > C) continue; Bist[B[i]] = Bist[Now] + W[i]; if (!Vis[B[i]]) Q.push(B[i]), Vis[B[i]] = 1; } } }
int main() { scanf ("%d %d %d", &N, &M, &C); while (M--) { scanf ("%d %d %d", &U, &V, &L); AddEdge(U, V, L); } scanf ("%d", &P); while (P--) { scanf ("%d", &U); Plus[U] = 1; } scanf ("%d", &P); SPFA(1), BSPFA(N); if (Dist[N] == 0x3f3f3f3f) return puts("-1") & 0; while (P--) { scanf ("%d %d", &U, &L); if (Plus[U]) Ans = max(Ans, C * L); else Ans = max(Ans, L * (C - Dist[U] - Bist[U])); } printf ("%d\n", Ans); }
|