#include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define SQ(a) ((a)*(a)) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long int64; typedef pair<int, int> PII; typedef pair<int64, int64> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; const int INF = 1000 * 1000 * 1000; int n, m, res; vector<int> nxt[510]; vector<int> target; int ps[1010]; int trans[15][510][1010]; int when1[510]; int when2[510][510]; int cont(int k, int c){ if(k == m) return k; while(k > 0 && c != target[k]) k = ps[k-1]; if(c == target[k]) ++k; return k; } int main(){ scanf("%d %d", &n, &m); FWD(i,1,n+1){ int k; scanf("%d", &k); FWD(j,0,k){ int a; scanf("%d", &a); nxt[i].push_back(a); } } FWD(i,0,m){ int a; scanf("%d", &a); target.push_back(a); } FWD(i,1,m){ ps[i] = ps[i-1]; while(ps[i] > 0 && target[i] != target[ps[i]]) ps[i] = ps[ps[i]-1]; if(target[i] == target[ps[i]]) ps[i]++; } FWD(i,1,n+1){ FWD(k,0,m+1){ trans[0][i][k] = cont(k, i); } } FWD(r,1,15){ FWD(i,1,n+1){ FWD(k,0,m+1){ int s = k; for(int u : nxt[i]) s = trans[r-1][u][s]; trans[r][i][k] = s; } } } res = INF; FWD(i,1,n+1) when1[i] = INF; priority_queue<PII> Q; Q.push(PII(-1,1)); while(!Q.empty()){ int u = Q.top().nd; int d = -Q.top().st; Q.pop(); if(when1[u] != INF) continue; when1[u] = d; for(int v : nxt[u]) Q.push(PII(-d-1, v)); } FWD(t,0,15) FWD(i,1,n+1) if(trans[t][i][0] == m){ //printf("%d %d %d\n", t, i, when1[i]); res = min(res, when1[i]+t); } FWD(i,1,n+1) FWD(j,1,n+1) when2[i][j] = INF; priority_queue<pair<int, PII>> R; FWD(i,1,n+1){ if(when1[i] == INF) continue; FWD(j,0,SIZE(nxt[i])-1) R.push(make_pair(-when1[i]-1, PII(nxt[i][j], nxt[i][j+1]))); } while(!R.empty()){ int d = -R.top().st; int u = R.top().nd.st; int v = R.top().nd.nd; R.pop(); if(when2[u][v] != INF) continue; when2[u][v] = d; R.push(make_pair(-d-1, PII(nxt[u].back(), nxt[v].front()))); } FWD(t,0,15) FWD(i,1,n+1) FWD(j,1,n+1) if(trans[t][j][trans[t][i][0]] == m){ res = min(res, when2[i][j]+t); } if(res == INF) res = -1; printf("%d\n", res); return 0; }
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define SQ(a) ((a)*(a)) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long int64; typedef pair<int, int> PII; typedef pair<int64, int64> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; const int INF = 1000 * 1000 * 1000; int n, m, res; vector<int> nxt[510]; vector<int> target; int ps[1010]; int trans[15][510][1010]; int when1[510]; int when2[510][510]; int cont(int k, int c){ if(k == m) return k; while(k > 0 && c != target[k]) k = ps[k-1]; if(c == target[k]) ++k; return k; } int main(){ scanf("%d %d", &n, &m); FWD(i,1,n+1){ int k; scanf("%d", &k); FWD(j,0,k){ int a; scanf("%d", &a); nxt[i].push_back(a); } } FWD(i,0,m){ int a; scanf("%d", &a); target.push_back(a); } FWD(i,1,m){ ps[i] = ps[i-1]; while(ps[i] > 0 && target[i] != target[ps[i]]) ps[i] = ps[ps[i]-1]; if(target[i] == target[ps[i]]) ps[i]++; } FWD(i,1,n+1){ FWD(k,0,m+1){ trans[0][i][k] = cont(k, i); } } FWD(r,1,15){ FWD(i,1,n+1){ FWD(k,0,m+1){ int s = k; for(int u : nxt[i]) s = trans[r-1][u][s]; trans[r][i][k] = s; } } } res = INF; FWD(i,1,n+1) when1[i] = INF; priority_queue<PII> Q; Q.push(PII(-1,1)); while(!Q.empty()){ int u = Q.top().nd; int d = -Q.top().st; Q.pop(); if(when1[u] != INF) continue; when1[u] = d; for(int v : nxt[u]) Q.push(PII(-d-1, v)); } FWD(t,0,15) FWD(i,1,n+1) if(trans[t][i][0] == m){ //printf("%d %d %d\n", t, i, when1[i]); res = min(res, when1[i]+t); } FWD(i,1,n+1) FWD(j,1,n+1) when2[i][j] = INF; priority_queue<pair<int, PII>> R; FWD(i,1,n+1){ if(when1[i] == INF) continue; FWD(j,0,SIZE(nxt[i])-1) R.push(make_pair(-when1[i]-1, PII(nxt[i][j], nxt[i][j+1]))); } while(!R.empty()){ int d = -R.top().st; int u = R.top().nd.st; int v = R.top().nd.nd; R.pop(); if(when2[u][v] != INF) continue; when2[u][v] = d; R.push(make_pair(-d-1, PII(nxt[u].back(), nxt[v].front()))); } FWD(t,0,15) FWD(i,1,n+1) FWD(j,1,n+1) if(trans[t][j][trans[t][i][0]] == m){ res = min(res, when2[i][j]+t); } if(res == INF) res = -1; printf("%d\n", res); return 0; } |