题意:有一个人从某个城市要到另一个城市, 有n个马车票,相邻的两个城市走的话要消耗掉一个马车票。花费的时间呢,是马车票上有个速率值

,问最后这个人花费的最短时间是多少。

析:和TSP问题差不多,dp[s][i] 表示当前在第 i 个城市,还剩余集合 s的票,需要的最短时间。状态转移方程:

dp[s][i] = min{dp[s|j][k] + d[i][k] }

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 10;
const int mod = 100000000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
double dp[1<<8][31];
int a[31][31];
double tt[10]; int main(){
// freopenr;
int s, t, p;
while(scanf("%d %d %d %d %d", &n, &m, &p, &s, &t) == 5 && n+m+p+s+t){
int all = 1 << n;
for(int i = 0; i < all; ++i)
fill(dp[i], dp[i]+m+1, inf);
for(int i = 0; i < n; ++i) scanf("%lf", tt+i);
memset(a, INF, sizeof a);
for(int i = 0; i < p; ++i){
int u, v, d;
scanf("%d %d %d", &u, &v, &d);
a[u][v] = a[v][u] = d;
}
dp[all-1][s] = 0;
for(int i = all-2; i >= 0; --i)
for(int j = 0; j < n; ++j) if(!(i&(1<<j))){
for(int u = 1; u <= m; ++u){
for(int v = 1; v <= m; ++v){
if(a[u][v] == INF) continue;
if(dp[i|(1<<j)][v] == inf) continue;
dp[i][u] = min(dp[i][u], dp[i|(1<<j)][v] + a[u][v] * 1.0 / tt[j]);
}
}
}
double ans = inf;
for(int i = 0; i < all; ++i)
ans = min(ans, dp[i][t]);
if(ans >= inf) printf("Impossible\n");
else printf("%.3f\n", ans);
}
return 0;
}

  

POJ 2686 Traveling by Stagecoach (状压DP)的更多相关文章

  1. POJ 2686 Traveling by Stagecoach 壮压DP

    大意是有一个人从某个城市要到另一个城市(点数<=30) 然后有n个马车票,相邻的两个城市走的话要消耗掉一个马车票. 花费的时间呢,是马车票上有个速率值,用边/速率就是花的时间. 问最后这个人花费 ...

  2. poj 2686 Traveling by Stagecoach ---状态压缩DP

    题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马. 点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) ...

  3. Traveling by Stagecoach /// 状压DP oj22914

    题目大意: 输入n,m,p,a,b n是车票数(1<=n<=8),m是城市数(2<=m<=30) p是路径数(可能为0),a是起点,b是终点 接下来一行有n个数 为每张车票的马 ...

  4. POJ 1185 炮兵阵地(状压DP)

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26426   Accepted: 10185 Descriptio ...

  5. POJ 2411 Mondriaan's Dream -- 状压DP

    题目:Mondriaan's Dream 链接:http://poj.org/problem?id=2411 题意:用 1*2 的瓷砖去填 n*m 的地板,问有多少种填法. 思路: 很久很久以前便做过 ...

  6. POJ 2411 Mondriaan's Dream ——状压DP 插头DP

    [题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出 ...

  7. poj 2288 Islands and Bridges ——状压DP

    题目:http://poj.org/problem?id=2288 状压挺明显的: 一开始写了(记忆化)搜索,但一直T: #include<iostream> #include<cs ...

  8. 【POJ 2923】Relocation(状压DP+DP)

    题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...

  9. POJ 1185 炮兵阵地 (状压DP)

    题目链接 题意 : 中文题不详述. 思路 :状压DP,1表示该位置放炮弹,0表示不放.dp[i][j][k],代表第 i 行的状态为k时第i-1行的状态为 j 时放置的最大炮弹数.只是注意判断的时候不 ...

随机推荐

  1. js改变select的选中项不触发select的change事件

    // test var selectEl = document.querySelector('select') var buttonEl = document.querySelector('butto ...

  2. HDU - 5307 :He is Flying (分治+FFT)(非正解)

    JRY wants to drag racing along a long road. There are nn sections on the road, the ii-th section has ...

  3. WPF之X名称空间学习

    WPF的X名称空间都有什么呢?首先,盗用张图来说明: 我将就图表中的内容进行总结: 1.x:Array具有一个Iteams属性,它能暴漏一个ArratList实例,ArratList实例的内部成员类型 ...

  4. laravel DB::raw() DB::RAW()的用法

    laravel中的 DB::raw() 和DB::RAW()是同一种功能;用法如下 public function test() { $real = 66;]); $res = \ai\Models\ ...

  5. (转)详解C#中的反射

    本文转载自:http://blog.csdn.net/educast/article/details/2894892 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到 ...

  6. get方法传递中文数据的时候如何进行转码

    首先,如果是在js端的代码,用window.href进行请求时,需要进行转码 前台jsp中: var param = document.getElementById('param').value;pa ...

  7. PostgreSQL 数据库角色

    数据库角色PostgreSQL使用角色的概念管理数据库访问权限.一个角色可以被看成是一个数据库用户或者是一个数据库用户组,这取决于角色被怎样设置.角色可以拥有数据库对象(例如,表和函数)并且能够把那些 ...

  8. Exception in thread "main" javax.validation.ValidationException: Unable to find a default provider

    Exception in thread "main" javax.validation.ValidationException: Unable to find a default ...

  9. 使用cython把python编译so

    1.需求 为了保证线上代码安全和效率,使用python编写代码,pyc可直接反编译,于是把重要代码编译so文件 2.工作 2.1 安装相关库: pip install cython yum insta ...

  10. python-xlrd 实现excel 导入数据

    首先安装 xlrd 两种方式: 1.wheel 方式 安装: 首先要下载 wheel :