#include<iostream>
#include<iomanip>
#include<cstring>
#include<queue>
#include<cmath>
#include<vector>
using namespace std;
typedef pair<int, int> PII;
const int N = 1e6 + 10;
int n, m;
int h[N], e[N], ne[N], idx;
int w[N];
vector<int> p[N];
int dist[N];
bool st[N];
void add(int a, int b, int c) {
e[idx] = b, ne[idx] = h[a], w[idx] = c , h[a] = idx++;
}
bool find(int u1,int u2) {
for (int kk : p[u2]) if (kk == u1) return false;
return true;
}
int dijkstra() {
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({ 0,1 });
while (!heap.empty()) {
auto t = heap.top();
heap.pop();
int ver = t.second;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; ~i; i = ne[i]) {
int j = e[i];
if (dist[j] > dist[ver] + w[i]&&find(dist[ver] + w[i],j)||dist[j] > dist[ver] + w[i]&&j==n) {
dist[j] = dist[ver] + w[i];
heap.push({ dist[j],j });
}
}
}
if (dist[n] == 0x3f3f3f3f) return -1;
else return dist[n];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
memset(h, -1, sizeof h);
cin >> n >> m;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
if (k) for (int j = 1; j <= k; j++) {
int d;
cin >> d;
p[i].push_back(d);
}
}
cout << dijkstra() << endl;
return 0;
}
````
https://www.acwing.com/activity/content/problem/content/8147/
```