poj2686


题目描述:

有限车票去走城市,限定起点a,终点b,求最短时间。

题解:

每张车票都不一样,因此到达某个点的描述并不够,而是要再加上已经用了什么票.因此状压dp

重点:

状压dp,dp的状态决定顺序:从0开始

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(int i = a;i < b;i++)
#define REP_D(i, a, b) for(int i = a;i <= b;i++)
typedef long long ll;

using namespace std;

const int maxn = (1<<8) + 100;
const int key = (1<<8) - 1;
const int INF = INT_MAX/2 - 1;

double dp[maxn][100];
int d[maxn][maxn], t[maxn], a, b, p, n, m;

void get_dp()
{
    REP_D(i, 0, key)
    {
        REP_D(j, 0, m)
        {
            dp[i][j] = INF;
        }
    }
    dp[0][a] = 0;//什么票都没用,在a点
    REP_D(i, 0, key)//决定顺序
    {
        REP_D(j, 0, m - 1)
        {
            if(!(fabs(dp[i][j] - INF) < 0.0001) )//其实有很多不合法的东西
            {
                REP_D(k, 0, n - 1)
                {
                    if(((1<<k)&i) == 0)//看用哪一张车票
                    {
                        REP_D(z, 0, m - 1)//尝试去另一个地方.
                        {
                            if(d[j][z] != INF)
                            {
                                dp[(1<<k)|i][z] = min(dp[(1<<k)|i][z], dp[i][j] + d[j][z]*1.0/(1.0*t[k]));
                            }
                        }
                    }
                }
            }
        }
    }
}
void solve()
{
    get_dp();
    double ans = INF;
    REP_D(i, 0, key)
    {
        ans = min(ans, dp[i][b]);
    }
    if(fabs(ans - INF) < 0.00001)
        printf("Impossible\n");
    else
        printf("%f\n", ans);
}
int main()
{
    //freopen("1Ain.txt", "r", stdin);
    //freopen("1Aout.txt", "w", stdout);
    while(scanf("%d%d%d%d%d", &n, &m, &p, &a, &b))
    {
        if(!n && !m && !p && !a && !b)
            break;
        a--;
        b--;
        REP(i, 0, n)
            scanf("%d", &t[i]);
        REP_D(i, 0, m - 1)
        {
            REP_D(j, 0, m - 1)
            {
                d[i][j] = INF;
            }
        }
        REP(i, 0, p)
        {
            int x, y, dist;
            scanf("%d%d%d", &x, &y, &dist);
            x--;
            y--;
            d[x][y] = dist;
            d[y][x] = dist;
        }
        solve();
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值