zoukankan      html  css  js  c++  java
  • 1290:采药

    http://ybt.ssoier.cn:8088/problem_show.php?pid=1290

    大爆搜DFS写法(30分)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int t, m, a[105][2], ans;
     4 void dfs(int s, int ti, int val){
     5     if(s>=m+1){
     6         ans=max(ans, val);
     7         return;
     8     }
     9     dfs(s+1, ti, val);
    10     if(ti+a[s][0] <= t)
    11         dfs(s+1, ti+a[s][0], val+a[s][1]);
    12 }
    13 int main()
    14 {
    15     scanf("%d%d", &t, &m);
    16     for(int i=1; i<=m; i++)
    17         scanf("%d%d", &a[i][0], &a[i][1]);
    18     dfs(1, 0, 0);
    19     printf("%d", ans);
    20     return 0;
    21 } 

    记忆化搜索(100)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int INF=1<<30;
     4 int n,t;
     5 int tcost[103],mget[103];
     6 int mem[103][1003];
     7 int dfs(int pos,int tleft){
     8     if( mem[pos][tleft] != -1 ) return mem[pos][tleft];
     9     if(pos == n+1)
    10         return mem[pos][tleft] = 0;
    11     int dfs1,dfs2 = -INF;
    12     dfs1 = dfs(pos+1,tleft);
    13     if( tleft >= tcost[pos] )
    14         dfs2 = dfs(pos+1,tleft-tcost[pos]) + mget[pos];
    15     return mem[pos][tleft] = max(dfs1,dfs2);
    16 }
    17 int main(){
    18     memset(mem,-1,sizeof(mem));
    19     cin >> t >> n;
    20     for(int i = 1;i <= n;i++)
    21         cin >> tcost[i] >> mget[i];
    22     cout << dfs(1,t) << endl;
    23     return 0;
    24 }

    01背包(100)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int t, m, a[105][2], dp[105][1005];
     4 int main()
     5 {
     6     scanf("%d%d",&t, &m);
     7     for(int i=1; i<=m; i++)
     8         scanf("%d%d", &a[i][0], &a[i][1]);
     9     for(int i=1; i<=m; i++)
    10         for(int j=0; j<=t; j++)
    11             if(j>=a[i][0])dp[i][j]=max(dp[i-1][j], dp[i-1][j-a[i][0]]+a[i][1]);
    12             else dp[i][j]=dp[i-1][j];    
    13     printf("%d", dp[m][t]);
    14     return 0;
    15  } 
  • 相关阅读:
    msp430时钟小结
    JDBC第一天连接池案例
    maltab几个常见的问题
    qt 获取天气的接口
    Qt Style Sheets制作UI特效
    qt 在指定区域添加图片
    qt 设置背景图片
    linux下mysql数据库的学习
    qt文本编辑器
    C++小游戏:扑克牌21点
  • 原文地址:https://www.cnblogs.com/tflsnoi/p/14773216.html
Copyright © 2011-2022 走看看