There's a pizza shop that's going to send n pizzas to a different place.
Now give the distance between each location, each position can be repeated, asked to send all the pizza back to the store to walk the shortest distance is how much
Problem-solving ideas: This problem, there are two states, one is the current location, the other is the location has been passed, so the DP array is two-dimensional
DP[I][J] is the shortest route through which the state of the city is J, which is the current location
So Dp[i][state | (1 << i)] = Min (dp[i][state | (1 << i)], dp[j][state] + g[j][i])
。。。 I actually used the queue to update DP, is also drunk
#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#define N#define MAXN 1050#define INF 0x3f3f3f3fusing namespace STD;structdp{intnum, State;} Startintn, G[n][n];intDP[N][MAXN];voidInit () {intT for(inti =0; I < n; i++) for(intj =0; J < N; J + +) {scanf("%d", &t); G[I][J] = t; }memset(DP,0x3f,sizeof(DP)); Start.num =0; Start.state =1; dp[0][1] =0;}intSolve () { queue<DP>Q Q.push (start); while(!q.empty ()) {DP T = Q.front (); Q.pop (); for(inti =0; I < n; i++) {if(i = = T.num)Continue;if(Dp[i][t.state | (1<< i)] > Dp[t.num][t.state] + g[t.num][i]) {dp[i][t.state | (1<< i)] = Dp[t.num][t.state] + g[t.num][i]; DP tt; Tt.num = i; Tt.state = (t.state) | (1<< i); Q.push (TT); } } }returndp[0][(1<< N)-1];}intMain () { while(scanf("%d", &n)! = EOF && n) {n++; Init ();printf("%d\n", solve ()); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ-3311 Hie with the Pie (state compression)