FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-for-coding-test/9/4.java at master · ndb796/python-for-coding-test · GitHub
ndb796
/
python-for-coding-test
Public
Notifications
You must be signed in to change notification settings
Fork
831
Star
2.4k
Code
Issues
129
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-for-coding-test
/
9
/
4.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (51 loc) · 1.94 KB
Breadcrumbs
python-for-coding-test
/
9
/
4.java
Copy path
File metadata and controls
62 lines (51 loc) · 1.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
util
.*;
public
class
Main
{
public
static
final
int
INF
= (
int
)
1e9
;
// 무한을 의미하는 값으로 10억을 설정
// 노드의 개수(N), 간선의 개수(M), 거쳐 갈 노드(X), 최종 목적지 노드(K)
public
static
int
n
,
m
,
x
,
k
;
// 2차원 배열(그래프 표현)를 만들기
public
static
int
[][]
graph
=
new
int
[
101
][
101
];
public
static
void
main
(
String
[]
args
) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
n
=
sc
.
nextInt
();
m
=
sc
.
nextInt
();
// 최단 거리 테이블을 모두 무한으로 초기화
for
(
int
i
=
0
;
i
<
101
;
i
++) {
Arrays
.
fill
(
graph
[
i
],
INF
);
}
// 자기 자신에서 자기 자신으로 가는 비용은 0으로 초기화
for
(
int
a
=
1
;
a
<=
n
;
a
++) {
for
(
int
b
=
1
;
b
<=
n
;
b
++) {
if
(
a
==
b
)
graph
[
a
][
b
] =
0
;
}
}
// 각 간선에 대한 정보를 입력 받아, 그 값으로 초기화
for
(
int
i
=
0
;
i
<
m
;
i
++) {
// A와 B가 서로에게 가는 비용은 1이라고 설정
int
a
=
sc
.
nextInt
();
int
b
=
sc
.
nextInt
();
graph
[
a
][
b
] =
1
;
graph
[
b
][
a
] =
1
;
}
x
=
sc
.
nextInt
();
k
=
sc
.
nextInt
();
// 점화식에 따라 플로이드 워셜 알고리즘을 수행
for
(
int
k
=
1
;
k
<=
n
;
k
++) {
for
(
int
a
=
1
;
a
<=
n
;
a
++) {
for
(
int
b
=
1
;
b
<=
n
;
b
++) {
graph
[
a
][
b
] =
Math
.
min
(
graph
[
a
][
b
],
graph
[
a
][
k
] +
graph
[
k
][
b
]);
}
}
}
// 수행된 결과를 출력
int
distance
=
graph
[
1
][
k
] +
graph
[
k
][
x
];
// 도달할 수 없는 경우, -1을 출력
if
(
distance
>=
INF
) {
System
.
out
.
println
(-
1
);
}
// 도달할 수 있다면, 최단 거리를 출력
else
{
System
.
out
.
println
(
distance
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL