Title: poj 3311 HIE with the pie
The question is: the batch worker will send the batch, and then give you the distance and direction of each point, and then let you go back to the source of the shortest.
Analysis: Because the dense graph is provided, it is necessary to handle the shortest path, Floyd
Then, the TSP is good.
Enumerate each status. For each vertices in the current status that have passed, enumeration comes from that point, with the shortest path updated.
Status: DP [st] [I]: the shortest path to vertex I in the St state
Transition equation: DP [st] [I] = min (DP [st &~ (1 <I)] [J] + MP [J] [I], DP [st] [I]);
Then, add the back distance. Note that we need to update and. Here we wa it once.
AC code:
# Include <cstdio> # include <algorithm> # include <cstring> # include <string> # include <iostream> # include <vector> using namespace STD; const int INF = 0x3f3f3f3f; const int n = 12; int MP [N] [N]; int N; int DP [1 <n] [N]; int main () {// freopen ("input.txt", "r", stdin); While (~ Scanf ("% d", & N) {n ++; For (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) scanf ("% d", & MP [I] [J]);} for (int K = 0; k <n; k ++) {for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) MP [I] [J] = min (MP [I] [J], MP [I] [k] + MP [k] [J]) ;}}for (INT ST = 0; ST <(1 <n); ST ++) {for (INT I = 0; I <n; I ++) {If (st & (1 <I) = 0) // 0 continue; if (ST = (1 <I) {DP [st] [I] = MP [0] [I]; continue ;} DP [st] [I] = inf; For (Int J = 0; j <n; j ++) {If (st & (1 <j ))&& I! = J) // 1 {DP [st] [I] = min (DP [st &~ (1 <I)] [J] + MP [J] [I], DP [st] [I]) ;}}} int ans = inf; for (INT I = 0; I <n; I ++) {ans = min (ANS, DP [(1 <n) -1] [I] + MP [I] [0]);} printf ("% d \ n", ANS);} return 0 ;}
Poj 3311 HIE with the pie [traveling salesman + origin retrieval]