zoukankan      html  css  js  c++  java
  • 【BZOJ5290】[HNOI2018]道路(动态规划)

    【BZOJ5290】【HNOI2018】道路(动态规划)

    题面

    BZOJ
    洛谷

    题目直接到洛谷上看吧

    题解

    开始写写今年省选的题目
    考场上我写了一个模拟退火骗了(90)分。。。然而重测后只剩下45了QwQ

    然而这道题目是道傻逼题
    考虑(dp)
    (f[i][a][b])表示从(i)节点向上经过(a)条公路(b)条铁路的最小代价
    很明显的转移是:

    [f[i][a][b]=min(f[lson][a][b]+f[rson][a][b+1],f[lson][a+1][b]+f[rson][a][b]) ]

    然后从根节点开始做一遍树形(dp)就行了。。。

    真心傻逼题啊。。。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<vector>
    #include<queue>
    using namespace std;
    #define ll long long
    #define RG register
    #define MAX 40404
    inline int read()
    {
        RG int x=0,t=1;RG char ch=getchar();
        while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
        if(ch=='-')t=-1,ch=getchar();
        while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
        return x*t;
    }
    int n,A[MAX],B[MAX],C[MAX],son[2][MAX];
    ll f[MAX>>1][44][44];
    ll Calc(int u,int a,int b)
    {
    	if(u>=n)return 1ll*C[u]*(A[u]+a)*(B[u]+b);
    	return f[u][a][b];
    }
    void dfs(int u,int a,int b)
    {
    	if(u>=n)return;
    	int v1,v2;
    	dfs(v1=son[0][u],a+1,b);
    	dfs(v2=son[1][u],a,b+1);
    	for(int i=0;i<=a;++i)
    		for(int j=0;j<=b;++j)
    			f[u][i][j]=min(Calc(v1,i,j)+Calc(v2,i,j+1),Calc(v1,i+1,j)+Calc(v2,i,j));
    }
    int main()
    {
    	n=read();
    	for(int i=1;i<n;++i)
    	{
    		int s=read(),t=read();
    		if(s<0)s=-s+n-1;
    		if(t<0)t=-t+n-1;
    		son[0][i]=s;son[1][i]=t;
    	}
    	for(int i=1;i<=n;++i)A[i+n-1]=read(),B[i+n-1]=read(),C[i+n-1]=read();
    	dfs(1,0,0);
    	printf("%lld
    ",f[1][0][0]);
    	return 0;
    }
    
    
  • 相关阅读:
    一个简单实现的string类
    Python基础(二)
    Python基础(一)
    区块链初探
    某电商平台开发记要——客服系统
    某电商平台开发记要
    Upload files to aliyunOSS with bootstrap-fileinput
    jquery.validate[.unobtrusive]和Bootstrap实现tooltip错误提示
    PostgreSQL笔记
    天冷了,那些树还好吗?
  • 原文地址:https://www.cnblogs.com/cjyyb/p/8869783.html
Copyright © 2011-2022 走看看