G. Kirill and Company bfs+状压dp+bitset

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].
The vertices where friends without cars live are highlighted in green, and with cars — in red.

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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
用C语言解决下列问题:Kirill wants to weave the very beautiful blanket consisting of n×m of the same size square patches of some colors. He matched some non-negative integer to each color. Thus, in our problem, the blanket can be considered a B matrix of size n×m consisting of non-negative integers. Kirill considers that the blanket is very beautiful, if for each submatrix A of size 4×4 of the matrix B is true: A11⊕A12⊕A21⊕A22=A33⊕A34⊕A43⊕A44, A13⊕A14⊕A23⊕A24=A31⊕A32⊕A41⊕A42, where ⊕ means bitwise exclusive OR Kirill asks you to help her weave a very beautiful blanket, and as colorful as possible! He gives you two integers n and m . Your task is to generate a matrix B of size n×m , which corresponds to a very beautiful blanket and in which the number of different numbers maximized. Input The first line of input data contains one integer number t (1≤t≤1000 ) — the number of test cases. The single line of each test case contains two integers n and m (4≤n,m≤200) — the size of matrix B . It is guaranteed that the sum of n⋅m does not exceed 2⋅105 . Output For each test case, in first line output one integer cnt (1≤cnt≤n⋅m) — the maximum number of different numbers in the matrix. Then output the matrix B (0≤Bij<263) of size n×m . If there are several correct matrices, it is allowed to output any one. It can be shown that if there exists a matrix with an optimal number of distinct numbers, then there exists among suitable matrices such a B that (0≤Bij<263) .
03-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柯西可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值