From 6869818b4d7f25ec08cfd97ba9b5fade13e0afe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E6=99=A8?= Date: Wed, 1 Mar 2023 10:09:03 +0800 Subject: [PATCH] add exercises --- .gitignore | 3 +- .../exercises.md" | 39 +++++++++++++++++++ .../soulution.py" | 35 +++++++++++++++++ .../test_cases/1.in" | 7 ++++ .../test_cases/1.out" | 1 + .../test_cases/10.in" | 3 ++ .../test_cases/10.out" | 1 + .../test_cases/2.in" | 5 +++ .../test_cases/2.out" | 1 + .../test_cases/3.in" | 4 ++ .../test_cases/3.out" | 1 + .../test_cases/4.in" | 4 ++ .../test_cases/4.out" | 1 + .../test_cases/5.in" | 2 + .../test_cases/5.out" | 1 + .../test_cases/6.in" | 2 + .../test_cases/6.out" | 1 + .../test_cases/7.in" | 5 +++ .../test_cases/7.out" | 1 + .../test_cases/8.in" | 6 +++ .../test_cases/8.out" | 1 + .../test_cases/9.in" | 4 ++ .../test_cases/9.out" | 1 + 23 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/exercises.md" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/soulution.py" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/1.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/1.out" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/10.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/10.out" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/2.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/2.out" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/3.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/3.out" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/4.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/4.out" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/5.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/5.out" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/6.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/6.out" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/7.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/7.out" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/8.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/8.out" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/9.in" create mode 100644 "exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/9.out" diff --git a/.gitignore b/.gitignore index c6f70fa..6b8fd10 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,4 @@ release/ debug/ *.o *.obj -*.a -*.out \ No newline at end of file +*.a \ No newline at end of file diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/exercises.md" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/exercises.md" new file mode 100644 index 0000000..13c1ebd --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/exercises.md" @@ -0,0 +1,39 @@ +# 狄杰斯特拉 + +给定一张有向加权图,图中每条边的权重均为正整数。现在你需要从图中的一个起点出发,找到到达一个特定节点的最短路径。请编写一个程序,读入有向加权图和起点、终点,输出从起点到终点的最短距离。 + +## 输入描述 + +第一行包含三个整数n,m,s,分别表示节点数、边数和起点编号。节点编号从1到n。 + +接下来m行,每行包含三个整数u,v,w,表示u到v有一条有向边,边权为w。 + +1≤n≤1e5,1≤m≤2e5,1≤s≤n,1≤u,v≤n,1≤w≤1e9 + +## 输出描述 + +共一行,包含一个整数,表示从起点到终点的最短距离,如果不存在从起点到终点的路径,则输出INF。 + + +## 输入样例 + +6 9 1 +1 2 2 +1 3 1 +2 3 2 +2 4 3 +3 4 1 +3 5 3 +4 6 4 +5 4 2 +5 6 1 + +## 输出样例 + +8 + +## 提示 + +对于40%的数据,1≤n≤1e3,1≤m≤1e4,1≤w≤100。 + +对于100%的数据,有解保证存在且最大点数不超过1e5,边数不超过2e5。 \ No newline at end of file diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/soulution.py" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/soulution.py" new file mode 100644 index 0000000..07de546 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/soulution.py" @@ -0,0 +1,35 @@ +import heapq + +def dijkstra(graph, start, end): + # 距离数组,初始化为无穷大 + dist = {node: float('inf') for node in graph} + dist[start] = 0 + + # 优先队列,存储节点和到起点的距离 + pq = [(0, start)] + + while pq: + # 取出距离起点最近的节点 + distance, node = heapq.heappop(pq) + # 如果该节点已经被处理过,则跳过 + if distance > dist[node]: + continue + # 更新该节点的所有邻居节点的距离值 + for neighbor, weight in graph[node].items(): + new_dist = dist[node] + weight + if new_dist < dist[neighbor]: + dist[neighbor] = new_dist + heapq.heappush(pq, (new_dist, neighbor)) + + # 返回从起点到终点的最短距离 + return dist[end] if dist[end] < float('inf') else "INF" + +# 读入有向加权图和起点、终点 +n, m, s = map(int, input().split()) +graph = {i: {} for i in range(1, n+1)} +for _ in range(m): + u, v, w = map(int, input().split()) + graph[u][v] = w + +# 计算从起点到终点的最短距离 +print(dijkstra(graph, s, n)) diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/1.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/1.in" new file mode 100644 index 0000000..90f3de7 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/1.in" @@ -0,0 +1,7 @@ +5 6 1 +1 2 1 +1 3 4 +2 3 2 +2 4 5 +3 4 1 +4 5 1 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/1.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/1.out" new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/1.out" @@ -0,0 +1 @@ +5 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/10.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/10.in" new file mode 100644 index 0000000..e74e53d --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/10.in" @@ -0,0 +1,3 @@ +3 2 1 +1 2 1 +1 3 1 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/10.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/10.out" new file mode 100644 index 0000000..d00491f --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/10.out" @@ -0,0 +1 @@ +1 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/2.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/2.in" new file mode 100644 index 0000000..008ec87 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/2.in" @@ -0,0 +1,5 @@ +4 4 1 +1 2 1 +1 3 2 +2 4 2 +3 4 1 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/2.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/2.out" new file mode 100644 index 0000000..00750ed --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/2.out" @@ -0,0 +1 @@ +3 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/3.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/3.in" new file mode 100644 index 0000000..f938485 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/3.in" @@ -0,0 +1,4 @@ +3 3 1 +1 2 1 +1 3 2 +2 3 1 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/3.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/3.out" new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/3.out" @@ -0,0 +1 @@ +2 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/4.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/4.in" new file mode 100644 index 0000000..860d5fa --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/4.in" @@ -0,0 +1,4 @@ +3 3 1 +1 2 1 +2 3 2 +3 1 3 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/4.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/4.out" new file mode 100644 index 0000000..771404d --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/4.out" @@ -0,0 +1 @@ +INF diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/5.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/5.in" new file mode 100644 index 0000000..59c808d --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/5.in" @@ -0,0 +1,2 @@ +2 1 1 +1 2 1 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/5.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/5.out" new file mode 100644 index 0000000..d00491f --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/5.out" @@ -0,0 +1 @@ +1 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/6.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/6.in" new file mode 100644 index 0000000..4c9ba95 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/6.in" @@ -0,0 +1,2 @@ +2 1 2 +1 2 1 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/6.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/6.out" new file mode 100644 index 0000000..771404d --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/6.out" @@ -0,0 +1 @@ +INF diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/7.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/7.in" new file mode 100644 index 0000000..008ec87 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/7.in" @@ -0,0 +1,5 @@ +4 4 1 +1 2 1 +1 3 2 +2 4 2 +3 4 1 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/7.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/7.out" new file mode 100644 index 0000000..00750ed --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/7.out" @@ -0,0 +1 @@ +3 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/8.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/8.in" new file mode 100644 index 0000000..f97d073 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/8.in" @@ -0,0 +1,6 @@ +5 5 1 +1 2 2 +2 3 3 +3 4 4 +4 5 5 +1 5 6 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/8.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/8.out" new file mode 100644 index 0000000..1e8b314 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/8.out" @@ -0,0 +1 @@ +6 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/9.in" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/9.in" new file mode 100644 index 0000000..24b5169 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/9.in" @@ -0,0 +1,4 @@ +3 3 1 +1 2 1 +1 3 3 +2 3 2 diff --git "a/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/9.out" "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/9.out" new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ "b/exercises/zhangzc/\344\270\255\347\255\211/\347\213\204\346\235\260\346\226\257\347\211\271\346\213\211/test_cases/9.out" @@ -0,0 +1 @@ +2 -- GitLab