Deep Search 7.1 - subset sum problem (+ subset enumeration pruning)

--- --- restore content begins

7.1, 5 today to AC topic today, as we explain the popularity of a deep search pruning problem set, you need to simulate in the hands of the mind and corresponding reference solution of this question should not have unrealistic expectations when, should not half-hearted

Title Description

For a given set of positive integer s = {x1, x2, x3 ... xn} positive integers and c, programming a first subset of s calculated s1, s1 such that the subset is equal to c.

 

Entry

The first line has two positive integers n and c
second row has n positive integers
n <7000, c <maxlongint


Export

A row of data sequentially input output, if no solution then the output "No Solution!"
 

Sample input

5 10
2 2 6 5 4

Sample Output

226 

Solution:
  This question is basic idea is to use problem-solving dfs enumerated array, and an array of computing.
  However, if the data is time consuming large, we will need to optimize the pruning, the corresponding pruning:
    1. Calculate the suffix array and the sum [I] and the suffix array minimum Mina [I]
    2. For the sum [ i] array, the enumeration process if the current array and suffix cnt and i SUM [i] is less than C, directly break out of the loop, the current state ends dfs
    3. for Mina [i] array, the enumeration process if the current array and cnt plus mina [i]> c, is directly out of the loop, the current state ends dfs, jump next state

explanations Code:

#include <the iostream>
#include <String>
the using namespace STD;
int n-;
Long Long C, A [7005], SUM [7005], Mina [7005], ANS [7005], Minn = 0x3f3f3f3f, SUM1 = 0;
BOOL In Flag = 0;
void DFS (int index1,, int index2, int CNT) {
 IF (CNT> C) return;
 IF (CNT == C) {
  for (int I = 0; I <index2; I ++) {
   the printf ( "% LLD", ANS [I]);
  } the printf ( "\ n-" );
  Exit (0);
 }
 for(int i=index1+1;i<n;i++){
  if(cnt+sum[i]<c) break;
  if(cnt+mina[i]>c) break;
  ans[index2]=a[i];
  dfs(i,index2+1,cnt+ans[index2]);
 }
}
int main(){
 freopen("setsum.in","r",stdin);
 scanf("%d%lld",&n,&c);
 for(int i=0;i<n;i++){
  scanf("%lld",&a[i]);
 }
 for(int i=n-1;i>=0;i--){
  minn=min(minn,a[i]);
  sum1+=a[i];
  sum[i]=sum1;
  mina[i]=minn;
 }
 /*
 for(int i=0;i<n;i++){
  printf("%lld %lld\n",sum[i],mina[i]);
 }
 */
 dfs(-1,0,0);
 printf("No Solution!\n");
 
 return 0;Introspection and Vista:
}



  7 on the 1st remember this day, as Mr. Sun said: the pursuit of excellence, success will inadvertently meet with you
  today Dan usaco bronze, adhere to progress a little bit every day, even more than brush a question, more than 10 minutes to learn English, and Fan Hao, Li Junhui chiefs and common progress for the early Sun West over the chiefs
  for an early win usaco Dan gold, informatics competitions, traditional algorithms senior scarcity of talent, talent higher education there is no market price, the opportunity to re-energized at hand, even if only one chance, should go all out
  and now I can only coaching group entry, and the entry group counseling can be too many people strive for self-improvement as soon as possible in the summer, the popularity of the topic group of brushing, the popularity of group counseling to improve self in the first place, to make money in the second place, to earn to earn a big.
  Rich view taken about the thick and thin of plot.

--- end --- restore content

Guess you like

Origin www.cnblogs.com/cxs070998/p/11117549.html