码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2686 Traveling by Stagecoach 状压DP

时间:2019-12-17 13:29:19      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:long   class   col   span   res   amp   poj   状压   else   

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\n");
49         else
50             printf("%.3f\n",res);
51     }
52 
53     return 0;
54 }

POJ 2686 Traveling by Stagecoach 状压DP

标签:long   class   col   span   res   amp   poj   状压   else   

原文地址:https://www.cnblogs.com/iat14/p/12053820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!