G. Kirill and Company
time limit per test 3 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
题目描述
Kirill lives on a connected undirected graph of n vertices and m edges at vertex 1. One fine evening he gathered f friends, the i-th friend lives at the vertex hi. So all friends are now in the vertex 1, the i-th friend must get to his home to the vertex hi.
The evening is about to end and it is time to leave. It turned out that k (k≤6) of his friends have no cars, and they would have to walk if no one gives them a ride. One friend with a car can give a ride to any number of friends without cars, but only if he can give them a ride by driving along one of the shortest paths to his house.
For example, in the graph below, a friend from vertex hi=5 can give a ride to friends from the following sets of vertices: [2,3], [2,4], [2], [3], [4], but can’t give a ride to friend from vertex 6 or a set [3,4].
Kirill wants as few friends as possible to have to walk. Help him find the minimum possible number.
输入描述
The first line of input data contains an integer t (1≤t≤10^3) — the number of test cases in the test.
The first line of the test case contains two integers n and m (2≤n≤104, n−1≤m≤min(10^4,n⋅(n−1)2)) — the number of vertices and edges, respectively.
The next m lines of the test case contain a description of the edges, two integers each u and v (1≤u,v≤n, u≠v) — indexes of vertices connected by an edge. It is guaranteed that there is at most one edge between any pair of vertices (i.e. no multiple edges in the graph).
Then follows line containing the number f (1≤f≤10^4) — the number of Kirill’s friends.
The next line of the test case contains f integers: h1,h2,…,hf (2≤hi≤n) — the vertices in which they live. Some vertices may be repeated.
The next line of the set contains the number k (1≤k≤min(6,f)) — the number of friends without cars.
The last line of each test case contains k integers: p1,p2,…,pk (1≤pi≤f, pi<pi+1) — indexes of friends without cars.
It is guaranteed that the sum of n over all cases does not exceed 10^4, as well as the sums of m and f.
输出描述
Output t lines, each of which contains the answer to the corresponding test case. As an answer, output a single integer — the minimum possible number of friends who will have to walk.
题意
给定一张 n 个点 m 条边的无向连通图 给定 f 个人家的点 以及 k 个没车的人员编号 现所有人都在点 1 有车的人可携带任意个数人 仅当其家的点在有车的人回家的最短路径上 现询问最少有几个人需要走回家(即假定的一种方案没有被携带的人数)
思路
观察 k 的范围为 6 很小 考虑使用状压dp 使用6位 2 进制数描述携带状态 通过 bfs 找到各点的最短路上能携带的不同方案 记为 d p [ 从 1 出发到达的点 ] [ 状态 ] = 0 / 1 (不可达或可达) dp[从1出发到达的点][状态]=0/1(不可达或可达) dp[从1出发到达的点][状态]=0/1(不可达或可达) 最后开 2 k 2^k 2k 的 bitset 记录对应二进制数的状态是否可达即可 将有车的人的状态依次转移 即记录这个人出发前的各情况是否以及这个人能携带的情况数按位或操作更新为新的状态 注意本次更新是基于这个人之前的状态 故本次更新的状态不能用于本次状态的继续更新 故需要开两个 bitset 滚动操作(这也是为什么不用数组的原因 bitset之间可以直接赋值) 最后遍历这 2 k 2^k 2k 种情况记录能携带的最大人数
Code
#include<bits/stdc++.h>
using namespace std;
#define __T int csT;scanf("%d",&csT);while(csT--)
const int mod=1e9+7;
const int maxn=1e4+3;
int n,m,f,k,u,v,cnt;
int h[maxn],dp[maxn][103],home[maxn],vis[maxn],p[maxn];
struct node{
int nx,to;
}e[2*maxn];
map<int,int> ntgo;
int bit(int x)
{
int sum=0;
while(x>0)
{
if(x%2==1)++sum;
x/=2;
}
return sum;
}
void bfs()
{
int at,st;
dp[1][0]=1;
vis[1]=1;
vector<pair<int,int>> q,nx;
q.push_back({1,0});
while(q.size())
{
nx.clear();
for(int i=0;i<q.size();++i)
{
vis[q[i].first]=1;
}//防止当前层的点互跑
for(int i=0;i<q.size();++i)
{
at=q[i].first;
st=q[i].second;
for(int j=h[at];~j;j=e[j].nx)
{
int to=e[j].to;
if(vis[to]==1)continue;
int nst=st;
for(int z=0;z<k;++z)
{
if(home[p[z]]==to)
{
nst=nst|(1<<z);
}
}
if(dp[to][nst]==0)
{
dp[to][nst]=1;
nx.push_back({to,nst});
}
}
}
q=nx;
}
}
inline void sol()
{
cnt=0;
memset(dp,0,sizeof dp);
memset(vis,0,sizeof vis);
memset(h,-1,sizeof h);
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
{
scanf("%d%d",&u,&v);
e[++cnt].to=v;
e[cnt].nx=h[u];
h[u]=cnt;
e[++cnt].to=u;
e[cnt].nx=h[v];
h[v]=cnt;
}
scanf("%d",&f);
for(int i=1;i<=f;++i)
{
scanf("%d",&home[i]);
}
scanf("%d",&k);
ntgo.clear();
for(int i=0;i<k;++i)
{
scanf("%d",&p[i]);
ntgo[p[i]]=1;
}
bfs();
int to;
bitset<64> nw,per;
per[0]=1;//起始只有空集状态可达
for(int i=1;i<=f;++i)
{
if(ntgo[i]==1)continue;//没车人无法带人
to=home[i];
nw=per;
for(int j=0;j<(1<<k);++j)
{
for(int z=0;z<(1<<k);++z)
{
if(dp[to][z]==1)
{
if(per[j]==1)
{
nw[j|z]=1;
}
}
}
}
per=nw;
}
int gt=0;
for(int i=0;i<(1<<k);++i)
{
if(per[i]==1)
{
gt=max(gt,bit(i));
}
}
printf("%d\n",k-gt);
}
int main()
{
__T
sol();
return 0;
}