diff --git a/dijkstra.cpp b/dijkstra.cpp index 3a1e872..32e5a1b 100644 --- a/dijkstra.cpp +++ b/dijkstra.cpp @@ -1,35 +1,35 @@ class Solution { public: - int diskstra(vector>& edges, int n, int start, int end) { - vector> graph(n, vector(n, INT_MAX / 2)); - // === 建图 === - for (auto& [from, to, dist]: edges) { - graph[from][to] = dist; - } + int diskstra(vector>& edges, int n, int start, int end) { + vector> graph(n, vector(n, INT_MAX / 2)); + for (auto& edge : edges) { + int from = edge[0], to = edge[1], dist = edge[2]; + graph[from][to] = dist; // build the graph + } - vector distance(n, INT_MAX/2), visited(n, 0); - distance[start] = 0; - while (true) { - int next_node = -1; - for (int node = 0; node < n; node++) { - if (visited[node]) { - continue; // 不重复更新 - } - if (next_node < 0 || distance[next_node] > distance[node]) { - next_node = node; // 寻找移动路径最小的节点 - } - } + vector distance(n, INT_MAX / 2), visited(n, 0); + distance[start] = 0; + while (true) { + int next_node = -1; + for (int node = 0; node < n; node++) { + if (visited[node]) { + continue; // update non-repeatedly + } + if (next_node < 0 || distance[next_node] > distance[node]) { + next_node = node; // find the node with the smallest moving path + } + } - if (next_node < 0 || distance[next_node] == INT_MAX / 2) { - break; // 找不到节点 - } + if (next_node < 0 || distance[next_node] == INT_MAX / 2) { + break; // cannot find the node + } - visited[next_node] = 1; - for (int node = 0; node < n; node++) { - // 根据最近节点路径更新其余节点移动路径 - distance[node] = min(distance[node], distance[next_node] + graph[next_node][node]); - } - } - return distance[end] == INT_MAX / 2 ? -1 : distance[end]; - } + visited[next_node] = 1; + for (int node = 0; node < n; node++) { + // update the moving paths of the remaining nodes based on the nearest node + distance[node] = min(distance[node], distance[next_node] + graph[next_node][node]); + } + } + return distance[end] == INT_MAX / 2 ? -1 : distance[end]; + } }; \ No newline at end of file diff --git a/trie_tree.cpp b/trie-tree.cpp similarity index 100% rename from trie_tree.cpp rename to trie-tree.cpp diff --git a/union-find-sets.cpp b/union-find-sets.cpp new file mode 100644 index 0000000..34d48a6 --- /dev/null +++ b/union-find-sets.cpp @@ -0,0 +1,36 @@ +class UnionFind { + vector _pre; // 代表元 + vector _rank; // 集合的秩 + +public: + UnionFind(int n) : _pre(n), _rank(n, 1) { + iota(_pre.begin(), _pre.end(), 0); + //ranges::iota(_pre, 0); + } + + int find(int x) { + return _pre[x] == x ? x : _pre[x] = find(_pre[x]); + } + + bool same(int x, int y) { + return find(x) == find(y); + } + + bool merge(int from, int to) { + int x = find(from), y = find(to); + if (x == y) { + return false; + } + if (_rank[x] < _rank[y]) { + _pre[x] = y; + } + else if (_rank[x] > _rank[y]) { + _pre[y] = x; + } + else { + _pre[x] = y; + _rank[y]++; + } + return true; + } +}; \ No newline at end of file diff --git a/union-find_sets.cpp b/union-find_sets.cpp deleted file mode 100644 index c578c7d..0000000 --- a/union-find_sets.cpp +++ /dev/null @@ -1,36 +0,0 @@ -class UnionFind { - vector _pre; // 代表元 - vector _rank; // 集合的秩 - -public: - UnionFind(int n) : _pre(n), _rank(n, 1), cc(n) { - // iota(fa.begin(), fa.end(), 0); - ranges::iota(fa, 0); - } - - int find(int x) { - return _pre[x] == x ? x : _pre[x] = fint(_pre[x]); - } - - bool same(int x, int y) { - return find(x) == find(y); - } - - bool merge(int from, int to) { - int x = find(from), y = find(to); - if (x == y) { - return false; //同一集合不做合并 - } - if (_rank[x] < _rank[y]) { - _pre[x] = y; - } - else if (_rank[x] > _rank[y]) { - _pre[y] = x; - } - else { - _pre[x] = y; - _rank[y]++; - } - return true; - } -};