Question

Give 10 cities located within 1,000 miles (left to right) by 1,000 miles (bottom to top)...

Give 10 cities located within 1,000 miles (left to right) by 1,000 miles (bottom to top) region, calculate the shortest traveling path from the traveling salesman problem. The 10 cities are A, B, C, D, E, F, G, H, I, and J. Locations of 10 cities are

A (X: 100, Y: 300)

B (X: 200, Y: 130)

C (X: 300, Y: 500)

D (X: 500, Y: 390)

E (X: 700, Y: 300)

F (X: 900, Y: 600)

G (X: 800, Y: 950)

H (X: 600, Y: 560)

I (X: 350, Y: 550)

  1. The text file contains the shortest path value and the sequence order of 10 cities visited.
  2. Not asking for code but the text file to get an idea please.
  3. for C++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:--                                                             Date:--01/04/2019

#include <iostream>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

map<char, pair<int, int> > val;

double calculateDistance(pair<int, int> first, pair<int, int> second) {
int x1 = first.first;
int y1 = first.second;

int x2 = second.first;
int y2 = second.second;

return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}

pair< double, vector<char> > tsp(vector<char> v) {
vector<char> path;
pair< double, vector<char> > answerPair;
pair<int, int> p = val[v[0]];
v.erase(v.begin());

double shortestDistance = 1<<30;
int size = v.size();

do {
double weight = 0;

weight += calculateDistance(p, val[v[0]]);
for(int i=1; i<=size; i++) {
weight += calculateDistance(val[v[i-1]], val[v[i]]);
}
weight += calculateDistance(val[v[size]], p); if(weight < shortestDistance) {
shortestDistance = weight;
path.clear();
path.push_back('A');
for(vector<char>::iterator it = v.begin(); it != v.end(); it++) {
path.push_back(*it);
}
}
} while(next_permutation(v.begin(), v.end()));answerPair.first = shortestDistance;
answerPair.second = path;
return answerPair;
}

int main() {
val['A'] = make_pair(100, 300);
val['B'] = make_pair(200, 130);
val['C'] = make_pair(300, 500);
val['D'] = make_pair(500, 390);
val['E'] = make_pair(700, 300);
val['F'] = make_pair(900, 600);
val['G'] = make_pair(800, 950);
val['H'] = make_pair(600, 560);
val['I'] = make_pair(350, 550);
val['J'] = make_pair(270, 350);

vector<char> v;
for(char ch = 'A'; ch <= 'J'; ch++) {
v.push_back(ch);
}
pair< double, vector<char> > p = tsp(v);
cout << "Shortest distance value: " << p.first << " miles" << endl;
cout << "Sequence order of 10 cities: ";
for(vector<char>::iterator it = p.second.begin(); it != p.second.end(); it++) {
cout << *it << " -> ";
}
cout << 'A';
}

output:--

Add a comment
Know the answer?
Add Answer to:
Give 10 cities located within 1,000 miles (left to right) by 1,000 miles (bottom to top)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a) Draw the budget constraint for a person with income of $1,000 if the price of...

    a) Draw the budget constraint for a person with income of $1,000 if the price of Pepsi is $5 and the price of pizza is $10. b) Choose any combination of pizzas and from the table and write a proper budget equation with Pizza denoted as good x and Pepsi denoted as good Y? c) What is the slope of the budget constraint? d) If the person wants to spend equal amount of money on both pizza and Pepsi what...

  • Problem 10-31 (Algo) Large-scale integrated (LSI) circuit chips are made in one department of an electronics...

    Problem 10-31 (Algo) Large-scale integrated (LSI) circuit chips are made in one department of an electronics firm. These chips are incorporated into analog devices that are then encased in epoxy. The yield is not particularly good for LSI manufacture, so the AQL specified by that department is 20% while the LTPD acceptable by the assembly department is 52%. Assume the company is willing to accept a consumer's risk of 10 percent and a producer's risk of 5 percent. Use Exhibit...

  • Project Cycle

    The Advanced Tech Company has a project to design an integrated information database for a major bank. Data for the project are given in Table 1 below. Indirect project costs amount to $300 per day. The company will incur a $150 per day penalty for each day the project lasts beyond day 14.(a) What is the shortest time duration for this project regardless of cost?(30 marks) (b) What is the total cost and time of the minimum-cost schedule?(10 marks)Table 1:...

  • The table below shows some of the expenditure amounts in the economy of Arkinia. The MPC,...

    The table below shows some of the expenditure amounts in the economy of Arkinia. The MPC, the MTR, and the MPM are all constant, as are the values of the three injections. a. Complete the table below. Y T YD C S I G X IM XN AE 0 50 180 50 100 60 -5 50 180 50 30 200 60 130 10 50 180 50 300 80 195 50 180 50 40 400 100 40 50 180 50 50...

  • ***** running late, mere 3 hours left for due time, please help ** #needed in c++...

    ***** running late, mere 3 hours left for due time, please help ** #needed in c++ #inputfile MinPath2.txt 0   SF 1   LA 2   DALLAS 3   CONCORD 4   PHOENIX 5   CHICAGO 6   ST LOUIS 7   BOSTON 8   NY 9   LONDON 10   PARIS 11   TOKYO 12   BANGKOK 13   MEXICO CITY 14   MONTREAL -1   0   1   40 0   2   100 0   4   130 0   8   200 0   9   800 0   10   900 1   2   50 1   3   80 1   4   70 1   8  ...

  • b. The value of equilibrium income is $ 600 c. At equilibrium, the value of total...

    b. The value of equilibrium income is $ 600 c. At equilibrium, the value of total injections is $ _____ and of total leakages is $ ______ d. The value of the MPE in Arkinia is ____ . Round your answer to 2 decimal places. e. The value of the multiplier in Arkinia is ____ . Round your answer to 2 decimal places. f. Suppose that exports from Arkinia were to increase by $90. Draw the new aggregate expenditure function...

  • 210 Excel Inventory Valuation Spreadsheet Fall 19 [Protected View] Microsoft Excel X File Page Layout Formulas Data Rev...

    210 Excel Inventory Valuation Spreadsheet Fall 19 [Protected View] Microsoft Excel X File Page Layout Formulas Data Review View Home Insert iProtected View This file originated from an Internet location and might be unsafe. Click for more details Enable Editing fr A8 В C D Е F G J K м O р 1 2 Excel Project Pt2: Inventory Valuation Methods The assumptions are provided at the top of the Calculations worksheet. In making the calculations in each step link...

  • Assignment Score: 62.54 Resources Give Up! Resume Question 1 of 4 > Stacked Attempt Aggregate Demand...

    Assignment Score: 62.54 Resources Give Up! Resume Question 1 of 4 > Stacked Attempt Aggregate Demand I - Work It Out: Question 2 Suppose that the money demand function is - 600 - 75 wherer is the interest rate in percent The money supply Mis 51200, and the price level Pis fixed 4 Round answers to one place after the decimal when necessary. c. What happens to the equilibrium interest rate, r, if the supply of money is raised from...

  • Use the following inventory information for the MRP problems that follow regarding Z: Lot Size: On Hand: 4,000 800 0 Safety Stock: Allocated: Lead Time: Regardless of the values for Y co...

    Use the following inventory information for the MRP problems that follow regarding Z: Lot Size: On Hand: 4,000 800 0 Safety Stock: Allocated: Lead Time: Regardless of the values for Y computed above, use the following information going forward. (That is, do not change your earlier answers but use the new values going forward.) Note: These are not the correct answers for the questions above. Planned order releases of 1.200 for X in periods four, five, and seven. Planned order...

  • For this exercise assume you are a human resources specialist for a hospital and you are...

    For this exercise assume you are a human resources specialist for a hospital and you are going to need to hire many nurses to staff a new hospital with 1200 beds. You realize that a good way to predict the number of nurses needed is to see how many beds a hospital has. So, you call around to a few of your HR colleagues in the area (actually 8) to see how many nurses and beds each hospital has. You...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT