Welcome to My Luogu Space .
[Title] effect
An undirected graph, the length of the sides are \ (1 \) , each point is dyed \ ( "R, B, Y , G" \) one of four colors.
Figure traveling rule is: start from the first step, each a four-step cycle, the weekly four steps must step on a different color above (initial position count the first step, not the last few complete cycle can not step on the same color ).
The first is now \ (k \) individuals were at a different starting point, ask if they can converge on the same point, if you find the shortest distance.
【answer】
Shaped pressure BFS + .
Binary numbers (0001,0010,0100,1000 \) \ represent stepped on something and colors.
The \ (k \) together to run wide search point, the search process if found to be a certain point \ (k \) individuals have passed, then the current state of the traveled distance is the shortest distance. I did not find that they can not converge.
It must be re-sentenced by the state for a point, because different states come to the same point, to go after the program is different.
[Code]
// output format !!
// long long !!
#include <bits/stdc++.h>
#define H puts("HYX")
const int MAXN = 50000+10;
struct DATA{
int x, id, len, f;
void build(int _x, int _id, int _c){
this->f = 0, this->x = _x, this->id = _id;
this->f = _c, this->len = 0;
}
};
int n, m, k, f1[MAXN][15][20], f2[MAXN][15], cnt[MAXN], c[MAXN], s[15];
int hed[MAXN], nex[MAXN*100], e[MAXN*100], ct;
char in[MAXN];
std::queue<DATA> Q;
int bfs(){
while(!Q.empty()){
DATA x = Q.front(); Q.pop();
for(int i=hed[x.x]; i; i=nex[i]){
DATA y = x;
if((y.f&c[e[i]])) continue;
if(f1[e[i]][y.id][y.f|=c[e[i]]]) continue;
if(!f2[e[i]][y.id]) f2[e[i]][y.id] = 1, ++cnt[e[i]];
if(cnt[e[i]] == k) return y.len+1;
if(y.f == 15) y.f = 0;
f1[e[i]][y.id][y.f] = 1;
++y.len, y.x = e[i];
Q.push(y);
}
}
return -1;
}
int main(){
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
scanf("%d%d%d%d", &n, &n, &m, &k);
for(int i=1; i<=k; ++i) scanf("%d", s+i);
scanf("%s", in+1);
for(int i=1; i<=n; ++i){
if(in[i] == 'R') c[i] = 1;
if(in[i] == 'B') c[i] = 2;
if(in[i] == 'Y') c[i] = 4;
if(in[i] == 'G') c[i] = 8;
}
for(int i=1; i<=k; ++i){
DATA a; a.build(s[i], i, c[s[i]]);
cnt[s[i]] = f2[s[i]][i] = 1, Q.push(a);
}
while(m--){
int a, b; scanf("%d%d", &a, &b);
e[++ct] = b, nex[ct] = hed[a], hed[a] = ct;
e[++ct] = a, nex[ct] = hed[b], hed[b] = ct;
}
printf("%d", bfs());
return 0;
}