Noip Simulation Problem--b

Source: Internet
Author: User

"Title description"
we want to choose M from N food, arrange a sequence to eat it (we), each food has a delicious value ai, and then we have a k rule, each rule has XI, Yi and ci three number, if after eating the first XI food, then immediately eat the first kind of food, the delicious value of the first J food will increase ci. Eat at most one of each food, what is the most delicious value?
"input Format"
The first line has three numbers n,m,k,k represents a K rule (0<=k<=n* (n-1)).
The second line has n numbers that represent the delicious value of each food.
Then there are k lines, each line three number xi,yi,ci. The same is guaranteed for Xi and Yi without any two rules.
"Output format"
one row A number represents the answer
"Sample INPUT1"
2 2 1
1 1
2 1 1
"Sample OUTPUT1"
3
"Sample input 2"
4 3 2
1 2 3 4
2 1 5
3 4 2
"Sample Output 2"
A
"Data range"
30% m<=n<=5, 0<=ci,ai<=1e5
100% m<=n<=18,0<=ci,ai<=1e9

Because the data is small, look for more, so consider the pressure DP. F[I][J] Indicates that I state the last number is the maximum value of j, if not eaten every time something else not eaten and eaten (as the current state of the last eaten), if eat more than the continue, otherwise (find out m) find Dp[i][j] the maximum value.

1#include <cstdio>2#include <iostream>3#include <cstring>4#include <cmath>5 using namespacestd;6 Const Long Longmaxn= -;7 Long Longdp[(1<<MAXN)] [MAXN];8 Long LongV[MAXN];9 Long LongPP[MAXN][MAXN];Ten Long Longn,m,k; One Long LongMaxLong LongXLong Longy) A { -     if(x<y)returny; -     returnx; the } - Long Long_count (Long Longx) - { -     Long Longan=0; +      while(x) -     {             +         if(x&1) an++; Ax>>=1; at     } -     returnan ; - } - intMain () - { -Freopen ("b.in","R", stdin); inFreopen ("B.out","W", stdout); -scanf"%i64d%i64d%i64d",&n,&m,&k); to      for(intI=0; i<=n-1; i++) +scanf"%i64d",&v[i]); -      for(intI=1; i<=k;i++) the     { *         intx, y;Long LongZ; $scanf"%d%d%i64d",&x,&y,&z);Panax Notoginsengpp[x-1][y-1]=Z; -     } the     Long Longans=0; +      for(intI=0; i<=n-1; i++) Adp[(1<<i)][i]=V[i]; the      for(intI=0;i< (1&LT;&LT;N); i++) +     { -         intqw=_count (i); $         if(qw>m)Continue; $         if(qw==m) -         { -              for(intj=0; j<=n-1; j + +) theans=Max (ans,dp[i][j]); -             Continue; Wuyi         } the         Else -         { Wu              for(intj=0; j<=n-1; j + +)//enumerate nothing to eat -             { About                 if(((1&LT;&LT;J) &i) = =0)//didn't eat $                 { -                      for(intk=0; k<=n-1; k++)//enumerate the last points eaten -                     if(((1<<k) &i))//'ve eaten -Dp[i| (1&LT;&LT;J)][j]=max (dp[i][k]+pp[k][j]+v[j],dp[i| (1<<j)] [j]); A                 }  +             } the              -         } $     } theprintf"%i64d", ans); the     return 0; the}

Noip Simulation Problem--b

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.