Skip to content

Instantly share code, notes, and snippets.

@shameemreza
Created June 5, 2018 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shameemreza/c2abd20513765250081ccae806ae6ab7 to your computer and use it in GitHub Desktop.
Save shameemreza/c2abd20513765250081ccae806ae6ab7 to your computer and use it in GitHub Desktop.
//Assuming that there are 20 friends
int dp[1<<20][20];
//mask=frinds I already visited
//at=last visited friend
int Dp(int mask, int at)
{
//I used reference. It helps me not
//writing dp[mask] [at] again and again.
int& ret = dp[mask] [at];
//Assume that we initilized dp with -1
if (ret !=-1) return ret;
//initialize ret with infinity
ret = 1000000000;
//n = number of friend
//dist contains distance between every two nodes
for (int i = 0; i<n; i++)
inf (!(mask & (1<<i)))
//mask | (1<<i) turns on i th bit in mask.
ret = MIN(re,
DP(mask | (1<<i), i) + dist[at][i]);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment