On Tuesday, 14 December 2021 at 05:54:49 UTC+1, Darioes wrote:
Hi guys,
Im programing a new exerciese that i need to do for a college project, in C++.
You need to pick the minimal ways so that the car's fuel tank is as low as possible.
IOW, you need to pick the path such that the maximum cost of any single step is minimal.
This is what I think should be done, but it isn’t working.
First you should decide the approach/algorithm: I suppose what you want to do here is a full, brute-force, search of all possible paths from origin to target and, for each path, compute the maximum cost of any single step (which you can do while building
the path), and, if that cost is lower than what you have found for the paths visited so far, record this path as the new minimal in our sense. At the end of the loop you will know which path is minimal overall. -- Right?
Do you have any ideas on how I can make it work?
int nodo = origen;
int minimo = INT_MAX;
while(nodo != destino){
for (int j = 0; j < totalNodos; j++){
if(!visitado[j] && coste[nodo][j] < minimo){
nodoMinimo = j;
minimo = coste[nodo][j];
}
}
nodo = v
Wasn't that supposed to be 'nodo = nodoMinimo'? But that's not going to work anyway: minimal is a path, not a single step, of the single step you rather want to know if it is the maximum within that path...
if(minimoTotal < minimo ) minimoTotal = minimo;
}
As a minimum, you seem to be missing pieces. In particular, I assume 'coste' exists and is the adjacency matrix with graph connection costs that you get in input, but you are not showing how you initialise 'visitado' and you are only reading from it,
which makes little sense anyway.
One way to approach programming problems in general is to decompose them. Here you could as a first step write code that does the loop over all paths. Once that works and, say, prints to the console all paths one after the other, you add another piece
to it which is computing the maximum cost of the path's single step, and print that too along with the path. Finally you add the piece that keeps track of the minimum such value found so far and which path was that, and at the very end of the loop now
also prints that minimum together with which path was it. And incrementally I suppose we can also help...
Julio
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)