Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <queue>
- #define N 1005
- #define oo 0x3f3f3f3f
- using namespace std;
- using VI=vector<int>;
- using VII=vector<VI>;
- ifstream fin("zoomba.in");
- ofstream fout("zoomba.out");
- queue<int> q;
- VII g, d;
- VI a;
- int n,m,k,z,x,y;
- void Lee(int v)
- {
- int x;
- while(!q.empty())
- {
- x=q.front();
- q.pop();
- for(auto y:g[x])
- if(d[y][v]>d[x][v]+1)
- d[y][v]=d[x][v]+1, q.push(y);
- }
- }
- int main()
- {
- fin>>n>>m>>k>>z;
- a=VI(n+1);
- g=VII(n+1);
- d=VII(n+1,VI((1<<k), oo));
- for(int i=0;i<k;i++)
- fin>>a[i];
- for(int i=1;i<=m;i++)
- {
- fin>>x>>y;
- g[x].push_back(y);
- g[y].push_back(x);
- }
- for(int i=0;i<k;i++)
- {
- d[a[i]][1<<i]=0;
- q.push(a[i]);
- Lee(1<<i);
- }
- for(int cnt=2;cnt<=k; cnt++)
- for(int j=1; j<(1<<k);j++)
- if(__builtin_popcount(j)==cnt)
- {
- for(int i=1;i<=n;i++)
- {
- for(int p=1;p<j;++p)
- if((p|j)==j)
- d[i][j]=min(d[i][j], d[i][p]+d[i][j^p]);
- q.push(i);
- }
- Lee(j);
- }
- if(d[z][(1<<k)-1]==oo)fout<<-1;
- else fout<<d[z][(1<<k)-1];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement