题目:https://leetcode.com/problems/shortest-path-visiting-all-nodes/description/
思路:用dp[st][i]存储在i节点时,遍历过点的状态
class Solution {
public:
const int INF = 12*12+5;
int d[12][12];
int dp[1<<12][12];
int shortestPathLength(vector<vector<int>>& graph) {
int n = graph.size();
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
if(i == j) d[i][j] = 0;
else d[i][j] = INF;
for(int i = 0;i < n;i++)
for(auto j : graph[i])
d[i][j] = d[j][i] = 1;//undirect
//floyd
for(int k = 0;k < n;k++)
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
for(int i = 0;i < (1<<n);i++)
for(int j = 0;j < n;j++)
if(i == (1<<j)) dp[i][j] = 0;
else dp[i][j] = INF;
for(int st = 1;st < (1<<n);st++){
for(int i = 0;i < n;i++){
if(st & (1<<i))
for(int j = 0;j < n;j++)
dp[st][i] = min(dp[st][i], dp[st^(1<<i)][j] + d[i][j]);
}
}
int minn = INF;
for(int i = 0;i < n;i++)
minn = min(minn, dp[(1<<n)-1][i]);
return minn;
}
};