Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 38733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:45:15+00:00 2026-05-10T14:45:15+00:00

Say you have a shipment. It needs to go from point A to point

  • 0

Say you have a shipment. It needs to go from point A to point B, point B to point C and finally point C to point D. You need it to get there in five days for the least amount of money possible. There are three possible shippers for each leg, each with their own different time and cost for each leg:

Array (     [leg0] => Array         (             [UPS] => Array                 (                     [days] => 1                     [cost] => 5000                 )              [FedEx] => Array                 (                     [days] => 2                     [cost] => 3000                 )              [Conway] => Array                 (                     [days] => 5                     [cost] => 1000                 )          )      [leg1] => Array         (             [UPS] => Array                 (                     [days] => 1                     [cost] => 3000                 )              [FedEx] => Array                 (                     [days] => 2                     [cost] => 3000                 )              [Conway] => Array                 (                     [days] => 3                     [cost] => 1000                 )          )      [leg2] => Array         (             [UPS] => Array                 (                     [days] => 1                     [cost] => 4000                 )              [FedEx] => Array                 (                     [days] => 1                     [cost] => 3000                 )              [Conway] => Array                 (                     [days] => 2                     [cost] => 5000                 )          )  ) 

How would you go about finding the best combination programmatically?

My best attempt so far (third or fourth algorithm) is:

  1. Find the longest shipper for each leg
  2. Eliminate the most ‘expensive’ one
  3. Find the cheapest shipper for each leg
  4. Calculate the total cost & days
  5. If days are acceptable, finish, else, goto 1

Quickly mocked-up in PHP (note that the test array below works swimmingly, but if you try it with the test array from above, it does not find the correct combination):

$shippers['leg1'] = array(     'UPS'    => array('days' => 1, 'cost' => 4000),     'Conway' => array('days' => 3, 'cost' => 3200),     'FedEx'  => array('days' => 8, 'cost' => 1000) );  $shippers['leg2'] = array(     'UPS'    => array('days' => 1, 'cost' => 3500),     'Conway' => array('days' => 2, 'cost' => 2800),     'FedEx'  => array('days' => 4, 'cost' => 900) );  $shippers['leg3'] = array(     'UPS'    => array('days' => 1, 'cost' => 3500),     'Conway' => array('days' => 2, 'cost' => 2800),     'FedEx'  => array('days' => 4, 'cost' => 900) );      $times = 0; $totalDays = 9999999;  print '<h1>Shippers to Choose From:</h1><pre>'; print_r($shippers); print '</pre><br />';  while($totalDays > $maxDays && $times < 500){             $totalDays = 0;             $times++;             $worstShipper = null;             $longestShippers = null;             $cheapestShippers = null;              foreach($shippers as $legName => $leg){                 //find longest shipment for each leg (in terms of days)                 unset($longestShippers[$legName]);                 $longestDays = null;                          if(count($leg) > 1){                     foreach($leg as $shipperName => $shipper){                         if(empty($longestDays) || $shipper['days'] > $longestDays){                             $longestShippers[$legName]['days'] = $shipper['days'];                             $longestShippers[$legName]['cost'] = $shipper['cost'];                             $longestShippers[$legName]['name'] = $shipperName;                             $longestDays = $shipper['days'];                         }                     }                            }             }              foreach($longestShippers as $leg => $shipper){                 $shipper['totalCost'] = $shipper['days'] * $shipper['cost'];                  //print $shipper['totalCost'] . ' &lt;?&gt; ' . $worstShipper['totalCost'] . ';';                  if(empty($worstShipper) || $shipper['totalCost'] > $worstShipper['totalCost']){                     $worstShipper = $shipper;                     $worstShipperLeg = $leg;                 }             }              //print 'worst shipper is: shippers[$worstShipperLeg][{$worstShipper['name']}]' . $shippers[$worstShipperLeg][$worstShipper['name']]['days'];             unset($shippers[$worstShipperLeg][$worstShipper['name']]);              print '<h1>Next:</h1><pre>';             print_r($shippers);             print '</pre><br />';              foreach($shippers as $legName => $leg){                 //find cheapest shipment for each leg (in terms of cost)                 unset($cheapestShippers[$legName]);                 $lowestCost = null;                  foreach($leg as $shipperName => $shipper){                     if(empty($lowestCost) || $shipper['cost'] < $lowestCost){                         $cheapestShippers[$legName]['days'] = $shipper['days'];                         $cheapestShippers[$legName]['cost'] = $shipper['cost'];                         $cheapestShippers[$legName]['name'] = $shipperName;                         $lowestCost = $shipper['cost'];                     }                 }                  //recalculate days and see if we are under max days...                 $totalDays += $cheapestShippers[$legName]['days'];               }             //print '<h2>totalDays: $totalDays</h2>';         }          print '<h1>Chosen Shippers:</h1><pre>';         print_r($cheapestShippers);         print '</pre>'; 

I think I may have to actually do some sort of thing where I literally make each combination one by one (with a series of loops) and add up the total ‘score’ of each, and find the best one….

EDIT: To clarify, this isn’t a ‘homework’ assignment (I’m not in school). It is part of my current project at work.

The requirements (as always) have been constantly changing. If I were given the current constraints at the time I began working on this problem, I would be using some variant of the A* algorithm (or Dijkstra’s or shortest path or simplex or something). But everything has been morphing and changing, and that brings me to where I’m at right now.

So I guess that means I need to forget about all the crap I’ve done to this point and just go with what I know I should go with, which is a path finding algorithm.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:45:16+00:00Added an answer on May 10, 2026 at 2:45 pm

    Could alter some of the shortest path algorithms, like Dijkstra’s, to weight each path by cost but also keep track of time and stop going along a certain path if the time exceeds your threshold. Should find the cheapest that gets you in under your threshold that way

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten
Say I have a SqlAlchemy model something like this: from sqlalchemy.ext.declarative import declarative_base from
Let's say have a string... String myString = my*big*string*needs*parsing; All I want is to
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say I have an HTML form like this to collect an email from a
Let’s say I have this array with shipments ids. s = Shipment.find(:all, :select =>
Say I have a class called Money which has parameters Dollars and Cents I
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
Say I have a Telerik MVC Grid, AJAX bound and I want to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.