Skip to content

Commit dce5333

Browse files
committed
solved(python): baekjoon 1865
1 parent 94ae285 commit dce5333

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

baekjoon/python/1865/__init__.py

Whitespace-only changes.

baekjoon/python/1865/main.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import math
2+
import sys
3+
4+
read = lambda: sys.stdin.readline().rstrip()
5+
6+
7+
class Problem:
8+
def __init__(self):
9+
self.n, self.m, self.w = map(int, read().split())
10+
self.graph, self.visited = [], set()
11+
12+
for _ in range(self.m):
13+
src, dest, time = map(int, read().split())
14+
self.graph.append((src - 1, dest - 1, time))
15+
self.graph.append((dest - 1, src - 1, time))
16+
17+
for _ in range(self.w):
18+
src, dest, time = map(int, read().split())
19+
self.graph.append((src - 1, dest - 1, -time))
20+
21+
def solve(self) -> None:
22+
for node in range(self.n):
23+
if node not in self.visited and self.bellman_ford(node):
24+
print("YES")
25+
return
26+
27+
print("NO")
28+
29+
def bellman_ford(self, node: int) -> bool:
30+
distances = [0 if node == idx else math.inf for idx in range(self.n)]
31+
32+
for idx in range(self.n):
33+
updated = False
34+
for src, dest, time in self.graph:
35+
if distances[src] != math.inf and distances[dest] > distances[src] + time:
36+
distances[dest] = distances[src] + time
37+
self.visited |= {src, dest}
38+
updated = True
39+
40+
if idx == self.n - 1:
41+
return True
42+
43+
if not updated:
44+
return False
45+
46+
return False
47+
48+
49+
if __name__ == "__main__":
50+
for _ in range(int(read())):
51+
Problem().solve()

baekjoon/python/1865/sample.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"input": [
4+
"2",
5+
"3 3 1",
6+
"1 2 2",
7+
"1 3 4",
8+
"2 3 1",
9+
"3 1 3",
10+
"3 2 1",
11+
"1 2 3",
12+
"2 3 4",
13+
"3 1 8"
14+
],
15+
"expected": [
16+
"NO",
17+
"YES"
18+
]
19+
}
20+
]

baekjoon/python/1865/test_main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import json
2+
import os.path
3+
import sys
4+
import unittest
5+
from io import StringIO
6+
from unittest.mock import patch
7+
8+
from parameterized import parameterized
9+
10+
from main import Problem
11+
12+
13+
def load_sample(filename: str):
14+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
15+
16+
with open(path, "r") as file:
17+
return [(case["input"], case["expected"]) for case in json.load(file)]
18+
19+
20+
class TestCase(unittest.TestCase):
21+
@parameterized.expand(load_sample("sample.json"))
22+
def test_case(self, case: str, expected: list[str]):
23+
# When
24+
with (
25+
patch("sys.stdin.readline", side_effect=case),
26+
patch("sys.stdout", new_callable=StringIO) as output,
27+
):
28+
for _ in range(int(sys.stdin.readline().rstrip())):
29+
Problem().solve()
30+
31+
result = output.getvalue().rstrip()
32+
33+
# Then
34+
self.assertEqual("\n".join(expected), result)
35+
36+
37+
if __name__ == "__main__":
38+
unittest.main()

0 commit comments

Comments
 (0)