HLG 哈理工 1053 Warcraft III (完全背包)

Warcraft III
Time Limit: 10000 MS Memory Limit: 65536 K
Total Submit: 587(194 users) Total Accepted: 260(173 users) Rating: Special Judge: No
Description

dccmx likes playing Warcraft III. Now, he is teaching his girlfriend to play it. In Warcraft III, there are many kinds of units. Every unit costs some gold and lumber. Different units have different attack value.

Now question comes. Given some amount of gold and a list of types of units, how to arrange your units to maximize the attack value of your units. Assume you have infinite lumbers.

Input

Line 1 contains an integer T: the number of test cases.

Next T blocks, each starts with two integers: G and U, represents the amount of gold and number of unit type. Next U lines, each contains two integers: the attack value of a type of unit and the cost.

Output

For each test case, output the maximum total attack value in one line.

Sample Input

2

100 1

20 10

300 4

100 60

250 120

120 100

35 20

Sample Output

200

605

Author

dccmx

/*=============================================================================
#
#      Author: liangshu - cbam
#
#      QQ : 756029571
#
#      School : 哈尔滨理工大学
#
#      Last modified: 2015-08-29 00:24
#
#     Filename: C.cpp
#
#     Description:
#        The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#
#include<stdio.h>
#include<iostream>
using namespace std;

const int INF = 100000;
int dp[INF];
struct A
{
    int val,cost;
}E[INF];
int main(){
    int T;
    cin>>T;
    while(T--){
        int G, U;scanf("%d%d",&G, &U);
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= U; i++){
            scanf("%d%d",&E[i].val, &E[i].cost);
        }
        for(int i = 1; i <= U; i++){
            for(int j = E[i].cost; j <= G; j++){
                dp[j] = max(dp[j], dp[j - E[i].cost] + E[i].val);
            }
        }
     printf("%d\n",dp[G]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-26 03:15:32

HLG 哈理工 1053 Warcraft III (完全背包)的相关文章

Hrbust1053 Warcraft III (完全背包)

本文出自:http://blog.csdn.net/svitter 原题:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1053 题意:完全背包不解释..直接贴代码.. #include <iostream> #include <stdio.h> #include <string.h> using namespace std; #define max(a,

矩阵经典题目七:Warcraft III 守望者的烦恼(矩阵加速递推)

https://www.vijos.org/p/1067 很容易推出递推式f[n] = f[n-1]+f[n-2]+......+f[n-k]. 构造矩阵的方法:构造一个k*k的矩阵,其中右上角的(k-1)*(k-1)的矩阵是单位矩阵,第k行的每个数分别对应f[n-1],f[n-2],,f[n-k]的系数.然后构造一个k*1的矩阵,它的第i行代表f[i],是经过直接递推得到的.设ans[][]是第一个矩阵的n-k次幂乘上第二个矩阵,f[n]就是ans[k][1]. 注意:用__int64 #in

矩阵十题【七】vijos 1067 Warcraft III 守望者的烦恼

题目链接:https://vijos.org/p/1067 题目大意:给你一个k以及n,k代表最多走的步数,n代表一共要走的步数. 问一共有多少种方法,结果mod7777777 题目意思是很明了,具体的公式也能推出来 状态转移方程为:f[n]=f[n-1]+f[n-2]+....f[n-k]. f[0]=1 当k=1,   f[1]=1; f[2]=f[1]=1; f[3]=f[2]=1; f[4]=f[3]=1; 当k=2,   f[1]=1; f[2]=f[1]+f[0]=2; f[3]=f

VOJ 1067 Warcraft III 守望者的烦恼 (矩阵快速幂+dp)

题目链接 显然可知 dp[n] = dp[n-k] + dp[n-k+1] + ... +dp[n-1]; 然后要用矩阵来优化后面的状态转移. 也就是矩阵 0 1 0 0    a     b 0 0 1 0 * b =  c 0 0 0 1    c     d 1 1 1 1    d    a+b+c+d 然后跑快速幂 #include <iostream> #include <cstdio> #include <algorithm> #include <c

[Vijos1067]Warcraft III 守望者的烦恼(DP + 矩阵优化)

传送门 可知 f[i] = f[i - 1] + f[i - 2] + ... + f[i - k] 直接矩阵优化就好了 #include <cstdio> #include <cstring> #define p 7777777 #define LL long long int n, k; struct Matrix { int n, m; LL a[11][11]; Matrix() { n = m = 0; memset(a, 0, sizeof(a)); } }; inli

[矩阵乘法][DP]Vijos1067 Warcraft III 守望者的烦恼

题目梗概 n个单位的路程,主角每次最多可以走k个单位(也就是每次可以走1-k个单位),问最后到第n个监狱的方法数. 思考 DP转移方程并不难推导: dp[i]表示第i个监狱的方法数 $dp\left [ i \right ] = dp\left [ i-1 \right ] + dp\left [ i-2 \right ]\cdots \cdots + dp\left [ i-k-1 \right ]$ 但是这个n有点太大了,所以我们需要对DP方程进行优化. 仔细观察转移方程会发现,每次都是加上

VOJ 1067 Warcraft III 守望者的烦恼 (矩阵高速功率+dp)

主题链接 明显的 dp[n] = dp[n-k] + dp[n-k+1] + ... +dp[n-1]; 然后要用矩阵来优化后面的状态转移. 也就是矩阵 0 1 0 0    a     b 0 0 1 0 * b =  c 0 0 0 1    c     d 1 1 1 1    d    a+b+c+d 然后跑高速幂 #include <iostream> #include <cstdio> #include <algorithm> #include <cm

Warcraft III 守望者的烦恼

题目链接: 这道题跟斐波那契数列类似,快速幂矩阵随便推一推就出来了! #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define ll long long #define re register #define pb push_back #define fi first #define se second const int N=1e6+10; const int mod=

DP题目

//HDU 4001 To Miss Our Children Time HDU 4433 locker HDU 4362 Dragon Ball[] HDU 3602 2012[] HDU 4385 Moving Bricks[] HLG 1067 QQ Farm[] HDU 4293 Groups HDU 3920 Clear All of Them I[] HDU 3466 Proud Merchants[] POJ 2923 Relocation[] HDU 3660 Alice and