<p>You have an undirected, connected graph of <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given an array <code>graph</code> where <code>graph[i]</code> is a list of all the nodes connected with node <code>i</code> by an edge.</p>

<p>Return <em>the length of the shortest path that visits every node</em>. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/12/shortest1-graph.jpg" style="width: 222px; height: 183px;" />
<pre>
<strong>Input:</strong> graph = [[1,2,3],[0],[0],[0]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible path is [1,0,2,0,3]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/12/shortest2-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible path is [0,1,4,2,3]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == graph.length</code></li>
	<li><code>1 &lt;= n &lt;= 12</code></li>
	<li><code>0 &lt;= graph[i].length &lt;&nbsp;n</code></li>
	<li><code>graph[i]</code> does not contain <code>i</code>.</li>
	<li>If <code>graph[a]</code> contains <code>b</code>, then <code>graph[b]</code> contains <code>a</code>.</li>
	<li>The input graph is always connected.</li>
</ul>

using graph = std::vector<std::vector<int>>;
using vi = std::vector<bool>;
using dist = std::vector<int>;
using memo = std::unordered_map<int, std::unordered_map<int, int>>;
using sp = std::vector<std::vector<int>>;

class Solution {
private:
    const int inf = 5 * 1e+05;
    int V;
    sp adjMatrix;
    int calculateMinPath(const graph& g, int source, int mask, memo& mt)
    {
        if(mask == ((1 << V) - 1))
          return 0;

        if(auto it_f = mt.find(source); it_f != mt.end())
          if(auto it_s = (*it_f).second.find(mask); 
            it_s != (*it_f).second.end())
            return (*it_s).second;

        int path = INT32_MAX;
        for(int v = 0; v < V; v++)
        {
            if((mask & (1 << v)))
              continue;
            int res = adjMatrix[source][v];
            path = std::min(path, res + calculateMinPath(g, v, (mask | (1 << v)), mt));
        }
        return mt[source][mask] = path;
    }
public:
    int shortestPathLength(vector<vector<int>>& g) 
    {
        V = g.size();
        int ans = INT32_MAX;
        
        adjMatrix = sp(V, std::vector<int>(V, inf));

        for(int u = 0; u < V; u++)
          for(auto v : g[u])
            adjMatrix[u][v] = 1;

        // Floyd Warshall’s algorithm
        for(int k = 0; k < V; k++)
          for(int i = 0; i < V; i++)
            for(int j = 0; j < V; j++)
              adjMatrix[i][j] = std::min(adjMatrix[i][j], adjMatrix[i][k] + adjMatrix[k][j]);

        memo mt;
        for(int v = 0; v < V; v++)
          ans = std::min(ans, calculateMinPath(g, v, (0 | (1 << v)), mt));
        return ans;   
    }
};
31853