simplified problem statement :
you have a DAG (direct acyclic graph) with nodes and 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 . You could only move two nodes together if they have the same destination.
Partial credit:
: dfs to find every possible set up.
: use bitmask dp to record the nodes. represent the nodes with bit , first point at , second point at ,transition:
: since the graph is a DAG. If the node with smaller id is i, you only have to record the nodes between . Use where 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.
: 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 where is the bit between nodes, is the smaller node, and means the bigger node’s id is
: is both too slow and use too much memory. However, it is easy to see that in the bit , the last bit with is always the highest node’s position. Therefore, we could use prefix sum to calculate the highest bit node, and only record , where represent the bit between two nodes, and j represent the small node. It is easy to see that the node with higher id is .
transition:
。
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:
#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;
}