POJ 2686 Traveling by Stagecoach 状压DP
Posted iat14
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2686 Traveling by Stagecoach 状压DP相关的知识,希望对你有一定的参考价值。
pass
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 typedef long long ll; 6 const double inf = 10000000000.0; 7 const int MAXM = 40,MAXN = 15; 8 int n,m,p,a,b,t[MAXN]; 9 int d[MAXM][MAXM]; 10 double dp[1 << MAXN][MAXM]; 11 double dfs(int sta,int u) 12 { 13 if (dp[sta][u] >= 0.0) 14 return dp[sta][u]; 15 dp[sta][u] = inf; 16 for (int v = 1;v <= m;v++) 17 for (int i = 1;i <= n;i++) 18 if (((sta >> (i - 1)) & 1) == 0) 19 if(d[v][u] >= 0) 20 dp[sta][u] = min(dp[sta][u],dfs(sta | (1 << (i - 1)),v) + (double)d[v][u] / t[i]); 21 return dp[sta][u]; 22 } 23 int main() 24 { 25 for(;;) 26 { 27 scanf("%d%d%d%d%d",&n,&m,&p,&a,&b); 28 if (n == 0) 29 break; 30 for (int i = 1;i <= n;i++) 31 scanf("%d",&t[i]); 32 memset(d,-1,sizeof(d)); 33 int tx,ty,tz; 34 for (int i = 1;i <= p;i++) 35 { 36 scanf("%d%d%d",&tx,&ty,&tz); 37 d[tx][ty] = tz; 38 d[ty][tx] = tz; 39 } 40 for (int i = 0;i < 1 << n;i++) 41 for (int j = 1;j <= m;j++) 42 dp[i][j] = -1.0; 43 double res = inf; 44 dp[(1 << n) - 1][a] = 0.0; 45 for (int i = 0;i < (1 << n);i++) 46 res = min(res,dfs(i,b)); 47 if (res == inf) 48 printf("Impossible "); 49 else 50 printf("%.3f ",res); 51 } 52 53 return 0; 54 }
以上是关于POJ 2686 Traveling by Stagecoach 状压DP的主要内容,如果未能解决你的问题,请参考以下文章
poj 2686 Traveling by Stagecoach
POJ 2686 Traveling by Stagecoach(状压二维SPFA)
POJ 2686 Traveling by Stagecoach 状压DP
POJ 2686 Traveling by Stagecoach(状压DP)