Facebook
From Filip, 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 192
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     float miasta[130][3];
  10.     float odleglosc[130][130];
  11.     float c = 1000.0;
  12.     float wspx, wspy, a, cos;
  13.     float suma = 0.0, suma_aktualna = 0.0;
  14.     stringstream  trasaakt, trasa;
  15.  
  16.     ifstream plik;
  17.     plik.open("ch130.tsp");
  18.     plik >> cos;
  19.  
  20.  
  21.     int i = 0;
  22.  
  23.  
  24.     while( !plik.eof() )
  25.     {
  26.         plik >> a >> wspx >> wspy;
  27.  
  28.         miasta[i][0] = wspx;
  29.         miasta[i][1] = wspy;
  30.         miasta[i][2] = 0;
  31.  
  32.         i++;
  33.     }
  34.  
  35.     for(int i = 0; i < 130; i++)
  36.         for(int j = 0; j < 130; j++)
  37.             odleglosc[i][j] = sqrt(pow(miasta[j][0] - miasta[i][0], 2) + pow(miasta[j][1] - miasta[i][1], 2));
  38.  
  39.  
  40.     for(int i = 0; i < 130; i++)
  41.     {
  42.         miasta[i][2] = 1;
  43.  
  44.         int x = i;
  45.         int y;
  46.  
  47.         for(int k = 0; k < 129; k++)
  48.         {
  49.  
  50.            for(int j = 0; j < 130; j++)
  51.             {
  52.                 if(odleglosc[x][j] != 0 && miasta[j][2] == 0 && odleglosc[x][j] < c)
  53.                 {
  54.                     c = odleglosc[x][j];
  55.                     y = j;
  56.                 }
  57.             }
  58.  
  59.  
  60.             suma += odleglosc[x][y];
  61.  
  62.             x = y;
  63.  
  64.             miasta[y][2] = 1;
  65.  
  66.  
  67.         }
  68.  
  69.         suma += odleglosc[x][i];
  70.  
  71.  
  72.         for(int c = 0; c < 130; c++) miasta[c][2] = 0;
  73.  
  74.         cout <<  suma << endl;
  75.         suma = 0;
  76.  
  77.     }
  78.  
  79.  
  80. }
  81.