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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
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.