-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijk.java
More file actions
209 lines (171 loc) · 4.65 KB
/
dijk.java
File metadata and controls
209 lines (171 loc) · 4.65 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import java.util.Scanner;
import java.util.Arrays;
public class dijk{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Dijkstra d = new Dijkstra(20);
d.input("a","b",2);
d.input("a","e",5);
d.input("b","c",2);
d.input("b","f",5);
d.input("c","d",3);
d.input("c","g",5);
d.input("d","i",6);
d.input("e","f",2);
d.input("e","j",1);
d.input("f","g",2);
d.input("g","o",3);
d.input("g","h",2);
d.input("h","i",1);
d.input("i","t",3);
d.input("i","r",10);
d.input("t","r",11);
d.input("j","k",4);
d.input("k","l",5);
d.input("l","m",4);
d.input("o","p",2);
d.input("o","n",3);
d.input("n","m",3);
d.input("m","q",2);
d.input("q","p",8);
d.input("p","r",6);
d.input("r","s",5);
System.out.print("살 품목:");
String list = sc.nextLine();
String []shoplist=list.split("");
d.rearray(shoplist);
}
}
class Dijkstra {
Scanner sc = new Scanner(System.in);
private int n;
private int[][] weight;
private String[] saveRoute;
private String[] vertex = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t"};
public Dijkstra(int n) {
super();
this.n = n;
weight = new int[n][n];
saveRoute = new String[n];
}
public int stringToInt(String s) {
int x = 0;
for(int i=0; i<vertex.length; i++) {
if(vertex[i]==s) x=i;
}
return x;
}
public void rearray(String[] r) {
int []array = new int[r.length];
for(int i=0;i<r.length;i++) {
for(int j=0;j<vertex.length;j++) {
if(r[i].equals(vertex[j])) {
array[i]=j;
}
}
}
Arrays.sort(array);
System.out.print("출발 지점:");
String start = sc.next();
if(start.equals("i")) {
algorithm("i", vertex[array[0]]);
for(int i=0;i<array.length-1;i++) {
algorithm(vertex[array[i]], vertex[array[i+1]]);
}
}
else if(start.equals("t")) {
algorithm("t", vertex[array[0]]);
for(int i=0;i<array.length-1;i++) {
algorithm(vertex[array[i]], vertex[array[i+1]]);
}
}
}
public void input(String v1, String v2, int w) {
int x1 = stringToInt(v1);
int x2 = stringToInt(v2);
weight[x1][x2] = w;
weight[x2][x1] = w;
}
public void algorithm(String a, String k) {
boolean[] visited = new boolean[n];
int distance[] = new int[n];
for(int i=0; i<n; i++) {
distance[i] = Integer.MAX_VALUE;
}
int x = 0;
for(int i=0; i<vertex.length; i++) {
if(a.equals(vertex[i]))
x = i;
}
distance[x] = 0;
visited[x] = true;
saveRoute[x] = vertex[x];
int y = stringToInt(k);
for(int i=0; i<n; i++) {
if(!visited[i] && weight[x][i]!=0) {
distance[i] = weight[x][i];
saveRoute[i] = vertex[x];
}
}
for(int i=0; i<n-1; i++) {
int minDistance = Integer.MAX_VALUE;
int minVertex = -1;
for(int j=0; j<n; j++) {
if(!visited[j] && distance[j]!=Integer.MAX_VALUE) {
if(distance[j]<minDistance) {
minDistance = distance[j];
minVertex = j;
}
}
}
visited[minVertex] = true;
for(int j=0; j<n; j++) {
if(!visited[j] && weight[minVertex][j]!=0) {
if(distance[j]>distance[minVertex]+weight[minVertex][j]) {
distance[j] = distance[minVertex]+weight[minVertex][j];
saveRoute[j] = vertex[minVertex];
}
}
}
}
for(int i=0;i<n;i++) {
if(k.equals(vertex[i])==true) {
System.out.println("시작 꼭지점 "+a+"부터 꼭지점 "+k+"까지의 거리 :"+ distance[i]);
}
}
/*
* for(int j=0;j<3;j++) { list[j]=distance[i];
* System.out.println(j+" "+list[i]); }
*/
/*for(int j=0;j<1;j++) {
for(int i=0;i<n;i++) {
if(k.equals(vertex[i])==true) {
list[j]=distance[i];
//System.out.print(list[j]+" ");
Arrays.sort(list);
for(int num: list) {
System.out.print(Arrays.toString(visited));
}
//compare(list[j]);
//compare(a,k,distance[i]);
//System.out.println("시작 꼭지점 "+a+"부터 꼭지점 "+k+"까지의 거리 :"+ distance[i]);
}
}
}*/
for(int i=0; i<n; i++) {
if(k.equals(vertex[i])==true) {
String route = "";
System.out.println("시작 꼭지점 "+a+"부터 꼭지점 "+k+"까지의 경로");
int index = i;
while(true) {
if(saveRoute[index] == vertex[index]) break;
route += " " + saveRoute[index];
index = stringToInt(saveRoute[index]);
}
StringBuilder sb = new StringBuilder(route);
System.out.print(sb.reverse() + vertex[i]);
System.out.println("\n");
}
}
}
}