-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
93 lines (70 loc) · 3.01 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static double poidMinimal = Double.MAX_VALUE;
double poidCourant = 0;
static ArrayList<String> Ensemble = new ArrayList<String>();
static double[][] city;
static ArrayList<String> copieCity = new ArrayList<String>();
static ArrayList<String> finalTraject = new ArrayList<String>();
public static void genCity(int nbrVille) {
city = City.RandomArrayCity(nbrVille);
for (int i = 1; i <= city.length; i++)
copieCity.add("V" + i);
Ensemble.addAll(copieCity);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// define the number of City
System.out.println("Veuillez Entrer le nombre de ville:");
int nbrVille = sc.nextInt();
// set list E <---{V1.....Vn}
genCity(nbrVille);
// The user enters the initial city in the form V1 or V2 ..... Vn
System.out.println("Veuillez Entrer la ville initiale sous la forme V1 ou V2.....Vn:");
sc.nextLine();
String initialCity = sc.nextLine();
sc.close();
String element = initialCity;
if (!Ensemble.contains(element)) {
System.out.println("City enter is not in a list of city");
} else {
LocalTime time;
ArrayList<String> ancetre = new ArrayList<String>();
ArrayList<String> fils = new ArrayList<String>();
fils.addAll(Ensemble);
ancetre.add(element);
fils.remove(element);
time = LocalTime.now();
multiTarget(ancetre, fils, 0);
System.out.println(finalTraject.toString().replace("[", "").replace(",", " --> ").replace("]", ""));
System.out.println("poids du chemin: " + poidMinimal);
System.out.println("Execution Time:");
System.out.println(LocalTime.now() + " " + time + " " + ChronoUnit.MILLIS.between(time, LocalTime.now()));
}
}
public static void multiTarget(ArrayList<String> ancetre, ArrayList<String> fils, double poidCourant) {
String curenNode = ancetre.get(ancetre.size() - 1);
if (poidCourant >= poidMinimal) {
return;
}
if (ancetre.size() >= Ensemble.size()) {
poidMinimal = poidCourant;
finalTraject = ancetre;
return;
}
for (String e : fils) {
ArrayList<String> newAncetre = new ArrayList<String>();
ArrayList<String> newFils = new ArrayList<String>();
newAncetre.addAll(ancetre);
newAncetre.add(e);
newFils.addAll(fils);
newFils.remove(e);
double newWeight = poidCourant + city[copieCity.indexOf(curenNode)][copieCity.indexOf(e.toString())];
// System.out.println("poids du chemin: "+ newAncetre +" "+ newWeight );
multiTarget(newAncetre, newFils, newWeight);
}
}
}