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 | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 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.