Trouver le temps de vol le plus court vers le tableau de destination Java


On m'a donc assigné ce programme monstre

public class AirlineRoutes
{
private int[][] routes;

/**
 *  Creates a table with direct flight times for a group of cities.
 */
public AirlineRoutes(int[][] routes)
{
    this.routes = routes;
}

/**
 *  Returns the shortest flying time from start to destination
 *  going through at most one intermediate city.
 *  @precondition 1 <= start <= N (where N is size of matrix)
 *  @precondition 1 <= destination <= N (where N is size of matrix)
 *      Note that cities are labeled 1 to N , whereas the matrix
 *      is indexed from 0 to N-1
 *  @param start the city to start the flight from
 *  @param destination the city to end the flight at
 *  @return returns the least number of hours going from start
 *          city to destination.  The flight can be direct or
 *          have a stop in one other city - Destination travleing
 *          through at most one intermediate city.
 */
public int minTime(int start, int destination)
{

}

/**
 *  Returns a string containing a chart showing the times
 *  to each city in hours.
 *  @return a table showing the time in hours for direct
 *          flights from every city.
 */
public String toString()
{
    String s = "\t\t\tDestination City\n\t\t";
    s += "\n\t\t\t";
    for (int k = 1; k <= routes[0].length; k++)
        s += "\t" + k;
    s += "\n\t\t";
    for (int k = 0; k <= routes[0].length; k++)
        s += "----";
    s += "-\n";
    for (int r = 0; r < routes[0].length; r++)
    {
        if (r == 1)
            s += "Start";
        else if (r == 2)
            s += "City";
        else
            s += "\t";
        s += "\t" + (r+1) + "\t|\t";
        for (int c = 0; c < routes[r].length; c++)
            s += routes[r][c] + "\t";               
        s += "\n";          
    }
    return s;
}

}

public class AirlineRoutesDriver
{
public static void main(String[] args)
{
    int[][] routes = new int[4][4];
    for (int k = 0; k < routes.length; k++)
        routes[k][k] = 0;
    routes[0][1] = 3;
    routes[0][2] = 2;
    routes[0][3] = 6;
    routes[1][0] = 3;
    routes[1][2] = 1;
    routes[1][3] = 3;
    routes[2][0] = 2;
    routes[2][1] = 1;
    routes[2][3] = 5;
    routes[3][0] = 5;
    routes[3][1] = 4;
    routes[3][2] = 2;

    AirlineRoutes r = new AirlineRoutes(routes);

    System.out.println(r);
    System.out.println("From city 4 to city 1  takes " + r.minTime(4,1) + " hours");
    System.out.println("From city 1 to 3 to city  " + r.minTime(1,3) + " hours");
    System.out.println("From city 3 to 3 to city  " + r.minTime(3,3) + " hours");
}
}

public class AirlineRoutesDriver
{
public static void main(String[] args)
{
    int[][] routes = new int[4][4];
    for (int k = 0; k < routes.length; k++)
        routes[k][k] = 0;
    routes[0][1] = 3;
    routes[0][2] = 2;
    routes[0][3] = 6;
    routes[1][0] = 3;
    routes[1][2] = 1;
    routes[1][3] = 3;
    routes[2][0] = 2;
    routes[2][1] = 1;
    routes[2][3] = 5;
    routes[3][0] = 5;
    routes[3][1] = 4;
    routes[3][2] = 2;

    AirlineRoutes r = new AirlineRoutes(routes);

    System.out.println(r);
    System.out.println("From city 4 to city 1  takes " + r.minTime(4,1) + " hours");
    System.out.println("From city 1 to 3 to city  " + r.minTime(1,3) + " hours");
    System.out.println("From city 3 to 3 to city  " + r.minTime(3,3) + " hours");
}
}

Je ne sais pas quoi faire pour la méthode minTime(). Je comprends que je dois trouver le temps le plus court du point A au point B, mais je ne sais pas comment écrire cela dans le code.

, Merci beaucoup!

Mise à Jour: j'ai trouvé un algorithme, mais je ne sais pas comment le traduire en java

Mise à jour 2: J'ai commencé à écrire du code, je le posterai ci-dessous. Ce n'est pas encore fini, j'ai besoin d'une instruction if après le ctr++;, mais je ne sais pas quoi mettre dedans...Et puis, j'ai aussi besoin de retourner un int dans cette méthode

public int minTime(int start, int destination)
{
 int ctr = 1;
 int ran = routes.length;
 int shortest = 0; //keeps track of the shortest time
 int longest = 0; //keeps track of the longest time
 int[][] otherArray = new int[routes.length][routes[ran].length]; //creates a new array to store the shortest time in
 for (int i = 0; i < routes.length; i++)
 {
    for (int j = 0; j < routes[i].length; j++)
    {
        otherArray[ctr] = otherArray[i][j+ctr] + otherArray[j+ctr][j];
        ctr++;
    }
 }
 //return some integer here!
}
Author: Gillian Franks, 2016-03-02

1 answers

, Vous devrez utiliser la programmation dynamique.

Algorithme: À partir de A, calculez tous les chemins possibles vers B. Sélectionnez le plus court.

 0
Author: Renuka Deshmukh, 2016-03-02 18:24:44