Advertisement
Spocoman

03. Space Travel

Feb 14th, 2024 (edited)
1,006
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. #include <limits>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int main() {
  11.     string planet, line;
  12.     getline(cin, planet);
  13.  
  14.     vector<string> planets;
  15.  
  16.     while (planet != "END") {
  17.         planets.push_back(planet);
  18.         getline(cin, planet);
  19.     }
  20.  
  21.     vector<vector<int>> timeRoutes;
  22.  
  23.     int minTime = numeric_limits<int>::max(),
  24.         maxTime = 0;
  25.  
  26.     for (int r = 0; r < planets.size(); r++) {
  27.         getline(cin, line);
  28.  
  29.         istringstream ss(line);
  30.  
  31.         vector<int> times;
  32.  
  33.         int time;
  34.  
  35.         while (ss >> time) {
  36.             times.push_back(time);
  37.             if (time > maxTime) {
  38.                 maxTime = time;
  39.             }
  40.             if (time > 0 && time < minTime) {
  41.                 minTime = time;
  42.             }
  43.         }
  44.         timeRoutes.push_back(times);
  45.     }
  46.  
  47.     vector<int> routes;
  48.  
  49.     getline(cin, line);
  50.  
  51.     while (line != "END") {
  52.         vector<string> route;
  53.  
  54.         istringstream ss(line);
  55.  
  56.         while (ss >> planet) {
  57.             route.push_back(planet);
  58.         }
  59.  
  60.         int time = 0, row, col;
  61.  
  62.         for (size_t i = 0; i < route.size() - 1; i++) {
  63.             row = distance(planets.begin(), find(planets.begin(), planets.end(), route[i])),
  64.                 col = distance(planets.begin(), find(planets.begin(), planets.end(), route[i + 1]));
  65.             time += timeRoutes[row][col];
  66.         }
  67.  
  68.         routes.push_back(time);
  69.  
  70.         getline(cin, line);
  71.     }
  72.  
  73.     for (int t : { minTime, maxTime }) {
  74.         for (size_t r = 0; r < timeRoutes.size(); r++) {
  75.             for (size_t c = 0; c < timeRoutes.size(); c++) {
  76.                 if (t == timeRoutes[r][c]) {
  77.                     cout << t << ": " << planets[r] << " -> " << planets[c] << endl;
  78.                 }
  79.             }
  80.         }
  81.     }
  82.  
  83.     int totalTime = 0;
  84.  
  85.     for (auto& t : routes) {
  86.         cout << t << endl;
  87.         totalTime += t;
  88.     }
  89.  
  90.     cout << totalTime << endl;
  91.  
  92.     return 0;
  93. }
Advertisement
Comments
  • Vvardenfell
    312 days
    # text 2.85 KB | 0 0
    1. int minTime = 2147483647 should be int minTime = std::numeric_limits<int>::max() to get rid of the magic number and to be portable to systems where int doesn't happen to be 32 bits in size. also while not really tragic in the example at hand, but generally a good practice is to have const-correctness when working with references or pointers. when you obtain a reference to an element of a container using auto, as in "auto& t", then the compiler will deduce a const- or non-const iterator based on the constness of the container to whose elements the iterator will refer. when using "const auto& t" instead, the compiler will deduce the type of a const-iterator that does not allow modification of the elements of the container, independently of whether the container itself is const or not. as the elements that the iterator refers to only get read, but never written, it makes sense to make that intention clear by using "const auto& t" over "auto& t". in addition auto should be used only if the actual type is either unclear or very complex, as with nested template types. here the type is int, so "const int& t" would be more appropriate. but as the int is const anyway and int is a simple type that's cheap to copy, it's potentially better to just copy it instead of taking a reference to it and then we also no longer need the const, resulting in "int t" instead of "auto& t". also i saw that int is used as the type for the counter variable in multiple for loops when working with vector sizes, such as in "for (int r = 0; r < planets.size(); r++)". while it's unlikely, it's possible for the size of the vector to exceed the maximum value of an int. usually std::size_t is used in this case or the typedef member size_type that is provided by any standard container, such as "std::vector<std::string>>::size_type", which is usually a typedef for std::size_t (i actually don't know of any implementation where it isn't) and std::size_t is usually itself a typedef for unsigned int, unsigned long or unsigned long long. so using std::size_t for the counter variable in for loops that are compared against the size of containers should work on just about any platform. i also was happy to see the adoption of the -1 in the condition of "for (int i = 0; i < route.size() - 1; i++)" from one of my comments on the other pastebin. at least i assume it was adopted from there, if not then even better i guess =) also be aware that the calls to find() on line 61 and 62 can return the end-iterator if the entered planet of the route is not contained in the planets vector, resulting in an invalid value for row and col and even if the route's planet is contained in the planets vector it's possible that while the row value will be valid in that case, that the col value isn't if on line 25 an insufficient amount of times was provided by the user. wishing you a nice day and hope you have fun with c++ though =)
Add Comment
Please, Sign In to add comment
Advertisement