コード例 #1
0
int main()
{
  int i,sum,c,oxsum,nisum,ox,ni,n;
  int oxygen[1001],nitrogen[1001],weight[1001];
  scanf("%d",&c);
  while(c--)
  {
    scanf("%d %d",&ox,&ni);
    scanf("%d",&n);
    oxsum=nisum=0;
    for(i=1;i<=n;i++)
      {
       scanf("%d %d %d",&oxygen[i],&nitrogen[i],&weight[i]);
       oxsum+=oxygen[i];
       nisum+=nitrogen[i];
      }
   // col=(ox>ni)?ox:ni;
    memset(oxb,0,sizeof(oxb));
    memset(nib,0,sizeof(nib));    
    knapsack(oxygen,weight,n,oxsum-ox);
    printk(n,oxsum-ox);
    calculate(n,ox,oxygen);
    knapsack(nitrogen,weight,n,nisum-ni);
    calculate2(n,ni,nitrogen);
    print(oxb,n);
    print(nib,n);
    sum=0;
    for(i=1;i<=n;i++)
      if(oxb[i]==0 || nib[i]==0)
        sum+=weight[i];
    printf("%d\n",sum);
  }
  return 0;
}    
コード例 #2
0
ファイル: knapsack.cpp プロジェクト: mincongzhang/Algorithms
int knapsack(int n, int w)
{
    if (w < 0) return -1e9;
    if (n == 0) return 0;
    if (c[n][w]) return c[n][w];

    return c[n][w] = std::max(
        knapsack(n-1, w-weight[n]) + cost[n],
        knapsack(n-1, w)
    );
}
コード例 #3
0
ファイル: knap.c プロジェクト: deepw/FirstRepo
int
knapsack (int *values, int *sizes, int i, int n, int max_size) 
{
	if (i >= n)
		return 0;

	if (sizes[i] > max_size) {
		return knapsack (values, sizes, i+1, n, max_size);
	}

	return max (knapsack (values, sizes, i+1, n, max_size), knapsack (values, sizes, i+1, n, max_size-sizes[i]) + values[i]);
}
コード例 #4
0
unsigned knapsack(unsigned weights[], unsigned values[], unsigned capacity, unsigned items) {
	if (items == 0) {
		return 0;
	}

	unsigned res = knapsack(weights, values, capacity, items-1);
	if (weights[items-1] <= capacity) {
		res = max(res, values[items-1]+knapsack(weights, values, capacity-weights[items-1], items-1));
	}

	return res;
}
コード例 #5
0
int main(){
	weightedItem goldStatue = {4, 10, "Gold Statue"};
	weightedItem fountainPen = {3, 7, "Fountain Pen"};
	weightedItem crystalBall = {2, 4, "Crystal Ball"};
	weightedItem goldRing = {1, 1, "Gold Ring"};


	std::vector<weightedItem> inputVector(4);
	inputVector[0] = goldStatue;
	inputVector[1] = crystalBall;
	inputVector[2] = fountainPen;
	inputVector[3] = goldRing;

	// Get items.
	std::vector<weightedItem> itemsUsed = knapsack(inputVector, 8);

	// Print them out.
	std::cout << "\nItems Used to Find Max: \n";
	for(int x = 0; x < itemsUsed.size(); x++){
		std::cout << itemsUsed[x].name << ", \t";
	}

	std::cout << "\n";
	return 0;
}
コード例 #6
0
ファイル: 背包问题.cpp プロジェクト: chengn/AlgorithmRun
int  main()
{
 int n,c;
 int **m;
 cout<<"&&&&&&&&&&&&&&&&&&&&&欢迎使用0-1背包问题程序&&&&&&&&&&&&&&&&&&&"<<endl;
 cout<<"请输入物品个数和重量上限:";
 cin>>n>>c;
   int *v=new int[n+1];
 cout<<"Pls input the property (v[i]):"<<endl;
 for(int i=1;i<=n;i++)
  cin>>v[i];
 int *w=new int[n+1];
 cout<<"Pls input the weight (w[i]):"<<endl;
 for(int j=1;j<=n;j++)
  cin>>w[j];
 int *x=new int[n+1];
 m=new int*[n+1];  //动态的分配二维数组
 for(int p=0;p<n+1;p++)
 {
  m[p]=new int[c+1];
} 
 
 knapsack(v,w,c,n,m);
 traceback(m,w,c,n,x);
 
 
 cout<<endl;
 system("pause");

}
コード例 #7
0
ファイル: hack.c プロジェクト: pankajverma232/Programming
int main() {
int T,N,n,K,num;
int w[2000],v[2000];
int k,j,i;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&K);
i=N;
k=0;
while(i--)
{
j=1;
n=K;
scanf("%d",&num);
while(n)
{
v[k]=j;
w[k]=num*j;
k=k+1;
j=j+1;
n=K/(num*j);
}
}

for(j=0;j<k;j++)
printf("%d\t",w[j]);
printf("\n");
for(j=0;j<k;j++)
printf("%d\t ",v[j]);
}
printf("\n%d\n",knapsack(K,w,v,k));
return 0;
}
コード例 #8
0
int main() {
    int i, j, n, m, cost[10], weight[10], maxValue;

    printf("Enter the number of items: ");
    scanf("%d", &n);

    printf("Enter the weights of each item:\n");
    for (i = 1; i <= n; i++)
        scanf("%d", &weight[i]);

    printf("Enter the values of each item:\n");
    for (i = 1; i <= n; i++)
        scanf("%d", &cost[i]);

    printf("Enter the capacity of knapsack: ");
    scanf("%d", &m);

    maxValue = knapsack(n, m, cost, weight);

    printf("Solution to the Knapsack Problem:\n");
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++)
            printf("%d ", value[i][j]);
        printf("\n");
    }

    printf("\nThe maximum value is: %d", maxValue);

    return 0;
}
コード例 #9
0
int main() {
  int N = __VERIFIER_nondet_int();
  int M = __VERIFIER_nondet_int();
  if (N < 1) {
     N = 1;
  }
  if (M < 1) {
     M = 1;
  }
  int *size = (int*) alloca(N * sizeof(int));
  int *val = (int*) alloca(N * sizeof(int));
  int *cost = (int*) alloca(M * sizeof(int));
  int *best = (int*) alloca(M * sizeof(int));
	
	for(int i = 0; i < N; i++) 
	{
	  size[i] = __VERIFIER_nondet_int();
		val[i] = __VERIFIER_nondet_int();
	}
	
	for(int i = 0; i < M; i++)
	{
	  cost[i] = __VERIFIER_nondet_int();
	}
  
	knapsack(size, val, N, cost, best, M);
  return 0;
}
コード例 #10
0
ファイル: main.c プロジェクト: adamsx97/Practice-and-Work
int main(int argc, char **argv){
  int* weights;
  unsigned int* values;
  unsigned int num_items;
  int capacity;
  unsigned int max_value;

  
  if(argc == 2){
  	//readFile
  	readFile(argv[1], &weights, &values, &num_items, &capacity);
  	
  	//find the max that we can carry
  	max_value = knapsack(weights, values, num_items, capacity, 0);
  	printf("%u\n", max_value); //display result
  	
  	//free space malloced
  	free(weights);
  	free(values);
  } else{
  	printf("Usage: knapsack.out arg_file\n");
  }

  return 0;
  
 }//main
コード例 #11
0
ファイル: main.cpp プロジェクト: Raymond-Zhu/CSC220_Programs
int main()
{
    /* Initialize and declare the parameters */
    std::map<int, bool> bestSet;
    std::map<int, bool> include;
    std::vector<std::pair<int,int> > profitAndWeight;
    int numBest = 0;
    int maxProfit = 0;
    int maxWeight = 9; /* Really shouldn't hard code this. */
    int index = 0;

    /* Reads the file and stores the data */
    profitAndWeight = readFile("data.txt");

    /* Solves the knapsack problem */
    knapsack(index,profitAndWeight,maxWeight,0,0,maxProfit,bestSet,numBest,include);

    std::cout << maxProfit << std::endl;
    for (std::map<int,bool>::const_iterator i = bestSet.begin(); i != bestSet.end(); ++i) {
        if(i->second) {
            std::cout << "Item " << i->first - 1 << " has been included" << std::endl; /* There's a -1 here because for some reason, while the profit is right, the item number is off by 1.
                                                                                          it chooses items 2 and 4 instead of 1 and 3 but the profit is still 55. Couldn't figure out why */
        }
    }

    return 0;
}
コード例 #12
0
ファイル: 5knapsackWEB.c プロジェクト: shubhamchaudhary/uiet
//****Main****
int main(){
    int noItem=4;
    int knapSize = 10;
    int weights[4]={5,4,6,3};
    int values[4]={10,40,30,50};
    printf("Max Value: %d",knapsack(noItem-1,knapSize,weights,values));
    return 0;
}
コード例 #13
0
int main() {
    int val[]= {6,10,12};
    int wt[]= {1,2,3};
    int W=5;
    int n = sizeof(val)/sizeof(val[0]);
    printf("\nmaximum value : %d\n\n",knapsack(W,wt,val,n));
    return 0;
}
コード例 #14
0
int knapsack(int W , int wt[], int v[], int n)
{
	// Base Case
	if( n == 0 || W == 0 )
	{
		return 0;
	}
    
    // If weight of the nth item is more than Knapsack capacity W, then
    // this item cannot be included in the optimal solution
	if (wt[n-1] > W)
		return knapsack(W,wt,v,n-1);
    
	else return max(v[n-1] + knapsack (W - wt[n-1], wt, v , n-1),
                    knapsack(W,wt,v,n-1));
    
}
コード例 #15
0
ファイル: d637.c プロジェクト: gs0622/zerojudge
int main(void)
{
    int n, i;
    scanf("%d\n", &n);
    for (i=0;i<n;i++) scanf("%d %d\n", &d[i].vol, &d[i].full);
    printf("%d\n", knapsack(n, 100));
    return 0;
}
コード例 #16
0
ファイル: knap.c プロジェクト: deepw/FirstRepo
int main ()
{
	int values[] = {3, 2, 1, 1, 1, 1};
	int  sizes[] = {2, 3, 3, 3, 3, 1};
        int Size = 4;	
	printf ("Max value = %d\n", knapsack (values, sizes, 0, sizeof(sizes)/sizeof(sizes[0]), Size));
	return 0;
}
コード例 #17
0
ファイル: tests.c プロジェクト: alex011235/algorithm
void test_knapsack()
{
      int v[] = {15, 10, 9, 5};
      int w[] = {1, 5, 3, 4};
      size_t n = sizeof v / sizeof *v;
      int W = 8;
      int a = knapsack(v,w,n,W);
      printf("%d\n", a);
}
コード例 #18
0
int main()
{
    int val[] = {60, 100, 120};
    int wt[] = {10, 20, 30};
    int  W = 50;
    int n = sizeof(val)/sizeof(val[0]);
    printf("\nValue = %d", knapsack(W, wt, val, n));
    return 0;
}
コード例 #19
0
// run with -p 20 -l 5000
int main(int argc, char *argv[])
{
  struct item items[MAX_ITEMS];	/* array of items */
  int n, capacity, sol, benchmark, help;
  char filename[100];

  my_timer_t t;
  /* standard benchmark options */
  strcpy(filename, "knapsack-example3.input");

#ifdef CILK_BLOCK
  cilk_io_init();
#endif

  get_options(argc, argv, specifiers, opt_types, &prob, &length, filename, &benchmark, &help);

  if (help)
    return usage();

  if (benchmark) {
    switch (benchmark) {
      case 1:		/* short benchmark options -- a little work */
        strcpy(filename, "knapsack-example1.input");
        break;
      case 2:		/* standard benchmark options */
        strcpy(filename, "knapsack-example2.input");
        break;
      case 3:		/* long benchmark options -- a lot of work */
        strcpy(filename, "knapsack-example3.input");
        break;
    }
  }
  if (read_input(filename, items, &capacity, &n))
    return 1;

  /* Timing. "Start" timers */
  cilk_sync;
  printf("prob = %lu\tlength=%lu\tn = %lu\n", prob, length, n);

  TIMER_RESET(t);
  TIMER_START(t);
  sol = cilk_spawn knapsack(items, capacity, n, 0);
  cilk_sync;
  TIMER_STOP(t);

#ifdef CILK_BLOCK
  cilk_io_teardown();
#endif

  printf("\nCilk Example: knapsack\n");
  printf("options: problem-file = %s\n\n", filename);
  printf("Best value is %d\n\n", sol);
  printf("Running time  = %4f s\n", TIMER_EVAL(t));

  return 0;
}
コード例 #20
0
/* 
 * return the optimal solution for n items (first is e) and
 * capacity c. Value so far is v.
 */
int knapsack(struct item *e, int c, int n, int v)
{
  int with, without, best;
  double ub;

  /* base case: full knapsack or no items */
  if (c < 0)
    return INT_MIN;

  if (n == 0 || c == 0)
    return v;		/* feasible solution, with value v */

  ub = (double) v + c * e->value / e->weight;

  if (ub < best_so_far) {
    /* prune ! */
    return INT_MIN;
  }
  /* 
   * compute the best solution without the current item in the knapsack 
   */
  perturb(prob, n, length);
  without = cilk_spawn knapsack(e + 1, c, n - 1, v);

  /* compute the best solution with the current item in the knapsack */
  with = cilk_spawn knapsack(e + 1, c - e->weight, n - 1, v + e->value);

  cilk_sync;

  best = with > without ? with : without;

  /* 
   * notice the race condition here. The program is still
   * correct, in the sense that the best solution so far
   * is at least best_so_far. Moreover best_so_far gets updated
   * when returning, so eventually it should get the right
   * value. The program is highly non-deterministic.
   */
  if (best > best_so_far)
    best_so_far = best;

  return best;
}
コード例 #21
0
ファイル: PARTY.cpp プロジェクト: Vijeta141/SportProgramming
int main(){
	int n,i,W;
	scanf("%d%d",&W,&n);
	while(n&&W){	
		for(i=0;i<n;i++)
			scanf("%d%d",&w[i],&c[i]);
		knapsack(n,W);
		scanf("%d%d",&W,&n);
	}
}
コード例 #22
0
ファイル: knapsack.cpp プロジェクト: mincongzhang/Algorithms
int main(){
  cost[1]=2;
  cost[2]=4;
  cost[3]=5;
  weight[1]=4;
  weight[2]=6;
  weight[3]=7;
  int ks_result = knapsack(4,10);
  std::cout<<"Best value :"<<ks_result<<std::endl;
}
コード例 #23
0
int main() {
  int *size;
  int *val;
  int N = __VERIFIER_nondet_int();
  int *cost;
  int *best;
  int M = __VERIFIER_nondet_int();
  knapsack(size, val, N, cost, best, M);
  return 0;
}
コード例 #24
0
int main()
{
	int v[] = {2,3,4,5};
	int wt[] = {2,4,6,9};
	int W = 8;
	int n = 4;
	int m = knapsack(W,wt,v,n);
	printf("The output is %d\n", m);
	return 0;
    
}
コード例 #25
0
ファイル: knapsack.c プロジェクト: rafaldworaczek/Main
int main() {
  int n, size, i;
  scanf("%d %d", &size, &n);
  int val[n];
  int weight[n];
  for (i = 0; i < n; i++) 
    scanf("%d %d", &val[i], &weight[i]);

  printf("Max val: %d\n", knapsack(size, n, val, weight));
  return 0; 
}
コード例 #26
0
ファイル: Backpack.cpp プロジェクト: ShiyangHuang/AP
int LimitedKnapsack(std::vector<int> c, std::vector<int> w, int B, std::vector<int> n){
	std::vector<int> new_c, new_w;
	for (int i = 0; i < n.size(); ++i)
	{
		std::vector<int> d = Divide(n[i]);
		for(auto l : d){
			new_c.push_back(l*c[i]);
			new_w.push_back(l*w[i]);
		}
	}
	return knapsack(new_c,new_w,B);
}
コード例 #27
0
ファイル: knapsack.c プロジェクト: hoswan/C
int main(){
 
  int W;
  int wt[] = {50, 200, 300};
  int val[] = {10, 20, 30};
  W = 250;

  int n = sizeof(val) / sizeof(int);
  printf("knapsack = %d\n",knapsack(wt, val, W, n));

  return 0;
}
コード例 #28
0
int main(){
    int nItems = 4;
    int knapsackSize = 10;
    int weights[4] = {5,4,6,3};
    int values[4] = {10,40,30,50};

    printf("Max value = %d\n",knapsack(nItems,knapsackSize,weights,values));
    printf("Picks were: ");
    printPicks(nItems,knapsackSize, weights);

    return 0;
}
コード例 #29
0
int main(){
    int nItems = 2;
    int knapsackSize = 7;
    int weights[2] = {6,5};
    int values[2] = {1,1};

    printf("Max value = %dn",knapsack(nItems,knapsackSize,weights,values));
    printf("Picks were: ");
    printPicks(nItems,knapsackSize, weights);

    return 0;
}
コード例 #30
0
ファイル: knapsack.cpp プロジェクト: INomNom150I/Calculator
int main(){
    int v[3] = {60,100,120};
    int w[3] = {10,20,30};
    clock_t t = clock();
    std::cout << knap(w,v,3,50) << std::endl;
    std::cout << (clock() - (float)t)/CLOCKS_PER_SEC << std::endl;

    t = clock();
    std::cout << knapsack(w,v,3,50) << std::endl;
    std::cout << (clock() - (float)t)/CLOCKS_PER_SEC << std::endl;
    return 0;
}