跳到正文

个人记录

sol for dream

pocafup约 9 分钟
原文
文章信息与版本

文章信息

发布
2020年10月4日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 9 分钟
获赞
1

版本

  1. v1当前

simplified problem statement :

you have a DAG (direct acyclic graph) with nn nodes and mm edge. You are standing on node 1 at the begining and you want to move the nodes to the node n and maximaze the union path edge weight.

In one move, you could move any node to a position as long as their id difference is less than or equal to ll. You could only move two nodes together if they have the same destination.

Partial credit:

n≤10n\le 10 : dfs to find every possible set up.

O(n!)O(n!)


n≤16n\le 16 : use bitmask dp to record the nodes. dp[i][j][k]dp[i][j][k] represent the nodes with bit ii, first point at jj, second point at kk,transition:

dp[i][j][k]={dp[i+(1<<vj)][vj][k]dp[i+(1<<vk)][j][vk]dp[i+(1<<vj,k)][vj,k][vj,k] dp[i][j][k] = \left\{ \begin{aligned} &dp[i+(1<<v_{j})][v_{j}][k]& \\ &dp[i+(1<<v_{k})][j][v_{k}]& \\ &dp[i+(1<<v_{j,k})][v_{j,k}][v_{j,k}]& \end{aligned} \right.

O(2n×n2)O(2^n \times n^2)


n≤100,l≤8n\le 100, l \le 8 : since the graph is a DAG. If the node with smaller id is i, you only have to record the nodes between i→i+li \to i+l. Use dp[i][j][k][l]dp[i][j][k][l] where ii is the bit between k and l, j is the notes on the left, k is the first node and l is the second node.

O(n×2l×l2)O(n\times2^l \times l^2)


n≤103,l≤8 n \le 10^3, l\le 8 : dp[i][j][k][l]dp[i][j][k][l] would take too much space. However, it is easy to see that we don’t need j. What we can do is we keep k as the node with the smaller id. When the smaller id node move to a position greater than the node with bigger id, we swop the order of the nodes.

So, in this case, record dp[i][j][k]dp[i][j][k] where ii is the bit between nodes, jj is the smaller node, and kk means the bigger node’s id is j+kj+k

O(n×2l×l)O(n \times 2^l \times l)


n≤5×103,l≤12 n \le 5\times 10^3, l \le 12 : dp[i][j][k]dp[i][j][k] is both too slow and use too much memory. However, it is easy to see that in the bit ii, the last bit with 11 is always the highest node’s position. Therefore, we could use prefix sum to calculate the highest bit node, and only record dp[i][j]dp[i][j], where ii represent the bit between two nodes, and j represent the small node. It is easy to see that the node with higher id is j+highbit[i]j + highbit[i].

transition:

curr=j+highbit[i] curr = j + highbit[i]

dp[i][j]={dp[i][j+(1<<v)] {v−i≤l}dp[v][(j>>(i−v))∣1] {v<curr}dp[curr][(j>>(curr−i))+(1<<(v−curr))] {0≤v−curr≤l}dp[v][1]{i→v && curr→v} dp[i][j] = \left\{ \begin{aligned} &dp[i][j+(1<<v)] \space\{v-i \le l\}&\\ &dp[v][(j>>(i-v))|1] \space \{v<curr\} &\\ &dp[curr][(j>>(curr-i)) + (1<<(v-curr))] \space \{ 0\le v-curr \le l\}&\\ &dp[v][1] \{i \to v\space \&\&\space curr \to v\}& \end{aligned} \right.

O(2l+n×2l)O(2^l + n\times2^l)。

we could use rolling array to reduce space complexity. In this problem it is not necessary, but it can reduce 400 times the memory and no change in the time complexity.

code:

C++
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <vector>
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;
const int MAXN = 5e3+5;
const int MAXL = 13;
unordered_map<int,unordered_map<int,bool> > mp;
int n,m,l,pos[MAXN],dp[MAXL][(1<<MAXL)+5],top[(1<<MAXL)+5],big[MAXN],ans,now;
vector<int> adj[MAXN],num;
inline int max(int a, int b) {return a>b?a:b;}
int main(){
  cin >> n >> m >> l;
  for (int i=1;i<=n;i++) cin >> pos[i];
  for (int i=1;i<=m;i++){
    int a,b; cin >> a >> b; if (!mp[a][b]) mp[a][b] = true,adj[a].push_back(b);
  }
  memset(top,0xff,sizeof(top));
  for (int i=1;i<(1<<(l+1));i++){
    for (int k=l+1;k>=0;k--){
      if ((i>>k) & 1) {top[i] = k; break;}
    }
  }//prefix
  memset (dp,0xff,sizeof(dp));
  for (int i=1;i<=n;i++){
    now = (i-1)%MAXL;//current now id
    dp[now][1] = max(dp[now][1],big[i]);//first ensure the array is maximize.
    for (int j=1;j<(1<<(l+1));j++){
      int pos1 = i;
      int pos2 = i+top[j];
      if (dp[now][j]==-1) continue;
      for (int v : adj[pos2]){
        if (mp[pos1][v]) big[v] = max(big[v],dp[now][j]+pos[v]);
        if (v-i>l)continue;
        int nxt = j+(1<<(v-pos1));
        dp[now][nxt] = max(dp[now][nxt],dp[now][j]+pos[v]);
      }
      for (int v : adj[pos1]){
        if (v-pos2>l) continue;
        if (v>pos2){
          int nxt = (j>>(pos2-pos1)) + (1<<(v-pos2));
          int to = (pos2-1)%MAXL;
          dp[to][nxt] = max(dp[to][nxt],dp[now][j]+(((j>>(v-pos1)) & 1) ? 0 : pos[v]));
        }else{
          int nxt = (j>>(v-pos1))|1;
          int to = (v-1)%MAXL;
          dp[to][nxt] = max(dp[to][nxt],dp[now][j]+(((j>>(v-pos1)) & 1) ? 0 : pos[v]));
        }
      }
      if (i!=n)dp[now][j] = -1; //rolling array, do not change node n though.
    }
  }
  cout << dp[(n-1)%MAXL][1] << endl;
}
原文
存档评论1 条抓取于