-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsp.h
More file actions
153 lines (126 loc) · 3.85 KB
/
tsp.h
File metadata and controls
153 lines (126 loc) · 3.85 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Created by syeyoung on 5/8/25.
//
#ifndef DPTSP_TSP_H
#define DPTSP_TSP_H
#include <vector>
#include <functional>
#include <map>
#include <coroutine>
#include <array>
using namespace std;
typedef long long ll;
struct NodeMapping {
int N;
vector<int> nodeType;
vector<int> requireBitIndex;
vector<int> orSubIndex;
vector<bool> sanity;
vector<ll> orMult;
vector<int> orCount;
vector<ll> require;
vector<vector<int>> orReq;
};
struct VisitedSet {
ll requireBitSet;
ll orBitSet;
void add(NodeMapping& map, int id) {
if (map.nodeType[id] == 0) return;
if (map.nodeType[id] == 1) {
requireBitSet |= (1ll << map.requireBitIndex[id]);
return;
}
int orId = map.nodeType[id] - 2;
int targetIndex = map.orSubIndex[id];
ll curr = (orBitSet / map.orMult[orId]) % map.orCount[orId];
orBitSet += (targetIndex-curr) * map.orMult[orId];
}
void remove(NodeMapping& map, int id) {
if (map.nodeType[id] == 0) return;
if (map.nodeType[id] == 1) {
requireBitSet ^= (1ll << map.requireBitIndex[id]);
return;
}
int orId = map.nodeType[id] - 2;
int targetIndex = 0;
ll curr = (orBitSet / map.orMult[orId]) % map.orCount[orId];
orBitSet += (targetIndex-curr) * map.orMult[orId];
}
bool contains(NodeMapping& map, int id) const {
if (map.nodeType[id] == 0) return false;
if (map.nodeType[id] == 1) return (requireBitSet & (1ll << map.requireBitIndex[id]));
int orId = map.nodeType[id] - 2;
ll curr = (orBitSet / map.orMult[orId]) % map.orCount[orId];
return curr == map.orSubIndex[id];
}
bool canBeAdded(NodeMapping& map, int id) const{
if (map.nodeType[id] == 0) return false;
if (map.nodeType[id] == 1) return !(requireBitSet & (1ll << map.requireBitIndex[id]));
int orId = map.nodeType[id] - 2;
ll curr = (orBitSet / map.orMult[orId]) % map.orCount[orId];
return curr == 0;
}
bool operator <(const VisitedSet& y) const {
return std::tie(requireBitSet, orBitSet) < std::tie(y.requireBitSet, y.orBitSet);
}
bool operator ==(const VisitedSet& y) const {
return requireBitSet == y.requireBitSet && orBitSet == y.orBitSet;
}
};
typedef pair<VisitedSet, int> Key;
typedef array<double, 3> Vec3;
struct RoomStatus {
Vec3 pos;
int mechanicSet;
};
struct Value {
double cost = -1;
Key prev;
RoomStatus curr;
};
struct AwaitJava {
double result;
Vec3 coord;
int mechId;
int nodeId;
std::coroutine_handle<> handle;
bool await_ready() const noexcept {return false;}
void await_suspend(std::coroutine_handle<> h) {handle = h;}
pair<RoomStatus, double> await_resume() {return {RoomStatus{coord, mechId}, result}; }
};
struct TSPRoutine {
struct promise_type {
TSPRoutine get_return_object() {
return TSPRoutine{std::coroutine_handle<promise_type>::from_promise(*this)};
}
bool await_ready() const noexcept {return false;}
std::suspend_never initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() { }
void unhandled_exception() {}
};
std::coroutine_handle<promise_type> handle;
};
class TSP {
private:
NodeMapping mapping;
map<Key, Value> cost;
Vec3 start;
int stBitSet;
bool canVisit(Key key, int node);
TSPRoutine solveTSP();
public:
AwaitJava awaiter;
TSPRoutine routine;
TSP(NodeMapping mapping, Vec3 start, int stBitset) {
this->mapping = mapping;
this->start = start;
this->stBitSet = stBitset;
routine = solveTSP();
}
~TSP() {
routine.handle.destroy();
}
vector<int> getSolution(int goal);
};
#endif //DPTSP_TSP_H