Hdu3001 -- Travelling three-digit TSP, status Compression
TravellingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4106 Accepted Submission (s): 1310
Problem DescriptionAfter coding so many days, Mr Acmer wants to have a good rest. So traveling is the best choice! He has decided to visit n cities (he insists on seeing all the cities! And he does not mind which city being his start station because superman can bring him to any city at first but only once .), and of course there are m roads here, following a stored as usual. but Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice! And he is so mean that he wants to minimize the total amount! He is lazy you see. So he turns to you for help.
InputThere are several test cases, the first line is two intergers n (1 <= n <= 10) and m, which means he needs to visit n cities and there are m roads he can choose, then m lines follow, each line will include three intergers a, B and c (1 <=, B <= n), means there is a road between a and B and the cost is of course c. input to the End Of File.
OutputOutput the minimum limit that he shoshould pay, or-1 if he can't find such a route.
Sample Input
2 11 2 1003 21 2 402 3 503 31 2 31 3 42 3 10
Sample Output
100907
Source2009 Multi-University Training Contest 11-Host by HRBEU
Recommendgaojie | We have carefully selected several similar problems for you: 3004 3002 3006 3007
This is the TSP, and then think of the pressure, but here each city can go up to two times, so do not use ordinary binary to pressure, need three hexadecimal
In triplicate, each digit is 0, 1, 2, and 0, indicating that the city has never been used. 1 indicates that the city has been used once, and 2 indicates that the city has been used twice.
Since there are 10 cities in total, we first pre-process 3 ^ 0 --- 3 ^ 10, and then store it in a three-digit system, followed by dp, but note that, it can be regarded as the final State. Each of its bits is not 0 (otherwise, a city has never been there ), finally, enumerate all the statuses that can be used as the final state, and then obtain the minimum value.
#include #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include using namespace std;const int inf = 0x3f3f3f3f;int dp[60000][15];int dist[15][15];int three[15] = {0, 1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049};int city[60000][15];int main(){int n, m, u, v, w;memset (city, inf, sizeof(city));for (int i = 0; i < 59059; ++i){int tmp = i;for (int j = 1; j <= 10; ++j){city[i][j] = tmp % 3;tmp /= 3;}}while (~scanf("%d%d", &n, &m)){memset (dist, inf, sizeof(dist));memset (dp, inf, sizeof(dp));for (int i = 1; i <= n; ++i){dp[three[i]][i] = 0;}dp[0][0] = 0;for (int i = 1; i <= m; ++i){scanf("%d%d%d", &u, &v, &w);dist[u][v] = min(dist[u][v], w);dist[v][u] = dist[u][v];}int ans = inf;for (int i = 0; i < three[n + 1]; ++i){bool is_end = 1;for (int j = 1; j <= n; ++j){if (city[i][j] == 0){is_end = 0;}for (int k = 1; k <= 10; ++k){if (city[i][j] == 2){continue;}dp[i + three[j]][j] = min(dp[i + three[j]][j], dp[i][k] + dist[k][j]);}}if (is_end){for (int j = 1; j <= n; ++j){ans = min(ans, dp[i][j]);}}}if (ans == inf){printf("-1\n");continue;}printf("%d\n", ans);}return 0;}