Given a weighted graph, it is required to start at vertex 0, after each node exactly once and then return 0, to seek the minimum value of the edge right and.
2<=n<=15
0<=d (I,J) <=1000
Sample Example
5 8
0 1 3
0 3 4
1 2 5
2 0 4
2 3 5
3 4 3
4 0 7
4 1 6
Dp[s][u]=min{dp[s∪{v}][v]+d (U,V) | V∉s&&d (u,v)!=inf},
d[(1<<n) -1][0]=0.
O (2^n*n^2)
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define N 16#define INF 214748364#define M 250int n,m;int v[m<<1],w[m<<1],first[n],next[m<<1],en;void AddEdge (int U,int V, int W) {v[++en]=v;w[en]=w;next[en]=first[u];first[u]=en;} int dp[(1<<n) +1][n];int f (int s,int U) {if (dp[s][u]!=-1) return dp[s][u];if (s== (1<<n) -1&& (! U)) return dp[s][u]=0;int res=inf;for (int i=first[u];i;i=next[i]) if (! ( s>>v[i]&1)) res=min (Res,f (s| ( 1<<v[i]), V[i]) +w[i]); return dp[s][u]=res;} int main () {int x,y,z;scanf ("%d%d", &n,&m), for (int i=1;i<=m;++i) { scanf ("%d%d%d", &x,&y, &Z); Addedge (x, y, z); } Memset (Dp,-1,sizeof (DP));p rintf ("%d\n", F (0,0)); return 0;}
"Pressure DP" travel business problem