1 #include 2 #include<string> 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include<set> 11 #include 12 #include 13 #include 14 #include 15 #include 16 using namespace std; 17 18 #define au auto 19 #define debug(i) cout<<" "<"< 20 #define mfor(i,a,b) for(register int i=(a);i<=(b);i++) 21 #define mrep(i,a,b) for(register int i=(a);i>=(b);i--) 22 #define LLL __int128 23 #define Re register 24 #define il inline 25 #define mem(a,b) memset(a,(b),sizeof(a)) 26 typedef pair<int, int> intpair; 27 typedef long long int LL; 28 const int INF = 0x3f3f3f3f; 29 const long long int INFLL = 0x3f3f3f3f3f3f3f3f; 30 31 int cnt; 32 const int maxn = 5010; 33 34 struct Edge 35 { 36 int u, nxt; 37 int w; 38 }e[maxn]; 39 40 int head[maxn]; 41 42 void add(int a, int b, int w) 43 { 44 e[++cnt].u = b; 45 e[cnt].nxt = head[a]; 46 head[a] = cnt; 47 e[cnt].w = w; 48 } 49 50 int n, m, k, num; 51 int d; 52 int f[maxn]; 53 bool clo[maxn][maxn]; 54 bool now[maxn]; 55 56 typedef struct 57 { 58 bool operator ()(const intpair &a, const intpair &b)const 59 { 60 return a.second > b.second; 61 } 62 }cmp; 63 64 int dis[maxn]; 65 bool vis[maxn]; 66 priority_queue, cmp>q; 67 68 void dijstra(int x) 69 { 70 mem(dis, 0x3f); 71 mem(vis, false); 72 dis[x] = 0; 73 q.push(intpair(x, dis[x])); 74 while (!q.empty()) 75 { 76 intpair t = q.top(); 77 q.pop(); 78 if (vis[t.first]) continue; 79 vis[t.first] = true; 80 for (int i = head[t.first]; i != -1; i = e[i].nxt) 81 { 82 int u = e[i].u; 83 int w = e[i].w; 84 if (!vis[u] && dis[u] > t.second + w && !now[u]) 85 { 86 dis[u] = t.second + w; 87 q.push(intpair(u, dis[u])); 88 } 89 } 90 } 91 } 92 93 94 95 int main() 96 { 97 mem(f, 0x3f); 98 mem(head, -1); 99 cin >> n >> m >> k >> num; 100 mfor(i, 1, num) 101 { 102 int a, b, w; 103 cin >> a >> b >> w; 104 add(a, b, w); 105 add(b, a, w); 106 } 107 cin >> d; 108 mfor(i, 1, d) 109 { 110 int a, b, c; 111 cin >> a >> b >> c; 112 mfor(j, b, c) 113 { 114 clo[j][a] = true; 115 } 116 } 117 f[0] = -k; 118 mfor(i, 1, n) 119 { 120 mem(now, false); 121 mrep(j, i, 1) 122 { 123 mfor(l, 1, m) 124 if (clo [j][l]) now[l] = true; 125 dijstra(1); 126 if (dis[m] >= 1e8) break; 127 f[i] = min(f[i], f[j - 1] + (i - j + 1) * dis[m] + k); 128 } 129 } 130 cout << f[n]; 131 return 0; 132 }