Gold miner group backpack

Source: Internet
Author: User

Question:

N items must be retrieved within t time. Each item has its own time value.

The same line must be retrieved in sequence.

Algorithm:

At first, we used tree-like DP, which is dependent on the backpack .. TLE.

In addition, according to the requirements of the question, the subnode must be obtained before the subnode can be obtained.

Except for TLE, I am not sure about the conditions,

Make sure that you select the correct algorithm to analyze the feasibility of the algorithm (correctness, time, memory). Do not waste time.

Timeout tree-like DP:

View code

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <iostream> # include <vector> # include <string> # include <math. h ># include <map ># include <set> # include <algorithm> using namespace STD; int N, T; struct point {int X, Y; int T, V; bool operator <(const point & A) const {return ABS (x) <ABS (. x) ;}} P [300]; int MP [210] [210]; int DP [210] [210]; int visit [210]; void DFS (int v) {visit [v] = 1; for (INT I = 1; I <= N; I ++) {If (! Visit [I] & MP [v] [I]) {If (P [I]. T <= T) DFS (I);} else continue; For (Int J = T; j> = 1; j --) {for (int K = 1; k <j; k ++) {// If (DP [I] [k]! = 0 & DP [v] [J-K]! = 0) // {If (J-k> = P [v]. t) {DP [v] [J-K] = P [v]. v; DP [v] [J] = max (DP [v] [J], DP [I] [k] + dp [v] [J-K]);} else break; // }}}// printf ("V = % d t = % d v = % d \ n", V, p [v]. t, P [v]. v); DP [v] [p [v]. t] = P [v]. V ;}int main () {int abc = 1; while (scanf ("% d", & N, & T )! = EOF) {for (INT I = 1; I <= N; I ++) {scanf ("% d", & P [I]. x, & P [I]. y, & P [I]. t, & P [I]. v);} Sort (p + 1, P + n + 1); memset (visit, 0, sizeof (visit); memset (MP, 0, sizeof (MP); // you can determine whether the data is in the same line for (INT I = 1; I <= N; I ++) {int x = P [I]. x, Y = P [I]. y; If (! Visit [I]) MP [0] [I] = 1; for (Int J = I + 1; j <= N; j ++) {int X1 = P [J]. x; int Y1 = P [J]. y; If (x * Y1 = x1 * Y) {MP [I] [J] = 1; visit [J] = 1 ;}} memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) {DP [I] [0] = 0; if (MP [0] [I]) DP [I] [p [I]. t] = P [I]. v;} memset (visit, 0, sizeof (visit); DFS (0); printf ("case % d: % d \ n", ABC ++, DP [0] [T]);} return 0 ;}

Correct Solution:

Group A backpack. items on each line are considered as a group. Each group can take one or two items in order ,...

Programming did not contribute wa several times carefully.

View code

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <iostream> # include <vector> # include <string> # include <math. h ># include <map ># include <set> # include <algorithm> using namespace STD; int N, T; struct point {int X, Y; int T, V; bool operator <(const point & A) const {return ABS (x) <ABS (. x) ;}} P [300]; int visit [210]; struct node {int V [210]; // points} line [210]; int num [210]; struct pnode {int weight [210]; Int price [210];} PX [210]; int f [41000]; int CNT; int solve () {for (INT I = 0; I <= T; I ++) // indicates that the backpack does not need to be filled with f [I] = 0; For (int K = 0; k <CNT; k ++) {for (INT v = T; V> = 0; V --) // each group is treated as a 01 backpack, so the calculation order is V decreasing {for (INT I = 0; I <num [k]; I ++) // For each I {If (V-Px [k] in each group. weight [I]> = 0) f [v] = max (F [v], F [V-Px [K]. weight [I] + px [K]. price [I]) ;}}return f [T];} int main () {int abc = 1; while (SC ANF ("% d", & N, & T )! = EOF) {for (INT I = 1; I <= N; I ++) {scanf ("% d", & P [I]. x, & P [I]. y, & P [I]. t, & P [I]. v);} CNT = 0; sort (p + 1, P + n + 1); memset (visit, 0, sizeof (visit); memset (Num, 0, sizeof (Num); memset (PX, 0, sizeof (PX); // you can determine whether a common for (INT I = 1; I <= N; I ++) {int x = P [I]. x, Y = P [I]. y; int FF = 0; If (! Visit [I]) {line [CNT]. V [num [CNT] ++] = I; FF = 1;} else continue; For (Int J = I + 1; j <= N; j ++) {int X1 = P [J]. x; int Y1 = P [J]. y; If (x * Y1 = x1 * Y) {visit [J] = 1; line [CNT]. V [num [CNT] ++] = J ;}} if (FF) CNT ++ ;}for (INT I = 0; I <CNT; I ++) {int A = 0, B = 0; For (Int J = 0; j <num [I]; j ++) {int temp = line [I]. V [J]; A + = P [temp]. t; B + = P [temp]. v; PX [I]. weight [J] = A; PX [I]. price [J] = B; // printf ("A = % d B = % d \ n", a, B) ;}} printf ("case % d: % d \ n ", ABC ++, solve ();} return 0 ;}

 

 

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.