Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit98e9ff8416cb99814f200866dc8eb44040b01e5b (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-05-22 06:54:16 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-05-22 06:54:16 +0300
Files317932e9feaf6085628798cb59bc37df70e7d055
uni-projects-98e9ff8416cb99814f200866dc8eb44040b01e5b.tar
uni-projects-98e9ff8416cb99814f200866dc8eb44040b01e5b.tar.gz
uni-projects-98e9ff8416cb99814f200866dc8eb44040b01e5b.zip
Parent11758bb746a3515562faf949dc6e105dce27dac2 (diff)
[DAAP/H2] Added initial solution to homework
That times out
1 files changed, 47 insertions, 0 deletions
-rw-r--r--Year 2/Design and analysis of algorithms/Practicum/Homeworks/HW2-Zigzag.cpp47
diff --git a/Year 2/Design and analysis of algorithms/Practicum/Homeworks/HW2-Zigzag.cpp b/Year 2/Design and analysis of algorithms/Practicum/Homeworks/HW2-Zigzag.cpp
new file mode 100644
index 0000000..e5b9191
--- /dev/null
+++ b/Year 2/Design and analysis of algorithms/Practicum/Homeworks/HW2-Zigzag.cpp
@@ -0,0 +1,47 @@
+#include <functional>
+#include <iostream>
+#include <vector>
+#include <queue>
+using namespace std;
+
+long long N, M;
+typedef pair<long long, long long> Edge;
+
+void dfs(Edge& c, long long length, vector<vector<Edge>>& graph, bool lt, vector<bool>& visited, long long& shortest) {
+ if (c.second == N-1) {
+ shortest = min(shortest, length);
+ return;
+ }
+
+ // cout << "From: " << c.second << " " << length << endl;
+ visited[c.second] = true;
+ for (Edge& e : graph[c.second]) {
+ if (visited[e.second]) continue;
+ if ((lt && c.second < e.second) || (!lt && c.second > e.second)) {
+ dfs(e, length + e.first, graph, !lt, visited, shortest);
+ }
+ }
+ visited[c.second] = false;
+}
+
+int main() {
+ ios::sync_with_stdio(false);
+ cin.tie(NULL);
+ cout.tie(NULL);
+
+ cin >> N >> M;
+ vector<vector<pair<long long, long long>>> graph(N, vector<pair<long long, long long>>());
+ for (long long i = 0; i < M; i++) {
+ long long ai, bi, di;
+ cin >> ai >> bi >> di;
+ ai--; bi--;
+ graph[ai].push_back({ di, bi });
+ graph[bi].push_back({ di, ai });
+ }
+
+ long long shortest = 1000000005;
+ vector<bool> visited(N);
+ Edge s = {0, 0};
+ dfs(s, 0, graph, true, visited, shortest);
+ cout << shortest << endl;
+}