[Solution] [HNOI/AHOI2018] Road (Dynamic Programming)

Too lazy to copy, poke me poke me

Solution:

  • \(dp[i][j][k]\) With \(i\) as the root node of the subtree, to the root node there are \(j\) roads that have not been repaired, \(k\) railways that have not been repaired , saving subtrees is inconvenient and
  • \(dp[i][j][k]=min(dp[ls][j-1][k]+dp[rs][j][k] , dp[ls][j][k]+ dp[rs][j+1][k])\) , this formula is actually not difficult but I don't think it's easy qwq
  • just this

Code:

//It is coded by Ning_Mew on 4.17
#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int maxn=2e4+7;

int n;
struct Node{
  int l,r;LL a,b,c;LL dp[40][40];
  Node(){l=r=a=b=c=0;memset(dp,0LL,sizeof(dp));}
}node[maxn*2];

void dfs(int u){
  if(u>n)return;
  int ls=node[u].l,rs=node[u].r;
  dfs(ls);dfs(rs);
  for(int i=0;i<=39;i++){
    for(int j=0;j<=39;j++){
      node[u].dp[i][j]=min(node[ls].dp[i+1][j]+node[rs].dp[i][j] , node[rs].dp[i][j+1]+node[ls].dp[i][j]);
    }
  }return;
}
int main(){
  scanf("%d",&n);
  for(int i=1;i<=n-1;i++){
    int x,y;scanf("%d%d",&x,&y);
    if(x<0)x=-x+n;if(y<0)y=-y+n;
    node[i].l=x;node[i].r=y;
  }
  for(int i=n+1;i<=n+n;i++){
    LL a,b,c;scanf("%lld%lld%lld",&a,&b,&c);
    node[i].a=a;node[i].b=b;node[i].c=c;
    for(int j=0;j<=39;j++){
      for(int k=0;k<=39;k++){
    node[i].dp[j][k]=c*(a+j)*(b+k);
      }
    }
  }
  dfs(1);
  printf("%lld\n",node[1].dp[0][0]);
  return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325059255&siteId=291194637