C++ 最短/最便宜的路径?这里如何使用动态规划?

C++ 最短/最便宜的路径?这里如何使用动态规划?,c++,graph,shortest-path,C++,Graph,Shortest Path,我有一个关于动态规划的问题。这是一个最短路径问题。前提是我需要帮助一个“朋友”编写一个程序,用最便宜的瓷砖铺一条通往他的小屋的路。变量D(到棚的距离)可以是1>距离; //获取瓷砖类型的数量 cin>>num; //所用瓷砖的成本 int tillelength[num]; int TilePrice[num]; for(int i=0;i>Tillelength[i]; cin>>TilePrice[i]; } 总成本=最便宜的平铺(距离、位数、平铺长度、平铺价格); 对我来说,这听起来不像是

我有一个关于动态规划的问题。这是一个最短路径问题。前提是我需要帮助一个“朋友”编写一个程序,用最便宜的瓷砖铺一条通往他的小屋的路。变量D(到棚的距离)可以是1>距离; //获取瓷砖类型的数量 cin>>num; //所用瓷砖的成本 int tillelength[num]; int TilePrice[num]; for(int i=0;i>Tillelength[i]; cin>>TilePrice[i]; } 总成本=最便宜的平铺(距离、位数、平铺长度、平铺价格);
对我来说,这听起来不像是一个最短路径问题。这更像是一个背包问题,因为我假设你在努力最小化价格的同时仍然达到你的目标距离

en.wikipedia.org/wiki/背包问题


希望我能帮上忙。

你能举一个失败的例子吗?简单描述一下你的算法怎么样?这段代码没有编译;它充满了小错误,这表明它不是您正在使用的实际代码,因此它比根本没有代码更糟糕。基本上,只要脱落距离“D”大于1,它就会失败。总有一个长度为1的磁贴,因此距离为1的成本将取决于用户输入的内容。对不起,它没有编译。我是从电脑上复制的,我现在正在使用手机。今天晚些时候,我将对其进行编辑,以便进行编译。算法是否高效运行与是否使用递归是正交的。
#include <iostream>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <limits.h>
#include <cstdio>


using namespace std;

int cheapestTiling(int dist, int numtiles, int A[], int B[]){

    //distance to the shed
    int shedDistance = dist;
    //number of types of tiles used
    int numberTiles = numtiles;

    //make new arrays for the costs and lengths of each tiles
    int LengthTile[numberTiles];
    int PriceTile[numberTiles];
    int costPerSize[numberTiles];

    //min length, min price
    int minlength = 0;
    int minprice = 0;

    while (shedDistance != 0){

        for (int i = 0; i < nAumberTiles; i++){
            LengthTile[i] = A[i];
            PriceTile[i] = B[i];
            costPerSize[i] = (A[i]/B[i])

            while((LengthTile[i] > LengthTile[i+1])
            {
                if(shedDistance > lengthTile[i])
                {
                //here i'm trying to find the longer tile and use those first
                //I havent started worrying about the cost yet and am just focusing
                //on the length/distance aspect
                int tempTile = lengthTile[i];
                shedDistance = shedDistance - tempTile;
                }
               // else if((shedDistance < lengthTile[i]) && (lengthTile[i+1] < shedDistance))
            }

        }
        minlength = LengthTile[0];
 minprice = PriceTile[0];

        for(int i = 1; i < numberTiles; i++)
        {
            if(LengthTile[i] < minlength)
            {
                minlength = LengthTile[i];
            }
            if(PriceTile[i] < minprice)
            {
                minprice = PriceTile[i];
            }
        }

      //error check for shed distance = 1
      if (shedDistance == 1)
      {
          shedDistance = shedDistance - minlength;
          return minprice;
      }
      //error check for shed distance < 0
      else if (shedDistance < 0)
      {
     return 0;
      }

    }



}

int main (){


//distance to shed
int distance = 0;
//number of types of tiles used
int num = 0;
//the return of the total cost, the answer
int totalCost = 0;


//get distance to shed
 cin >> distance;
//get number of types of tiles
 cin >> num;

 //cost of each tile used
 int TileLength[num];
 int TilePrice[num];


for (int i = 0; i < num; i++)
{
    cin >> TileLength[i];
    cin >> TilePrice[i];
}

totalCost = cheapestTiling(distance, numTiles, TileLength, TilePrice);
cout << totalCost << endl;


}