POJ 2686 Traveling by Stagecoach

状压DP

dp[s][p]用了哪几张票,到哪个节点的最小费用。

注意:G++ %.3lf输出会WA,但C++能过;改成%.3f,C++,G++都能AC

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

const double INF = 999999999999;
int n, m, p, a, b;
int t[20];
struct Edge
{
    int to;
    int dis;
}e[1000];
int tot;
vector<int>g[50];
double dp[500][50];

int main()
{
    while (~scanf("%d%d%d%d%d", &n, &m, &p, &a, &b))
    {
        if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0) break;

        tot = 0;
        for (int i = 1; i <= m; i++) g[i].clear();
        for (int i = 0; i < (1 << n); i++)
            for (int j = 1; j <= m; j++) dp[i][j] = INF;

        for (int i = 0; i < n; i++) scanf("%d", &t[i]);
        for (int i = 1; i <= p; i++)
        {
            int u, v, c; scanf("%d%d%d", &u, &v, &c);
            e[++tot].to = v; e[tot].dis = c; g[u].push_back(tot);
            e[++tot].to = u; e[tot].dis = c; g[v].push_back(tot);
        }

        dp[0][a] = 0;
        for (int i = 0; i < (1 << n); i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (dp[i][j] == INF) continue;
                for (int k = 0; k < n; k++)
                {
                    if ((i | (1 << k)) == i) continue;
                    for (int s = 0; s < g[j].size(); s++)
                    {
                        dp[(i | (1 << k))][e[g[j][s]].to] = min(
                            dp[(i | (1 << k))][e[g[j][s]].to],
                            dp[i][j] + 1.0*e[g[j][s]].dis / (1.0*t[k]));
                    }
                }
            }
        }

        double ans = INF;
        for (int i = 0; i < (1 << n); i++)
            ans = min(ans, dp[i][b]);
        if (ans == INF) printf("Impossible\n");
        else printf("%.3f\n", ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zufezzt/p/5414423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值