Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.lang.reflect.Array;
  6. import java.util.Arrays;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11.     public static double maxCost = 0;
  12.     public static double W = 0;
  13.  
  14.     public static class Item implements Comparable<Item>
  15.     {
  16.         int cost, weight;
  17.         boolean used;
  18.         public Item(int cost, int weight) {
  19.             this.cost = cost;
  20.             this.weight = weight;
  21.         }
  22.  
  23.         @Override
  24.         public String toString() {
  25.             return "Item{" +
  26.                     "cost=" + cost +
  27.                     ", weight=" + weight +
  28.                     '}';
  29.         }
  30.  
  31.         @Override
  32.         public int compareTo(Item o) {
  33.             long l1 = cost * o.weight;
  34.             long l2 = o.cost * weight;
  35.             return -Long.compare(l1, l2);
  36.         }
  37.     }
  38.  
  39.     public void run(Item[] items, int i, double curWeight, double curCost) throws FileNotFoundException {
  40.  
  41.         if (i <= items.length - 1)
  42.         {
  43.             if (curWeight + items[i].weight <= W )
  44.             {
  45.                 items[i].used = true;
  46.                 run(items, i + 1, curWeight + items[i].weight, curCost + items[i].cost);
  47.  
  48.             }
  49.  
  50.             else
  51.             {
  52.                 items[i].used = false;
  53.                 run(items, i + 1, curWeight, curCost);
  54.             }
  55.         }
  56.  
  57.         if (maxCost < curCost && W >= curWeight)
  58.         {
  59.             for (Item item: items) {
  60.                 if (item.used)
  61.                 {
  62.                     maxCost += (double)item.cost;
  63.                 }
  64.             }
  65.             run(items, i + 1, curWeight, curCost);
  66.         }
  67.     }
  68.  
  69.     public static void main(String[] args) throws FileNotFoundException {
  70.         Scanner input = new Scanner(new File("input.txt"));
  71.         int n = input.nextInt();
  72.         W = input.nextInt();
  73.         Item[] items = new Item[n];
  74.         System.out.println("Вес рюкзака: " + W);
  75.         for (int i = 0; i<n; i++)
  76.         {
  77.             items[i] = new Item(input.nextInt(), input.nextInt());
  78.         }
  79.         System.out.println("Список предметов:");
  80.         for (Item item: items) {
  81.             System.out.println(item);
  82.         }
  83.  
  84.         System.out.println();
  85.         int i = 0;
  86.         double curWeight = 0, curCost = 0;
  87.  
  88.         long startTime = System.nanoTime();;
  89.         Arrays.sort(items);
  90.         new Main().run(items, i, curWeight, curCost);
  91.         long finishTime = System.nanoTime();
  92.  
  93.         System.out.println("Оптимальная стоимость предметов: " + maxCost);
  94.         System.out.println("Время работы алгоритма ветвей и границ для рюкзака: " + (finishTime-startTime) + " ns");
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement