Guest User

Untitled

a guest
Jan 13th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5.  
  6. public class Algorithm {
  7.  
  8.     private ArrayList<ArrayList<Integer>> g = new ArrayList<>();
  9.     private ArrayList<Boolean> visited = new ArrayList<>();
  10.     private ArrayList<Integer> path = new ArrayList<>();
  11.     private ArrayList<Integer> optimal_path = new ArrayList<>();
  12.  
  13.     private int n = 0;
  14.     private int min_weight = 99999999;
  15.     private int start = 0;
  16.     private int finish = 0;
  17.  
  18.     private Visualization visual;
  19.  
  20.     Algorithm(Visualization visual){
  21.         this.visual = visual;
  22.     }
  23.  
  24.     private void DFS(int v, int from, int steps, int weight)
  25.     {
  26.  
  27.  
  28.         if (visited.get(v))
  29.         {
  30.             return;
  31.         }
  32.  
  33.         turnOn(from, v);
  34.  
  35.         if(min_weight < weight)
  36.         {
  37.             turnOff(from, v);
  38.             return;
  39.         }
  40.  
  41.         visited.set(v, true);
  42.         path.set(v, from);
  43.         if (steps == 0 && g.get(v).get(start)!= -1)
  44.         {
  45.             weight += g.get(v).get(start);
  46.             if(weight < min_weight){
  47.                 min_weight = weight;
  48.                 optimal_path = (ArrayList<Integer>) path.clone();
  49.                 finish = v;
  50.             }
  51.         }
  52.  
  53.  
  54.  
  55.         for(int i = 0; i < n; i++)
  56.         {
  57.             if(g.get(v).get(i) == -1)
  58.             {
  59.                 continue;
  60.             }
  61.             DFS(i, v, steps - 1, weight + g.get(v).get(i));
  62.         }
  63.         visited.set(v, false);
  64.  
  65.  
  66.         turnOff(from, v);
  67.     }
  68.  
  69.     private void turnOn(int from, int v){
  70.  
  71.         visual.turnOnVertex(v);
  72.         visual.turnOnEdge(from, v);
  73.         waitSecond();
  74.  
  75.     }
  76.  
  77.     private void turnOff(int from, int v){
  78.  
  79.         visual.turnOffVertex(v);
  80.         visual.turnOffEdge(from, v);
  81.         waitSecond();
  82.  
  83.     }
  84.  
  85.     private void waitSecond(){
  86.         try {
  87.             Thread.sleep(500);
  88.         } catch (InterruptedException e) {
  89.             e.printStackTrace();
  90.         }
  91.     }
  92.  
  93.  
  94.     private ArrayList<Integer> get_path()
  95.     {
  96.         ArrayList<Integer> ans = new ArrayList<>();
  97.         int prev = -1;
  98.         visual.turnOnVertex(start);
  99.         for (int v = finish; v != start; v = optimal_path.get(v))
  100.         {
  101.             if(prev != -1 && v != -1){
  102.                 visual.turnOnEdge(prev, v);
  103.             }
  104.             visual.turnOnVertex(v);
  105.  
  106.             ans.add(v);
  107.         }
  108.         ans.add(start);
  109.         Collections.reverse(ans);
  110.         return ans;
  111.     }
  112.  
  113.     public void start(){
  114.         getInput();
  115.         updateViz();
  116.         DFS(0, -1, n - 1, 0);
  117.         showAnswer();
  118.     }
  119.  
  120.     private void updateViz(){
  121.         ArrayList<Point> points = getCoordinates();
  122.         for(int i = 0; i < n; i++){
  123.             visual.addVertex(i, (int)(points.get(i).x * 250) + 250, (int)(points.get(i).y * 250) + 250);
  124.             //visual.turnOnVertex(i);
  125.             //visual.turnOffVertex(i);
  126.         }
  127.  
  128.         for(int i = 0; i < n; i++){
  129.             for(int j = i; j < n; j++){
  130.                 if(i == j) continue;
  131.                 visual.addEdge(i, j, g.get(i).get(j), true);
  132.                 //visual.turnOnEdge(i, j);
  133.             }
  134.         }
  135.  
  136.  
  137.         for(int i = 0; i < n; i++){
  138.             for(int j = 0; j < i; j++){
  139.                 visual.addEdge(i, j, g.get(i).get(j), false);
  140.                 //visual.turnOnEdge(i, j);
  141.             }
  142.         }
  143.     }
  144.  
  145.     private void getInput(){
  146.         System.out.println("Введите количество городов: ");
  147.         Scanner cin = new Scanner(System.in);
  148.         n = cin.nextInt();
  149.         System.out.println("Введите матрицу смежности: ");
  150.         for(int i = 0; i < n; i++) {
  151.             g.add(new ArrayList<>());
  152.             path.add(-1);
  153.             visited.add(false);
  154.             for (int j = 0; j < n; j++) {
  155.                 int k = cin.nextInt();
  156.                 g.get(i).add(k);
  157.             }
  158.         }
  159.     }
  160.  
  161.     private void showAnswer(){
  162.         ArrayList<Integer> a = get_path();
  163.         for(int i : a){
  164.             System.out.print(i+" ");
  165.         }
  166.         System.out.println("\n"+min_weight);
  167.     }
  168.  
  169.     private ArrayList<Point> getCoordinates(){
  170.         ArrayList<Point> coord = new ArrayList<>();
  171.         double step = (2*Math.PI)/n;
  172.         for(double i = 0; i < 2*Math.PI; i += step){
  173.             double y = Math.sin(i);
  174.             double x = Math.cos(i);
  175.             coord.add(new Point(x, y));
  176.         }
  177.         return coord;
  178.     }
  179. }
  180.  
  181.  
  182.  
  183. /*
  184. TESTS:
  185.  
  186.  3
  187.  -1 50 100
  188.  999 -1 15
  189.  188 13 -1
  190.  
  191.  0 - 2 - 1
  192.  253
  193.  
  194.  5
  195.  -1 34  76  45  49
  196.  64 -1  34  85  435
  197.  64 5   -1  9999    3
  198.  1  5   4   -1  8
  199.  45 9   1   5   -1
  200.  
  201.  0 - 4 - 3 - 2 - 1
  202.  77
  203.  
  204.  5
  205.  -1 34  76  45  49
  206.  64 -1  0   85  435
  207.  64 5   -1  9999    0
  208.  0  5   4   -1  8
  209.  45 9   1   5   -1
  210.  
  211.  0 - 4 - 3 - 2 - 1
  212.  39
  213.  
  214.  6
  215.  -1 34  76  45  49 3
  216.  64 -1  0   85  435 5
  217.  64 5   -1  9999    0 8
  218.  0  5   4   -1  8 5
  219.  45 9   1   5   -1 4
  220.  12 32 54 239 438 -1
  221.  
  222.  0 - 5 - 1 - 2 - 4 - 3
  223.  40
  224.  
  225.  7
  226.  -1 34  76  45  49 3 5
  227.  64 -1  0   85  435 5 1
  228.  64 5   -1  9999    0 8 2
  229.  0  5   4   -1  8 5 5
  230.  45 9   1   5   -1 4 4
  231.  12 32 54 239 438 -1 2
  232.  456 7 4 7 4 9 -1
  233.  
  234.  0 - 6 - 5 - 4 - 3 - 2 - 1
  235.  17
  236.  
  237.  */
Advertisement
Add Comment
Please, Sign In to add comment