You are given N points (N <= 20). Distance between 2 points is abs(x1-x2)+abs(y1-y2)
. You should find the shortest way which will visit all points. You can start from any point.
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
You are given N points (N <= 20). Distance between 2 points is abs(x1-x2)+abs(y1-y2)
. You should find the shortest way which will visit all points. You can start from any point.
Name |
---|
Can you give me link?
It's like the Traveling Salesman problem, but since N ≤ 20, you can do DP with bitmask with complexity O(2N * N).
Seems like actually it will be N*(2^N). =)
Yes, you're correct. I forgot to consider that.
I don't know bitmasks. Where can I read about it? And also if it isn't hard can some of you write code or pseudocode for it?
This tutorial talks about DP with bitmasks.
And here's my code for the problem: C++ Code
Well if O(2^N * N^2) is fast enough, then you can just create a full graph with the distances and solve the Travelling Salesman problem similarly to Hamiltonian Path problem, using bitmasks in states.
Why O(2N * N2) ???
There are 2N states, and you perform N checks in each one of them (which point to go next), so total complexity would be O(2N * N).
EDIT: Actually, you're right. There are 2N * N states, so complexity is O(2N * N2).
Enchom is right.
There're actually N * (2^N) states.
DP is f[mask][lastVertex].
So it's really N^2 * (2^N). :)
Yes, that was pretty stupid of me... sorry.