zoukankan      html  css  js  c++  java
  • ZOJ 3631 Watashi's BG DFS

    点击打开链接

    思路:

    本来是一个背包问题,背包容量太大,就T了
    于是就只有DFS然后搜索,注意剪枝

    代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 #define mem(a) memset(a,0,sizeof(a))
     5 #define mp(x,y) make_pair(x,y)
     6 const int INF = 0x3f3f3f3f;
     7 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
     8 inline ll read(){
     9     ll x=0,f=1;char ch=getchar();
    10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    11     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    12     return x*f;
    13 }
    14 //////////////////////////////////////////////////////////////////////////
    15 const int maxn = 1e5+10;
    16 
    17 int n,M,cost[35];
    18 int ans;
    19 
    20 bool cmp(int x,int y){
    21     return x > y;
    22 }
    23 
    24 void dfs(int tot,int s){
    25     if(ans == M) return ;
    26     if(s > M) return ;
    27     if(tot>n){
    28         ans = max(s,ans);
    29         return ;
    30     }
    31 
    32     int sum = s;
    33     for(int i=tot; i<=n; i++)
    34         sum += cost[i];
    35     if(sum < ans) return ;
    36 
    37     dfs(tot+1,s+cost[tot]);
    38     dfs(tot+1,s);
    39 }
    40 
    41 int main(){
    42     while(scanf("%d%d",&n,&M)==2){
    43         ans = 0;
    44         for(int i=1; i<=n; i++)
    45             cost[i] = read();
    46         sort(cost+1,cost+1+n,cmp);
    47 
    48         dfs(0,0);
    49         cout << ans << endl;
    50     }
    51 
    52     return 0;
    53 }


    代码二:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 #define mem(a) memset(a,0,sizeof(a))
     5 #define mp(x,y) make_pair(x,y)
     6 const int INF = 0x3f3f3f3f;
     7 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
     8 inline ll read(){
     9     ll x=0,f=1;char ch=getchar();
    10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    11     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    12     return x*f;
    13 }
    14 //////////////////////////////////////////////////////////////////////////
    15 const int maxn = 1e5+10;
    16 
    17 int n,M,cost[35],vis[35];
    18 int ans;
    19 
    20 bool cmp(int x,int y){
    21     return x > y;
    22 }
    23 
    24 void dfs(int tot,int v){
    25     if(ans == M) return ;
    26     if(v > M) return ;
    27     if(v == M){
    28         ans = v;
    29         return ;
    30     }
    31     int sum=v;
    32     for(int j=tot+1;j<=n;j++){
    33         if(!vis[j])
    34             sum+=cost[j];
    35     }
    36     if(sum <= ans) return;
    37     ans=max(ans,v);
    38 
    39     for(int j=tot+1;j<=n;j++)
    40     {
    41         if(vis[j])
    42             continue;
    43         vis[j]=1;
    44         dfs(j,v+cost[j]);
    45         vis[j]=0;
    46     }
    47 }
    48 
    49 int main(){
    50     while(scanf("%d%d",&n,&M)==2){
    51         mem(vis);
    52         ans = 0;
    53         for(int i=1; i<=n; i++)
    54             cost[i] = read();
    55         sort(cost+1,cost+1+n,cmp);
    56 
    57         for(int i=1;i<=n;i++){
    58             vis[i]=1;
    59             dfs(i,cost[i]);
    60             vis[i]=0;
    61         }
    62         cout << ans << endl;
    63     }
    64 
    65     return 0;
    66 }
  • 相关阅读:
    GuozhongCrawler系列教程 (1) 三大PageDownloader
    数据库中表的复杂查询&amp;分页
    AngularJs 在控制器中过滤
    【iOS开发-79】利用Modal方式实现控制器之间的跳转
    leetCode(51):Valid Palindrome
    Eclipse上开发IBM Bluemix应用程序
    POJ 2104 K-th Number 静态主席树(裸
    BZOJ 3210 花神的浇花集会 计算几何- -?
    [易飞]一张领料单单身仓库&quot;飞了&quot;引起的思考
    Hbuilder开发app实战-识岁06-face++的js实现【完结】
  • 原文地址:https://www.cnblogs.com/yxg123123/p/6827700.html
Copyright © 2011-2022 走看看