If multiple vertices are connected to the origin, you need to group the first vertex as a separate vertex and the first two as a separate vertex... finally, you can group the backpack directly.
The Code is as follows:
# Include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # define maxn 205 using namespace STD; int n, m, CNT [maxn], vis [maxn], idx; int f [40005]; struct node {int X, Y, T, V; bool operator <(node temp) const {return Y <temp. Y ;}// sort the Y axis to obtain the nearest vertex} e [maxn]; struct team {int T, V ;} P [maxn] [maxn]; bool inline (int A, int B) {return E [A]. x * E [B]. y = E [A]. y * E [B]. x;} void Init () {int St = 0, SV = 0; idx = 0; memset (VIS, 0, sizeof (VIS); memset (CNT, 0, sizeof (CNT )); for (INT I = 1; I <= N; ++ I) {If (! Vis [I]) {vis [I] = 1; ++ idx, ++ CNT [idx]; ST = E [I]. t, SV = E [I]. v; P [idx] [CNT [idx]. T = sT; P [idx] [CNT [idx]. V = SV; For (Int J = 1; j <= N; ++ J) {If (! Vis [J] & inline (I, j) {vis [J] = 1; ++ CNT [idx]; ST + = E [J]. t, SV + = E [J]. v; P [idx] [CNT [idx]. T = sT; P [idx] [CNT [idx]. V = SV ;}}}void solve () {memset (F, 0, sizeof (f); For (INT I = 1; I <= idx; ++ I) {// first take into account a group and obtain the optimal answer for (INT T = m; t> = 0; -- t) for this group) {// because only one item can be retrieved in a group, this loop is in the outer for (Int J = 1; j <= CNT [I]; ++ J) {If (T> = P [I] [J]. t) {f [T] = max (F [T], F [T-P [I] [J]. t] + P [I] [J]. v) ;}}} printf ("% d \ n", F [m]);} int main () {int CA = 0; while (scanf ("% d", & N, & M) = 2) {for (INT I = 1; I <= N; ++ I) {scanf ("% d", & E [I]. x, & E [I]. y, & E [I]. t, & E [I]. v);} Sort (E + 1, E + 1 + n); printf ("case % d:", ++ CA); Init (); solve ();} return 0 ;}