-
Notifications
You must be signed in to change notification settings - Fork 2
/
zad4_jk.cpp
66 lines (59 loc) · 1.91 KB
/
zad4_jk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
O(n! * n) solution with backtracking (without any optimisations)
Author: Jan Kwieciński
Possible optimisation: looking at the bitmask of the already constructed path
(For example paths 0-2-1 and 0-1-2 can be continued in the same way, which is counted twice in the solution below)
*/
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
Solution(vector<pair<int, int>> _r, vector<vector<int>> _w) : n(_w.size()), rest_inp(_r), weights(_w)
{
restrictions.resize(n, {});
for (auto p : rest_inp) restrictions[p.second].push_back(p.first);
for (int i = 0; i < n; i++) if (i != n-1) restrictions[n-1].push_back(i);
}
//solve can be run many times on the same input
int solve()
{
answer = 0;
visited.resize(n, false); visited[0] = true;
recurrent_solve(0, 0, 1);
return answer;
}
private:
int n;
vector<pair<int, int>> rest_inp; //restrictions input
vector <vector<int>> restrictions; //parsed restrictions
vector<vector<int>> weights;
vector<bool> visited;
int answer;
void recurrent_solve(int curr, int len, int node_len)
{
if (restrictions_breach(curr)) return;
if (node_len == n) {answer = max(answer, len); return;}
for (int i = 0; i < n; i++)
{
if (!visited[i])
{
visited[i] = true;
recurrent_solve(i, len + weights[curr][i], node_len+1);
visited[i] = false;
}
}
}
bool restrictions_breach(int x)
{
for (auto y : restrictions[x]) if (!visited[y]) return true;
return false;
}
};
int main()
{
vector <vector<int>> W = {{0, 2, 1, 1}, {2, 0, 400, 2}, {100, 1, 0, 1}, {100, 400, 1, 0}}; //(doesn't have to be symmetrical)
vector <pair<int, int>> R = {make_pair(2, 1)};
Solution rozw = Solution(R, W);
assert(rozw.solve() == 4);
}