Advertisement
Alisator

PAL 1

Oct 5th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. package pal;
  2.  
  3. import java.awt.Point;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * @author Alisa
  8.  */
  9. public class Main {
  10.  
  11.     public static Double actLength = 0.0;
  12.     public static Integer countVertexes = 0;
  13.     private static int koren = 0;
  14.     private static int MAXcountVertexes = 0;
  15.     private static Double MAXbestLenght = 0.0;
  16.  
  17.     public static void main(String[] args) {
  18.         Scanner sc = new Scanner(System.in);
  19.         //read N number of vertexes, max length of road
  20.         String[] splitInfo = sc.nextLine().split(" ");
  21.         int N = Integer.parseInt(splitInfo[0]);
  22.         int length = Integer.parseInt(splitInfo[1]);
  23.         Point[] pointArray = new Point[N];
  24.         Point p;
  25.         //read N vertexes (x,y)
  26.         for (int i = 0; i < N; i++) {
  27.             p = new Point();
  28.             p.x = sc.nextInt();
  29.             p.y = sc.nextInt();
  30.             pointArray[i] = p;
  31.         }
  32.         boolean[] visitedVertexes = new boolean[N];
  33.  
  34.         //create edges and compute their length
  35.         Double[][] edgesFromVertexesLength = new Double[N][N];
  36.         for (int i = 0; i < N; i++) {
  37.             for (int j = 0; j < N; j++) {
  38.                 if (i == j) {
  39.                     edgesFromVertexesLength[i][j] = 0.0;
  40.                 } else {
  41.                     double u1 = Math.pow(pointArray[j].x - pointArray[i].x, 2);
  42.                     double u2 = Math.pow(pointArray[j].y - pointArray[i].y, 2);
  43.                     double size = Math.sqrt(u1 + u2);
  44.                     edgesFromVertexesLength[i][j] = size;
  45.                 }
  46.                 visitedVertexes[i] = false;
  47.             }
  48.         }
  49.         //vektor of visited Vertexes in DFS
  50.         //calling DFS
  51.         for (int i = 0; i < N; i++) {
  52.             koren = i;
  53.             dfs(edgesFromVertexesLength, i, visitedVertexes, length);
  54.             visitedVertexes[i] = true;
  55.             if (MAXcountVertexes == N - i) {
  56.                 break;
  57.             }
  58.         }
  59.  
  60.         System.out.println((int) Math.ceil(MAXbestLenght));
  61.     }
  62.  
  63.     static void dfs(Double adjMatrix[][], int indexVertex, boolean[] visited, int maxLength) {
  64.         visited[indexVertex] = true;
  65.         if (countVertexes > MAXcountVertexes) {
  66.                     MAXcountVertexes = countVertexes;
  67.                     MAXbestLenght = actLength + adjMatrix[koren][indexVertex];
  68.  
  69.        }
  70.          else if (countVertexes == MAXcountVertexes && (actLength + adjMatrix[koren][indexVertex] < MAXbestLenght))      {
  71.                     MAXbestLenght = actLength + adjMatrix[koren][indexVertex];
  72.                 }
  73.  
  74.         for (int i = koren; i < visited.length; i++) {
  75.             //jeste nenavstiveny bod
  76.             if (visited[i] == false) {
  77.                 //pocet vrcholu je maximalni, ale cesta je vetsi nez nejlepsi
  78.                 // if (countVertexes == (visited.length - koren) && actLength + adjMatrix[indexVertex][i] + adjMatrix[koren][i] > MAXbestLenght) {
  79.                 //     continue;
  80.                 // }
  81.                 //nova delka vetsi + zpet do korene > nez povolena
  82.                 if ((actLength + adjMatrix[indexVertex][i] + adjMatrix[koren][i]) > maxLength) {
  83.                     continue;
  84.                 }
  85.                 //pocet vrcholu je ok, ale nova delka vetsi nez povolena
  86.                 if ((countVertexes  + 4 < MAXcountVertexes) && (actLength + adjMatrix[koren][i] + adjMatrix[indexVertex][i] > MAXbestLenght)) {
  87.                     continue;
  88.                 }
  89.                
  90.                  //nalezena delsi cesta se stejnym poctem vrcholu
  91.                
  92.                 actLength += adjMatrix[indexVertex][i];
  93.                 //musĂ­ se uchovat s navstivene body
  94.                 countVertexes++;
  95.                 dfs(adjMatrix, i, visited, maxLength);
  96.                 //odecteme posledni vrchol a hranu
  97.                 countVertexes--;
  98.                 actLength -= adjMatrix[indexVertex][i];
  99.                 visited[i] = false;
  100.             }
  101.  
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement