SDUT3303 to send a backpack open appetizer (simulated backpack), DFS

Source: Internet
Author: User

Come on, get a backpack, open an appetizer Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ Title Description

There are n kinds of goods, each commodity has Si pieces, the price and quality of each commodity are Pi and Mirespectively. Now you have the money. V, ask what the total mass of the goods you can buy is the largest.

Enter multiple sets of inputs. For each set of inputs:

The first line is two integers n,V(1 <= n <= 3,1 <= V <= 1,000,000,000).

The next n rows, three integers per line, Si,Pi,Mi, respectively, represent the first i quantity, price and quality of the items (1 <= Si <= 100,1 <= Pi <= 1,000,000,000, 1 <= Mi <= 1,000,000,000) .

Output for each set of data, output an integer representing the answer. Sample input
1 1010 1 10
Sample output
100
Tip This question number is too big, can not use backpack dp[10000000 ...], even with the map, also because from 0 initialized to 1 billion and timeout;
But the quantity is small, can be violent, can DFS.
#include <iostream> #include <cstdio> #include <cstring> #include <cmath>using namespace std; int main () {int n;long long v,sum=-1,ans,s[4],m[5],p[5];while (~scanf ("%d%lld", &n,&v)) {Sum=-1;memset (s,0, sizeof (s)); memset (P,0,sizeof (P)); memset (M,0,sizeof (m)); for (int i=0;i<n;i++) scanf ("%lld%lld%lld", &s[i], &p[i],&m[i]); for (int i=0;i<=s[0];i++)       

#include <iostream> #include <cstring> #include <cstdio>using namespace std;long long int n,v;long long Sum=-1;long long int m[1000],p[1000],s[1000];int vis[5],tt=0;int DFS (int i,long long int v,long long int ans,int num) {
   
    if (v<0)             return 0;    if (i>n)        //type        return 0;    if (NUM>TT)     //number cannot exceed the total number of        return 0;    if (ans>sum)    {        Sum=ans;        return 0;    }    for (int j=0; j<=s[i]; j + +)    {        if (v-p[i]*j>=0)        {            DFS (i+1,v-p[i]*j,ans+m[i]*j,num+j);        }    }}int Main () {while    (~scanf ("%lld%lld", &n,&v))    {        tt=0;        for (int i=0; i<n; i++)        {            scanf ("%lld%lld%lld", &s[i],&p[i],&m[i]);            Tt+=s[i];        }        memset (vis,0,sizeof (Vis));        sum=0;        DFS (0,v,0,0);        printf ("%lld\n", sum);}    }
   


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SDUT3303 to send a backpack open appetizer (simulated backpack), DFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.