jasonpogi1669

Transport (TLAC) Complete Solution using C++

Jul 26th, 2021 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. /*
  6.  
  7. SAMPLE TESTCASE 0:
  8.     4
  9.     JUU 2
  10.     JFK 3
  11.     JER 3
  12.     JRU 2
  13.     JFK 2 JER 3
  14.     JUU 2 JER 1 JRU 18
  15.     JUU 3 JFK 1 JRU 3
  16.     JFK 18 JER 3
  17.     JUU JRU
  18. SAMPLE OUTPUT 0 = JUU->JER->JRU
  19.  
  20. SAMPLE TESTCASE 1:
  21.     4
  22.     JUU 2
  23.     JFK 3
  24.     JER 3
  25.     JRU 2
  26.     JFK 2 JER 4
  27.     JUU 2 JER 1 JRU 18
  28.     JUU 3 JFK 1 JRU 3
  29.     JFK 18 JER 3
  30.     JUU JRU
  31. SAMPLE OUTPUT 1 = JUU->JFK->JER->JRU
  32.  
  33. */
  34.  
  35. vector<string> cities;
  36. vector<int> connected;
  37. vector<vector<int>> a;
  38. vector<vector<int>> cost;
  39. vector<int> path;
  40. vector<vector<int>> all;
  41.  
  42. int ConvertCity(string city) {
  43.     for (int i = 0; i < (int) cities.size(); i++) {
  44.         if (cities[i] == city) {
  45.             return i;
  46.         }
  47.     }
  48.     return -1;
  49. }
  50.  
  51. void FindPath(int origin, int destination, vector<bool> visited) {
  52.     visited[origin] = true;
  53.     path.emplace_back(origin);
  54.     if (origin == destination) {
  55.         all.emplace_back(path);
  56.     } else {
  57.         for (auto it = a[origin].begin(); it != a[origin].end(); it++) {
  58.             if (!visited[*it]) {
  59.                 FindPath(*it, destination, visited);
  60.             }
  61.         }
  62.     }
  63.     visited[origin] = false;
  64.     path.pop_back();
  65. }
  66.  
  67. int main() {
  68.     ios::sync_with_stdio(false);
  69.     cin.tie(0);
  70.     int n;
  71.     cin >> n;
  72.     cities = vector<string>(n);
  73.     connected = vector<int>(n);
  74.     for (int i = 0; i < n; i++) {
  75.         cin >> cities[i] >> connected[i];
  76.     }
  77.     a = vector<vector<int>>(n);
  78.     cost = vector<vector<int>>(n, vector<int>(n, 0));
  79.     for (int u = 0; u < n; u++) {
  80.         for (int i = 0; i < (int) connected[u]; i++) {
  81.             string city;
  82.             int weight;
  83.             cin >> city >> weight;
  84.             int v = ConvertCity(city);
  85.             a[u].emplace_back(v);
  86.             cost[u][v] = weight;
  87.         }
  88.     }
  89.     string start, end;
  90.     cin >> start >> end;
  91.     vector<bool> visited(n, false);
  92.     FindPath(ConvertCity(start), ConvertCity(end), visited);
  93.     int min_weight = INT_MAX;
  94.     int min_count = INT_MAX;
  95.     vector<string> res;
  96.     for (int u = 0; u < (int) all.size(); u++) {
  97.         int total = 0;
  98.         vector<string> collect;
  99.         for (int v = 0; v < (int) all[u].size(); v++) {
  100.             if (v < (int) all[u].size() - 1) {
  101.                 total += (cost[all[u][v]][all[u][v + 1]]);
  102.             }
  103.             collect.emplace_back(cities[all[u][v]]);
  104.         }
  105.         if (total < min_weight) {
  106.             min_weight = total;
  107.             min_count = (int) all[u].size();
  108.             res = collect;
  109.         } else if (total == min_weight && (int) all[u].size() < min_count) {
  110.             min_count = (int) all[u].size();
  111.             res = collect;
  112.         }
  113.     }
  114.     for (int i = 0; i < (int) res.size(); i++) {
  115.         if (i > 0) {
  116.             cout << "->";
  117.         }
  118.         cout << res[i];
  119.     }
  120.     cout << '\n';
  121.     return 0;
  122. }
  123.  
Add Comment
Please, Sign In to add comment