• BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索


    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1673

    题意:

      有n个砝码(n <= 1000),重量为w[i]。

      你要从中选择一些砝码,使得这些砝码的总重量最大,但不超过c。

      w[i]按递增顺序给出,并且保证w[i] >= w[i-1]+w[i-2] (i >= 3)。

    题解:

      dfs。

      因为w[i] >= w[i-1]+w[i-2],所以其实n最大只有45左右(类似斐波那契数列)。

      优化:

        (1)启发式:优先选砝码(不跳过),并且优先选重量大的砝码,尽早更新ans。

        (2)A*搜索:如果当前tot + 剩下砝码的总重(前缀和) < ans,则return。

    AC Code:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #define MAX_N 1005
     5 
     6 using namespace std;
     7 
     8 int n,c;
     9 int ans=0;
    10 int w[MAX_N];
    11 long long sum[MAX_N];
    12 
    13 void dfs(int col,int tot)
    14 {
    15     if(tot>c) return;
    16     ans=max(ans,tot);
    17     if(col>n) return;
    18     if(tot+sum[n]-sum[col-1]<=ans) return;
    19     dfs(col+1,tot+w[col]);
    20     dfs(col+1,tot);
    21 }
    22 
    23 void read()
    24 {
    25     cin>>n>>c;
    26     for(int i=n;i>=1;i--)
    27     {
    28         cin>>w[i];
    29     }
    30 }
    31 
    32 void solve()
    33 {
    34     sum[0]=0;
    35     for(int i=1;i<=n;i++)
    36     {
    37         sum[i]=sum[i-1]+w[i];
    38     }
    39     dfs(1,0);
    40 }
    41 
    42 void print()
    43 {
    44     cout<<ans<<endl;
    45 }
    46 
    47 int main()
    48 {
    49     read();
    50     solve();
    51     print();
    52 }
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    易购商城首页
    使用HTML5验证博客园用户注册页面
    正则表达式相关内容和用法
    表单
    jQuery制作图片提示效果
    jQuery遍历
    用js制作论坛发贴
    使用jQuery操作DOM
    京东常见问题分类页面
  • 原文地址:https://www.cnblogs.com/Leohh/p/7634313.html
Copyright © 2020-2023  润新知