【JZOJ1444】交换

本文介绍如何使用双向BFS算法解决一个排列变换问题,即通过给定的交换操作将一个1到N的排列变换为有序排列1,2,3,...,N,并寻找最小的变换次数。文中详细解释了如何将排列状态压缩为数值形式,利用哈希表进行状态存储与判重,从而降低搜索复杂度。

description

给定1到N的一个排列,再给定一些允许的交换方法,要求用最少的交换次数把该排列变为1,2,3,,,N。


analysis

  • nnn比较小,考虑搜索,dfsdfsdfs不可能,bfsbfsbfs可以试试

  • 注意一点,起始状态和终止状态都已经给出了,那么用双向bfs

  • 由于搜索树从一棵变成两棵,双向复杂度是单向的开方级别,用哈希判重

  • 把当前状态压成一个十进制下的十三进制数,不超过longlonglong longlonglong,所以我们把哈希也开longlonglong longlonglong即可

  • 接下来就很简单了,从起始状态和终止状态开始扩展,一个状态出现两次直接输出退出


code

  • 由于和谐的空间限制,我的哈希开的不大,虽说是正解但TTT了一个点看情况适当打表
const
        maxn=1000007;
type
        arr=array[0..12]of longint;
var
        que1,que2,hash1,hash2:array[0..maxn]of
        record
                s:int64;
                sum:longint;
        end;
        x,y:array[0..100]of longint;
        n,m,i,j,h1,t1,h2,t2:longint;
        flag:boolean;
        temp:int64;
        a,b:arr;
procedure swap(var x,y:longint);
var
        z:longint;
begin
        z:=x;
        x:=y;
        y:=z;
end;
function turn(t:arr):int64;
var
        tp:int64;
        i:longint;
begin
        turn:=0;
        tp:=1;
        for i:=12 downto 1 do
        begin
                if t[i]>0 then
                turn:=turn+t[i]*tp;
                tp:=tp*13;
        end;
end;
procedure return(n:int64;var t:arr);
var
        i:longint;
begin
        fillchar(t,sizeof(t),0);
        for i:=12 downto 1 do
        begin
                t[i]:=n mod 13;
                n:=n div 13;
        end;
end;
function find1(x:int64):boolean;
var
        now:longint;
begin
        now:=x mod maxn;
        if hash1[now].s=0 then exit(true);
        if hash1[now].s=x then exit(false);
        now:=(now+1)mod maxn;
        while (hash1[now].s<>0)and(hash1[now].s<>x)do
        begin
                inc(now);
                if now=maxn then now:=0;
        end;
        if hash1[now].s=0 then exit(true);
        if hash1[now].s=x then exit(false);
end;
procedure into1(x:int64;y:longint);
var
        now:longint;
begin
        now:=x mod maxn;
        if hash1[now].s=0 then
        begin
                hash1[now].s:=x;
                hash1[now].sum:=y;
                exit;
        end;
        now:=(now+1)mod maxn;
        while (hash1[now].s<>0)and(hash1[now].s<>x) do
        begin
                inc(now);
                if now=maxn then now:=0;
        end;
        hash1[now].s:=x;
        hash1[now].sum:=y;
end;
function find2(x:int64):boolean;
var
        now:longint;
begin
        now:=x mod maxn;
        if hash2[now].s=0then exit(true);
        if hash2[now].s=x then exit(false);
        now:=(now+1)mod maxn;
        while (hash2[now].s<>0)and(hash2[now].s<>x)do
        begin
                inc(now);
                if now=maxn then now:=0;
        end;
        if hash2[now].s=0 then exit(true);
        if hash2[now].s=x then exit(false);
end;
procedure into2(x:int64;y:longint);
var
        now:longint;
begin
        now:=x mod maxn;
        if hash2[now].s=0 then
        begin
                hash2[now].s:=x;
                hash2[now].sum:=y;
                exit;
        end;
        now:=(now+1)mod maxn;
        while (hash2[now].s<>0)and(hash2[now].s<>x) do
        begin
                inc(now);
                if now=maxn then now:=0;
        end;
        hash2[now].s:=x;
        hash2[now].sum:=y;
end;
function lookfor(x:int64):longint;
var
        now:longint;
begin
        now:=x mod maxn;
        if hash1[now].s=x then exit(hash1[now].sum);
        while true do
        begin
                inc(now);
                if now=maxn then now:=0;
                if hash1[now].s=x then exit(hash1[now].sum);
        end;
end;
begin                                            
        readln(n,m);
        flag:=true;
        for i:=1 to n do
        begin
                read(a[i]);
                if a[i]<>i then flag:=false;
                b[i]:=i;
        end;
        if flag then
        begin
                writeln(0);
                halt;
        end;
        for i:=1 to m do readln(x[i],y[i]);
        h1:=0;
        t1:=1;
        h2:=0;
        t2:=1;
        que1[1].s:=turn(a);
        que2[1].s:=turn(b);
        repeat
                inc(h1);
                if h1=maxn then h1:=0;
                for i:=1 to m do
                begin
                        return(que1[h1].s,b);
                        swap(b[x[i]],b[y[i]]);
                        temp:=turn(b);
                        if find1(temp) then
                        begin
                                into1(temp,que1[h1].sum+1);
                                inc(t1);
                                if t1=maxn then t1:=0;
                                que1[t1].s:=temp;
                                que1[t1].sum:=que1[h1].sum+1;
                        end;
                end;
                inc(h2);
                if h2=maxn then h2:=0;
                if not find1(que2[h2].s)then
                begin
                        writeln(que2[h2].sum+lookfor(que2[h2].s));
                        halt;
                end;
                for i:=1 to m do
                begin
                        return(que2[h2].s,b);
                        swap(b[x[i]],b[y[i]]);
                        temp:=turn(b);
                        if find2(temp)then
                        begin
                                into2(temp,que2[h2].sum+1);
                                inc(t2);
                                if t2=maxn then t2:=0;
                                que2[t2].s:=temp;
                                que2[t2].sum:=que2[h2].sum+1;
                        end;
                end;
        until false;
end.

### 解题思路 题目要求解决的是一个与图相关的最小覆盖问题,通常在特定条件下可以通过状态压缩动态规划(State Compression Dynamic Programming, SCDP)来高效求解。由于状态压缩的适用条件是状态维度较小(例如K≤10),因此可以利用二进制表示状态集合,从而优化计算过程。 #### 1. 状态表示 - 使用一个整数 `mask` 表示当前选择的点集,其中第 `i` 位为 `1` 表示第 `i` 个节点被选中。 - 定义 `dp[mask]` 表示在选中 `mask` 所代表的点集后,能够覆盖的节点集合。 - 可以通过预处理每个点的邻域信息(包括自身所有直接连接的点),快速更新状态。 #### 2. 预处理邻域 对于每个节点 `u`,预先计算其邻域范围 `neighbor[u]`,即从该节点出发一步能到达的所有节点集合。这样,在后续的状态转移过程中,可以直接使用这些信息进行合并操作。 #### 3. 状态转移 - 初始化:对每个单独节点 `u`,设置初始状态 `dp[1 << u] = neighbor[u]`。 - 转移规则:对于任意两个状态 `mask1` `mask2`,如果它们没有交集,则可以通过合并这两个状态得到新的状态 `mask = mask1 | mask2`,并更新对应的覆盖范围为 `dp[mask1] ∪ dp[mask2]`。 - 在所有状态生成之后,检查是否某个状态的覆盖范围等于全集(即覆盖了所有节点)。如果是,则记录此时使用的最少节点数量。 #### 4. 最优解提取 遍历所有可能的状态,找出能够覆盖整个图的最小节点数目。 --- ### 时间复杂度分析 - 状态总数为 $ O(2^K) $,其中 `K` 是关键点的数量。 - 每次状态转移需要枚举所有可能的子集组合,复杂度为 $ O(2^K \cdot K^2) $。 - 整体时间复杂度控制在可接受范围内,适用于 `K ≤ 10~20` 的情况。 --- ### 代码实现(状态压缩 DP) ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 25; int neighbor[MAXN]; // 每个节点的邻域 int dp[1 << 20]; // dp[mask] 表示选中的点集合为 mask 时所能覆盖的点集合 int min_nodes; // 最小覆盖点数 void solve(int n, vector<vector<int>>& graph) { // 预处理每个节点的邻域 for (int i = 0; i < n; ++i) { neighbor[i] = (1 << i); // 包括自己 for (int j : graph[i]) { neighbor[i] |= (1 << j); } } // 初始化 dp 数组 memset(dp, 0x3f, sizeof(dp)); for (int i = 0; i < n; ++i) { dp[1 << i] = neighbor[i]; } // 状态转移 for (int mask = 1; mask < (1 << n); ++mask) { if (__builtin_popcount(mask) >= min_nodes) continue; // 剪枝 for (int sub = mask & (mask - 1); sub; sub = (sub - 1) & mask) { int comp = mask ^ sub; if (comp == 0) continue; int new_mask = mask; int covered = dp[sub] | dp[comp]; if (covered == (1 << n) - 1) { min_nodes = min(min_nodes, __builtin_popcount(new_mask)); } dp[new_mask] = min(dp[new_mask], covered); } } } int main() { int n, m; cin >> n >> m; vector<vector<int>> graph(n); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); // 无向图 } min_nodes = n; solve(n, graph); cout << "Minimum nodes required: " << min_nodes << endl; return 0; } ``` --- ### 优化策略 - **剪枝**:当当前状态所用节点数已经超过已知最优解时,跳过后续计算。 - **提前终止**:一旦发现某个状态覆盖了全部节点,并且节点数达到理论下限,即可提前结束程序。 - **空间优化**:可以仅保存当前轮次的状态,减少内存占用。 --- ### 总结 本题通过状态压缩动态规划的方法,将原本指数级复杂度的问题压缩到可接受范围内。结合位运算技巧预处理机制,能够高效地完成状态转移覆盖判断操作。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值