Like to play Warcraft LTL

Source: Internet
Author: User

Enjoy playing Warcraft's LTL time limit: -Ms | Memory Limit:65535KB Difficulty:3
Describe

LTL is very fond of playing Warcraft, because Warcraft is very fastidious about the overall strength of the team, and he is now also for upgrading without dragging down the team and efforts.

He now has a lot of places to choose to go to the Spawner upgrade, but in every location he had to buy adequate supplies and the right props, in order to avoid monsters when the monster anti-killing, each location of the monster has not been finished (but also do not lose money and equipment), and he just selected the location will be finished brush the site all the monsters , and the corresponding experience values are obtained. Now LTL can give every location the money to buy supplies and props and the experience of hitting all the monsters, but he has a certain amount of money. So he wanted to know how to choose the place that made him the most experienced.

Input
The
first line is an integer T that represents the number of groups of test data 0<t<=10
The second row of two integers n,m,0<n<=100,0<m<=1000000 indicates that the LTL has N different locations of choice and M's total money
The next n rows of two integers per line ci,vi, (0<ci<=1000000,0<vi<=2000) represents the total amount of money and total experience that LTL will need to purchase supplies and props to finish the first I site
.
Output
an integer line representing the maximum XP value that the LTL can obtain
Sample input
23 107 72 33 52 53 52 1
Sample output
Max Experience:12max Experience:6
After reading the question, immediately determine the 01 knapsack problem, and then directly write code, unfortunately, the tle is not time to!
 timeout code: 
 #include <stdio.h>struct node{int c;int W;} Num[105];int dp[1000005];int Max (int a,int b) {return a>b?a:b;} int main () {int t,n,m;int i,j;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m), and for (i=0;i<n;i++) {scanf ( "%d%d", &NUM[I].C,&NUM[I].W);} for (i=0;i<n;i++) {for (j=m;j>=num[i].c;j--) {Dp[j]=max (DP[J],DP[J-NUM[I].C]+NUM[I].W);}} printf ("Max Experience:%d\n", Dp[m]);} return 0;} 

The following is optimized code:
 #include <stdio.h>struct node{int c;int W;} Num[105];int dp[1000005];int Max (int a,int b) {return a>b?a:b;} int main () {int t,n,m;int i,j,s,count;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m); S=0;for (i=0;i<n ; i++) {scanf ("%d%d", &NUM[I].C,&NUM[I].W); s+=num[i].c;} for (i=0;i<=m;i++) dp[i]=0;for (i=0;i<n;i++) {S-=num[i].c;count=max (M-S,NUM[I].C), for (j=m;j>=count;j--) { Dp[j]=max (DP[J],DP[J-NUM[I].C]+NUM[I].W);}} printf ("Max Experience:%d\n", Dp[m]);} return 0;} 

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

Like to play Warcraft LTL

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.