aboutsummaryrefslogtreecommitdiff
path: root/semestr-4/aisd/themis/BELLMAN.cpp
diff options
context:
space:
mode:
authorFranciszek Malinka <franciszek.malinka@gmail.com>2021-10-05 21:49:54 +0200
committerFranciszek Malinka <franciszek.malinka@gmail.com>2021-10-05 21:49:54 +0200
commitc5fcf7179a83ef65c86c6a4a390029149e518649 (patch)
treed29ffc5b86a0d257453cedcf87d91a13d8bf3b0d /semestr-4/aisd/themis/BELLMAN.cpp
parentf8a88b6a4aba1f66d04711a9330eaba49a50c463 (diff)
Duzy commit ze smieciami
Diffstat (limited to 'semestr-4/aisd/themis/BELLMAN.cpp')
-rw-r--r--semestr-4/aisd/themis/BELLMAN.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/semestr-4/aisd/themis/BELLMAN.cpp b/semestr-4/aisd/themis/BELLMAN.cpp
new file mode 100644
index 0000000..5a65096
--- /dev/null
+++ b/semestr-4/aisd/themis/BELLMAN.cpp
@@ -0,0 +1,55 @@
+#include <bits/stdc++.h>
+using namespace std;
+
+const int N = 503;
+int dist[N];
+vector<pair<int, pair<int, int>>> edges;
+int main()
+{
+ ios_base::sync_with_stdio(false);
+ cin.tie();
+ int n, m, s;
+ cin >> n >> m >> s;
+ for (int i = 0; i <= n; i++)
+ {
+ dist[i] = 2e9;
+ }
+ dist[s] = 0;
+ for (int i = 0; i < m; i++)
+ {
+ int u, v, w;
+ cin >> u >> v >> w;
+ edges.push_back({w, {u, v}});
+ // edges.push_back({w, {v, u}});
+ }
+
+ for (int i = 0; i < n + 1; i++)
+ {
+ for (auto e : edges)
+ {
+ int w = e.first;
+ int u = e.second.first;
+ int v = e.second.second;
+ if (dist[v] > dist[u] + w)
+ {
+ dist[v] = dist[u] + w;
+ }
+ }
+ }
+ for (auto e : edges)
+ {
+ int w = e.first;
+ int u = e.second.first;
+ int v = e.second.second;
+ if (dist[v] > dist[u] + w)
+ {
+ cout << "NIE\n";
+ return 0;
+ }
+ }
+ for (int i = 0; i < n; i++)
+ {
+ if (i != s && dist[i] < 1e9)
+ cout << i << " " << dist[i] << "\n";
+ }
+} \ No newline at end of file