2016-11-04 5 views
0

비행 여정 시스템의 최단 경로 총 비용을 계산하려고하지만 길이/비용 대신 걸리는 경로 수를 계산하는 것으로 보입니다. Flight_Info 개체에서 각 여행 당 비용을 얻는 방법을 연습 할 수 없습니다.JGraphT 그래프 최단 경로

여기 내 코드입니다 : 난 단지 추측하고있어

import java.util.Scanner; 
import org.jgrapht.graph.*; 
import org.jgrapht.Graphs; 
import org.jgrapht.alg.DijkstraShortestPath; 

public class Flights2 { 

public static SimpleDirectedWeightedGraph<String, Flight_Info> createGraph() { 

    SimpleDirectedWeightedGraph<String, Flight_Info> airport = new SimpleDirectedWeightedGraph<String, Flight_Info>(
      Flight_Info.class); 

    String Edinburgh = new String("Edinburgh"); 
    String Heathrow = new String("Heathrow"); 
    String Dubai = new String("Dubai"); 
    String Sydney = new String("Sydney"); 
    String KualaLumpur = new String("Kuala Lumpur"); 
    String Frankfurt = new String("Frankfurt"); 
    String Aukland = new String("Aukland"); 
    String RioDeJanerio = new String("Rio De Janerio"); 
    String NewYork = new String("New York"); 
    String Santiago = new String("Santiago"); 

    airport.addVertex(Edinburgh); 
    airport.addVertex(Heathrow); 
    airport.addVertex(Dubai); 
    airport.addVertex(Sydney); 
    airport.addVertex(KualaLumpur); 
    airport.addVertex(Frankfurt); 
    airport.addVertex(Aukland); 
    airport.addVertex(RioDeJanerio); 
    airport.addVertex(NewYork); 
    airport.addVertex(Santiago); 



    Flight_Info FL1001 = new Flight_Info("FL1001", 1530, 1630, 1, 80); 
    airport.addEdge(Edinburgh, Heathrow, FL1001); 
    Flight_Info FL1002 = new Flight_Info("FL1002", 1630, 1730, 1, 80); 
    airport.addEdge(Heathrow, Edinburgh, FL1002); 
    Flight_Info FL1003 = new Flight_Info("FL1003", 1630, 1730, 1, 80); 
    airport.addEdge(Heathrow, Dubai, FL1003); 
    Flight_Info FL1004 = new Flight_Info("FL1004", 1630, 1730, 1, 80); 
    airport.addEdge(Dubai, Heathrow, FL1004); 
    Flight_Info FL1005 = new Flight_Info("FL1005", 1630, 1730, 1, 80); 
    airport.addEdge(Heathrow, Sydney, FL1005); 
    Flight_Info FL1006 = new Flight_Info("FL1006", 1630, 1730, 1, 80); 
    airport.addEdge(Sydney, Heathrow, FL1006); 
    Flight_Info FL1007 = new Flight_Info("FL1007", 1630, 1730, 1, 80); 
    airport.addEdge(Dubai, KualaLumpur, FL1007); 
    Flight_Info FL1008 = new Flight_Info("FL1008", 1630, 1730, 1, 80); 
    airport.addEdge(KualaLumpur, Dubai, FL1008); 
    Flight_Info FL1009 = new Flight_Info("FL1009", 1630, 1730, 1, 80); 
    airport.addEdge(Dubai, Edinburgh, FL1009); 
    Flight_Info FL1010 = new Flight_Info("FL10010", 1630, 1730, 1, 80); 
    airport.addEdge(Edinburgh, Dubai, FL1010); 
    Flight_Info FL1011 = new Flight_Info("FL1011", 1630, 1730, 1, 80); 
    airport.addEdge(KualaLumpur, Sydney, FL1011); 
    Flight_Info FL1012 = new Flight_Info("FL1012", 1630, 1730, 1, 80); 
    airport.addEdge(Sydney, KualaLumpur, FL1012); 
    Flight_Info FL1013 = new Flight_Info("FL1013", 1630, 1730, 1, 80); 
    airport.addEdge(Edinburgh, Frankfurt, FL1013); 
    Flight_Info FL1014 = new Flight_Info("FL1014", 1630, 1730, 1, 80); 
    airport.addEdge(Frankfurt, Edinburgh, FL1014); 
    Flight_Info FL1015 = new Flight_Info("FL1015", 1630, 1730, 1, 80); 
    airport.addEdge(Sydney, Aukland, FL1015); 
    Flight_Info FL1016 = new Flight_Info("FL1016", 1630, 1730, 1, 80); 
    airport.addEdge(Aukland, Sydney, FL1016); 
    Flight_Info FL1017 = new Flight_Info("FL1017", 1630, 1730, 1, 80); 
    airport.addEdge(RioDeJanerio, NewYork, FL1017); 
    Flight_Info FL1018 = new Flight_Info("FL1018", 1630, 1730, 1, 80); 
    airport.addEdge(NewYork, RioDeJanerio, FL1018); 
    Flight_Info FL1019 = new Flight_Info("FL1019", 1630, 1730, 1, 80); 
    airport.addEdge(Santiago, NewYork, FL1019); 
    Flight_Info FL1020 = new Flight_Info("FL1020", 1630, 1730, 1, 80); 
    airport.addEdge(NewYork, Santiago, FL1020); 

    return airport; 
} 

public static void itinerary(SimpleDirectedWeightedGraph<String, Flight_Info> airport, String departure, 
     String destination) { 
    DijkstraShortestPath<String, Flight_Info> p = new DijkstraShortestPath<String, Flight_Info>(airport, departure, 
      destination); 




    System.out.println(p.getPathEdgeList()); 
    System.out.println("Cost of shortest (i.e cheapest) path = £" + p.getPathLength()); 
} 

public static void main(String args[]) { 

    SimpleDirectedWeightedGraph<String, Flight_Info> airport = createGraph(); 
    System.out.println("The following airports are in use:" + airport.vertexSet()); 
    @SuppressWarnings("resource") 
    Scanner s = new Scanner(System.in); 
    System.out.println("Please enter the starting airport:"); 
    String departure = s.nextLine(); 
    System.out.println("Please enter the destination aiport:"); 
    String destination = s.nextLine(); 
    itinerary(airport, departure, destination); 


} 

import org.jgrapht.graph.DefaultWeightedEdge; 

public class Flight_Info extends DefaultWeightedEdge { 

private String departure, destination, flightNumber; 
private int departureTime, arrivalTime, duration, ticketPrice; 

public Flight_Info() { 

} 

public Flight_Info(String flightNumber, int departureTime, int arrivalTime, int duration, int ticketPrice) { 
    super(); 
    this.flightNumber = flightNumber; 
    this.departureTime = departureTime; 
    this.arrivalTime = arrivalTime; 
    this.duration = duration; 
    this.ticketPrice = ticketPrice; 

} 


public int getDuration(){ 
    return duration; 
} 
+0

에 오신 것을 환영합니다! 좋은 질문을하고 좋은 대답을 얻으려면 [SO Question Checklist] (http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)를 검토하십시오. –

+0

수정 된 형식 및 여분의 브래킷 – gdlmx

답변

0

, 그러나 당신의 Flight_Info 클래스는 티켓 가격에 대한 게터 부족 것으로 보인다. 그런 다음 가장자리에서 계산 된 최단 경로 비용을 읽습니다. 최단 경로의 가장자리를 반복하여 티켓 가격을 합산하는 것이 더 좋을 것이라고 생각합니다. 내가 말했듯이

: 스택 오버플로 그냥 ... 짧은보기 이후 추측 =)

안부

알렉스

+0

도움 주셔서 감사합니다. –

+0

내 대답이 당신의 문제를 해결 했습니까? 그렇다면 답을 해결 된 것으로 표시해 주시겠습니까? 감사! – Quurz