zoukankan      html  css  js  c++  java
  • 18.12.20 DSA Full Tank?(DP+BFS)

    描述

    After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you visited. Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?

    To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.

    输入

    The first line of input gives 1 ≤ n ≤ 1000 and 0 ≤ m ≤ 10000, the number of cities and roads. Then follows a line with n integers 1 ≤ pi ≤ 100, where pi is the fuel price in the ith city. Then follow m lines with three integers 0 ≤ uv < n and 1 ≤ d ≤ 100, telling that there is a road between u and v with length d. Then comes a line with the number 1 ≤ q ≤ 100, giving the number of queries, and q lines with three integers 1 ≤ c ≤ 100, s and e, where c is the fuel capacity of the vehicle, s is the starting city, and e is the goal.

    输出

    For each query, output the price of the cheapest trip from s to e using a car with the given capacity, or "impossible" if there is no way of getting from s to e with the given car.

    样例输入

    5 5
    10 10 20 12 13
    0 1 9
    0 2 8
    1 2 1
    1 3 11
    2 3 7
    2
    10 0 3
    20 1 4

    样例输出

    170
    impossible
     1 #include <iostream>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <stack>
     5 #include <string>
     6 #include <math.h>
     7 #include <queue>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <set>
    11 #include <vector>
    12 #include <fstream>
    13 #define maxn 1005
    14 #define inf 999999
    15 #define cha 127
    16 #define eps 1e-6 
    17 #define oo 1503
    18 using namespace std;
    19 
    20 int n, m;
    21 int price[maxn];
    22 struct node {
    23     int e, len;
    24     node(int a, int b) :e(a), len(b) {}
    25 };
    26 struct state {
    27     int city, remain, cost;
    28     state(int a, int b, int c) :city(a), remain(b), cost(c) {}
    29 };
    30 bool operator <(state a, state b) {
    31     return a.cost > b.cost;
    32 }
    33 priority_queue<state>steps;
    34 vector<vector<node>>G(1005);
    35 int dp[1005][105];
    36 int c, s, e;
    37 
    38 void f() {
    39     while (!steps.empty())steps.pop();
    40     steps.push(state(s, 0, 0));
    41     while (!steps.empty()) {
    42         state k = steps.top();
    43         if (k.city == e) {
    44             printf("%d
    ", k.cost);
    45             return;
    46         }
    47         steps.pop();
    48         int s = k.city, remain = k.remain, cost = k.cost;
    49         int size = G[s].size();
    50         if (remain < c&&cost+price[s]<dp[s][remain+1]) {
    51             steps.push(state(s, remain + 1, cost + price[s]));
    52             dp[s][remain + 1] =  cost + price[s];
    53         }
    54         for (int i = 0; i < size; i++) {
    55             int len = G[s][i].len;
    56             if (remain >= len && cost < dp[G[s][i].e][remain - len])
    57             {
    58                 steps.push(state(G[s][i].e, remain - len, cost));
    59                 dp[G[s][i].e][remain - len] = cost;
    60             }
    61         }
    62     }
    63     printf("impossible
    ");
    64 }
    65 
    66 void init() {
    67     scanf("%d%d", &n, &m);
    68     for (int i = 0; i < n; i++)
    69         scanf("%d", &price[i]);
    70     for (int i = 1; i <= m; i++) {
    71         int s, e, len;
    72         scanf("%d%d%d", &s, &e, &len);
    73         G[s].push_back(node(e, len));
    74         G[e].push_back(node(s, len));
    75     }
    76     int kase;
    77     scanf("%d", &kase);
    78     while (kase--) {
    79         scanf("%d%d%d", &c, &s, &e);
    80         for (int i = 0; i < n; i++)
    81             for (int j = 0; j <= c; j++)
    82                 dp[i][j] = inf;
    83         dp[0][0] = 0;
    84         f();
    85     }
    86 }
    87 
    88 int main() {
    89     init();
    90     return 0;
    91 }
    View Code

    思路

    记录每次BFS的状态值,用优先队列来维护(cost从小到大),直到遇到某个状态到达目标城市或队列为空(没有合法值)。

    感觉也并不是很最短路。

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    计算机基础知识
    测试用例设计
    Windows MySql增量备份、完整备份采坑之路
    GOF 的23种JAVA常用设计模式总结 02 UML中的类图与类图之间的关系
    GOF 的23种JAVA常用设计模式总结 01 设计模式的概念分类和功能
    Springboot 整合ApachShiro完成登录验证和权限管理
    玩转Spring全家桶笔记 04 Spring的事务抽象、事务传播特性、编程式事务、申明式事务
    玩转Spring全家桶笔记 03 Spring的JDBC操作以及SQL批处理的实现
    玩转Spring全家桶笔记 02 那些好用的连接池HikariCP
    Springboot token令牌验证解决方案 在SpringBoot实现基于Token的用户身份验证
  • 原文地址:https://www.cnblogs.com/yalphait/p/10150773.html
Copyright © 2011-2022 走看看