poj 2686 Traveling by Stagecoach【状态压缩】
2015-08-24 21:03
309 查看
题目链接:http://poj.org/problem?id=2686
题意:
一个人从某个城市要到另一个城市。然后有n个马车票,相邻的两个城市走的话要消耗掉一个马车票。花费的时间是马车票上有个速率值,用边/速率就是花的时间。
问最后这个人花费的最短时间是多少
dp[i][j] 表示集合 i 车票 到 j 的最优解
代码:
题意:
一个人从某个城市要到另一个城市。然后有n个马车票,相邻的两个城市走的话要消耗掉一个马车票。花费的时间是马车票上有个速率值,用边/速率就是花的时间。
问最后这个人花费的最短时间是多少
dp[i][j] 表示集合 i 车票 到 j 的最优解
代码:
[code]#include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional> #include <iterator> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <list> #include <bitset> #include <sstream> #include <iomanip> #include <fstream> #include <iostream> #include <ctime> #include <cmath> #include <cstring> #include <cstdio> #include <time.h> #include <ctype.h> #include <string.h> #include <assert.h> using namespace std; const int inf = 1e9; int n, m, p, a, b; int t[100]; int g[50][50]; double dp[(1<<15)][50]; int main() { while (~scanf("%d%d%d%d%d", &n, &m, &p, &a, &b)) { if (!n && !m && !p && !a && !b) break; for (int i = 0;i < n;i++) scanf("%d",&t[i]); for (int i = 1;i <= m;i++) for (int j = 1;j <= m;j++) { if (i == j) g[i][j] = 0; else g[i][j] = inf; } int u, v, w; while (p--) { scanf("%d%d%d",&u,&v,&w); g[u][v] = min(g[u][v], w); g[v][u] = min(g[v][u], w); } for (int i = 0;i < (1 << n);i++) for (int j = 0;j <= m;j++) dp[i][j] = inf; dp[0][a] = 0; double ans = inf; for (int i = 0;i < (1 << n);i++) { for (int u = 1;u <= m;u++) for (int j = 0;j < n;j++) if (!(i & (1 << j))) { for (int v = 1;v <= m;v++) { if (g[u][v] != inf) dp[i | (1 << j)][v] = min(dp[i | (1 << j)][v], dp[i][u] + (double)g[u][v] / t[j]); } } ans = min(ans,dp[i][b]); } if (ans == inf) puts("Impossible"); else printf("%.3lf\n",ans); } return 0; }
相关文章推荐
- Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call
- 四大天王之一BroadCast广播
- HDU 5289 Assignment (二分+区间最值)
- Java问题总结之1-2--超出数据类型取值范围
- 08-排序3. Talent and Virtue (25)
- hdu5015 233 Matrix(矩阵快速幂)
- 高手-大型高并发高负载网站的系统架构
- js 获取事件对象以及元素
- 不能将Lnode * 类型的值分配到Lnode * 类型的实体
- JavaScript的技巧和最佳实践
- 编写高质量代码--改善Java程序的151个建议
- 解决双系统下如何完全卸载Ubuntu系统
- Android的控件CheckBox、ImageView以及ListView的自定义Adapter用法
- 由Maximum Gap,对话桶排序,基数排序和统计排序
- 【cocos2d-x 3.7 飞机大战】 决战南海I (十) 游戏主场景
- HDOJ1274展开字符串
- 【九度OJ1373】|【剑指offer32】整数中1出现的次数(从1到n整数中1出现的次数)
- CheckBox,ImageView,ListView
- UITextField 弹出键盘的外观(类型)设置
- Java专题之IO