From 79458001168003e57229fd30d9db385ec07052ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BE=D1=80=D0=BE=D0=B7=D0=BE=D0=B2=20=D0=90=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=B5=D0=B9?= Date: Thu, 11 Nov 2021 08:09:49 +0000 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82?= =?UTF-8?q?=D0=BC=20=D0=94=D0=B5=D0=B9=D0=BA=D1=81=D1=82=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/bin/src/graph.dart | 57 +++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/cmd/bin/src/graph.dart b/cmd/bin/src/graph.dart index cd8707b..9dfe2d4 100644 --- a/cmd/bin/src/graph.dart +++ b/cmd/bin/src/graph.dart @@ -89,6 +89,7 @@ class Dot { } class Graphs { + static const int intMax = 0x7fffffffffffffff; //Data String _name = "Undefined"; //Имя int _amount = 0; //Количество вершин @@ -151,6 +152,9 @@ class Graphs { if (from <= 0 || from > _amount || to <= 0 && to > _amount) { return "Can't find specified path"; } + if (!_dots[from - 1].hasConnection(to)) { + return "Already no connection between $from and $to"; + } _dots[from - 1].delPath(to); if (!_oriented) { _dots[to - 1].delPath(from); @@ -652,28 +656,41 @@ class Graphs { return label; } - void dijkstra(int source) { + List dijkstra(int from) { /* - create vertex set Q; + int n; + ... чтение n ... + //vector>> g(_amount); + */ + List d = List.filled(_amount, intMax); + List p = List.filled(_amount, -1); - for each vertex v in Graph{ - dist[v] ← INFINITY ; - prev[v] ← UNDEFINED ; - add v to Q;} - dist[source] ← 0; - - while Q is not empty{ - u ← vertex in Q with min dist[u] - - remove u from Q - - for each neighbor v of u still in Q{ - alt ← dist[u] + length(u, v); - if alt < dist[v]: { - dist[v] ← alt; - prev[v] ← u;} - }} - return dist[], prev[]*/ + d[from - 1] = 0; + List u = List.filled(_amount, false); + for (int i = 0; i < _amount; ++i) { + int v = -1; + for (int j = 0; j < _amount; ++j) { + // int t; + if (!u[j] && (v == -1 || d[j]! < d[v]!)) { + v = j; + } + } + if (d[v] == intMax) break; + u[v] = true; + for (int to in _dots[v].getL().keys) { + int len = _dots[v].getL()[to]!; + if (!_useLength && len == 0) len = 1; + if (d[v]! + len < d[to - 1]!) { + d[to - 1] = d[v]! + len; + p[to - 1] = v; + } + } + } + for (int i = 0; i < d.length; i++) { + // подумать как убрать эту часть + if (d[i] == intMax) d[i] = null; + } + return d; } //************Алгоритмы************ }