Solving the problem of traveling business by the method of poor lifting

Source: Internet
Author: User

First, the problem description

, a traveler departs from point A and needs to go through 5 cities without repeating abcde, and finally back to a. The cost per city (i.e. weight) is now required to find a path with the least total cost, i.e. the weight and the smallest path.

Second, the algorithm description

1. Algorithm One: Mountaineering method (Greedy method)

That is, in each city before the departure of the next can go to the city cost (weight), find the right value of the smallest walk.

Advantages and Disadvantages: Because only in each city to consider the minimum weight, but when the city is finished, the weight and not necessarily the smallest, so the algorithm can not get the exact solution, but the complexity of the algorithm is low.

2. Algorithm two: Brute-lifting method (This procedure uses this algorithm)

That is to calculate the weight of each path and, the choice of weights and the smallest path is the best path.

Advantages and Disadvantages: This algorithm can get the exact solution, but because of the use of poor lifting, so the complexity is higher.

Iii. Source code Example

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceTravel Business Issues8 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -             //Weight Matrix -             int[,] Weightvalue =New int[Ten,Ten]; the             //Number of cities -             intCitynum =0; -             //City Name -             string[] CityName =New string[Ten]; +             //The departure city is indicated by index -             intBegincityindex =0; +             //Weighted Value A             intMinweightvalue =int. MaxValue; at             intWeightSum =Minweightvalue; -             //Best Path -             int[] BestWay; -  -             //Enter the number of cities -Console.WriteLine ("Please enter the number of cities:"); inCitynum =int. Parse (Console.ReadLine ()); -Console.WriteLine ("Please enter each city name"); to              for(inti =0; i < Citynum; i++) +             { -Cityname[i] =console.readline (); the             } *Console.WriteLine ("Please enter a weight matrix:"); $             //Input Weights MatrixPanax Notoginseng              for(inti =0; i < Citynum; i++) -             { the                  for(intj =0; J < Citynum; J + +) +                 { AWeightvalue[i, J] =int. Parse (Console.ReadLine ()); the                 } +             } -             //Enter departure City $Console.WriteLine ("Please enter the departure city:"); $Begincityindex =int. Parse (Console.ReadLine ()); -  -BestWay =New int[citynum+1]; the  -              for(inti =0; i < Citynum; i++)Wuyi             { the                 if(I! =Begincityindex) -                 { Wu                      for(intj =0; J < Citynum; J + +) -                     { About                         if(J! = i && J! =Begincityindex) $                         { -                              for(intK =0; K < Citynum; k++) -                             { -                                 if(k!=j && k!=i && k!=Begincityindex) A                                 { +                                      for(intt =0; T < Citynum; t++) the                                     { -                                         if(t!=i && t!=j && t!=k && t!=Begincityindex) $                                         { theWeightSum =Weightvalue[begincityindex,i] the+Weightvalue[i,j] the+Weightvalue[j,k] the+Weightvalue[k,t] -+Weightvalue[t,begincityindex]; in                                             if(minweightvalue>weightSum) the                                             { theMinweightvalue=WeightSum; Aboutbestway[0] =Begincityindex; thebestway[1] =i; thebestway[2] =J; thebestway[3] =K; +bestway[4] =T; -bestway[5] =Begincityindex; the                                             }Bayi                                         } the                                     } the                                 } -                             } -                         } the                     } the                 } the             } the  -  theConsole.WriteLine ("the shortest path is:"); theConsole.WriteLine (cityname[bestway[0]]+" -" the+ cityname[bestway[1]] +" -"94+ cityname[bestway[2]] +" -" the+ cityname[bestway[3]] +" -" the+ cityname[bestway[4]] +" -" the+ cityname[bestway[5]]);98  AboutConsole.WriteLine ("the minimum weight value is {0}", minweightvalue); - 101 Console.readkey ();102         }103     }104}
View Code

Solving the problem of traveling business by the method of poor lifting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.