zoukankan      html  css  js  c++  java
  • luogu P4438 [HNOI/AHOI2018]道路

    传送门

    讲一下做题的过程 

    Day1 指定标签搜索 看题 从入门到入土

    恶心 再见吧

    Day2 这么咕了一道题好像不好?还是做一下吧

    10 minutes later……

    这不就是个二叉树吗!

    题意就是统计每个点经过的左右边的路径上选择多少条边进行标记

    然后化成一个没法巧算的式子来恶心你 求最小值

    给一个条件就是深度<=40

    所以搞一个记搜跑一下所有叶子结点的答案

    即dp[x][l][r]表示x的子树所有点到1的路径上的最小值和

    其中x到根的路径修理l条公路和r条铁路

    所以记搜搞一搞

    从x开始修公路(左儿子)就是dp[x][l][r] = dp[ls[x]][l][r] + dp[rs[x]][l][r+1]

    修铁路(右儿子)就是dp[x][l][r] = dp[ls[x]][l+1][r] + dp[rs[x]][l][r]

    然后取个min就行

    初值inf所以写了一个比较gou的异或

    Time cost: 15min + 55min

    Code:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<queue>
     6 #include<vector>
     7 #define ms(a,b) memset(a,b,sizeof a)
     8 #define rep(i,a,n) for(int i = a;i <= n;i++)
     9 #define per(i,n,a) for(int i = n;i >= a;i--)
    10 #define inf 4e9
    11 using namespace std;
    12 typedef long long ll;
    13 ll read() {
    14     ll as = 0,fu = 1;
    15     char c = getchar();
    16     while(c < '0' || c > '9') {
    17         if(c == '-') fu = -1;
    18         c = getchar();
    19     }
    20     while(c >= '0' && c <= '9') {
    21         as = as * 10 + c - '0';
    22         c = getchar();
    23     }
    24     return as * fu;
    25 }
    26 const int N = 40005;
    27 //head
    28 int n;
    29 ll a[N],b[N],c[N];
    30 
    31 int ls[N],rs[N];
    32 int numl[N],numr[N];
    33 ll dp[20005][50][50];
    34 ll dfs(int x,int l,int r) {
    35     if(x >= n) return (a[x] + l) * (b[x] + r) * c[x];
    36     if(dp[x][l][r] ^ dp[0][0][0]) return dp[x][l][r];
    37     dp[x][l][r] = dfs(ls[x],l+1,r) + dfs(rs[x],l,r);
    38     dp[x][l][r] = min(dp[x][l][r],dfs(ls[x],l,r) + dfs(rs[x],l,r+1));
    39     return dp[x][l][r];
    40 }
    41 
    42 int main() {
    43     n = read();
    44     //sons
    45     rep(i,1,n-1) {
    46     ls[i] = read(),rs[i] = read();
    47     if(ls[i] < 0) ls[i] = -ls[i] + n-1;
    48     if(rs[i] < 0) rs[i] = -rs[i] + n-1;
    49     }
    50     //val of village
    51     rep(i,1,n) {
    52     int x = i+n-1;
    53     a[x] = read(),b[x] = read(),c[x] = read();
    54     }
    55     ms(dp,63);
    56     printf("%lld
    ",dfs(1,0,0));
    57     return 0;
    58 }
    View Code

     

    > 别忘了 总有人在等着你
  • 相关阅读:
    eslint and stylelint config
    CSS3 animaion 和 transition 比较
    css之px、em、rem
    Three.js 中 相机的常用参数含义
    ES6中函数调用自身需要注意的问题
    MySQL数据库迁移之data目录
    ES6扩展运算符(三点运算符)...的用法
    Vue 组件通信方案
    关于ES6中Promise的应用-顺序合并Promise,并将返回结果以数组的形式输出
    Ubuntu 使用gps
  • 原文地址:https://www.cnblogs.com/yuyanjiaB/p/9903546.html
Copyright © 2011-2022 走看看