HasanRasulov

cheapest_parking_slot.cpp

Mar 10th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. /*
  2. There is a parking facility which is in the form of a graph having N nodes and M edges. The graph
  3. does not have self loops or multiple edges.
  4. Each node represents a parking slot and has a capacity of vehicles it can hold. Each edge
  5. has a weight w representing we can reach from node u to node v incurring a cost of w units. All
  6. parking slots have a parking fee F per vehicle which is same for all slots. There are K identical
  7. vehicles entering the parking facility, each vehicle enumerated with a distinct number from 1 to K.
  8. The vehicles enter in their natural order, that is, vehicle number 1 enters, then vehicle number 2,
  9. then 3 and so on till vehicle number K.
  10. For each vehicle, you have to print the minimum total cost that is incurred on the vehicle owner.
  11. Here, total cost includes cost of the path taken to reach the parking slot and parking fee of the
  12. slot.
  13. It is guaranteed that you can reach any slot from any other slot.
  14. All vehicles entering the parking facility enter from the parking slot 1.
  15. Input Format:
  16. The first line consists of three integers N, M and F, denoting number of nodes, number of edges and
  17. parking fee respectively.
  18. The second line consists of N space separated integers denoting the parking capacity of each
  19. parking slot. This array is represented by C[].
  20. Following M lines contain three space separated integers each:
  21. and w, denoting we can reach from node u to node v incurring a cost of w units.
  22. The last line of input contains an integer K.
  23. Output Format:
  24. Print K space separated integers denoting answer for each vehicle.
  25. integer in the space separated integers denotes answer for vehicle number.
  26. If it is not possible to park a vehicle , print for that vehicle.
  27.  
  28. */
  29.  
  30. #include <map>
  31. #include <vector>
  32. #include <utility>
  33. #include <queue>
  34. #include <set>
  35. #include <iostream>
  36. #include <algorithm>
  37. #include <climits>
  38.  
  39. std::vector<std::vector<std::pair<double, double>>> adj;
  40. std::vector<long long> d, c;
  41.  
  42.  
  43. void dijktras(long long s)
  44. {
  45.     d[s] = 0;
  46.     std::priority_queue<std::pair<double, double>, std::vector<std::pair<double, double>>,
  47.                         std::greater<std::pair<double, double>>> pq;
  48.  
  49.     pq.push({0, s});
  50.     while (!pq.empty())
  51.     {
  52.         long long v = pq.top().second;
  53.         long long d_v = pq.top().first;
  54.         pq.pop();
  55.         if (d[v] != d_v)
  56.             continue;
  57.         for (auto &edge : adj[v])
  58.         {
  59.             long long len = edge.second;
  60.             long long to = edge.first;
  61.             if (d[to] > d[v] + len)
  62.             {
  63.                 d[to] = d[v] + len;
  64.                 pq.push({d[to], to});
  65.             }
  66.         }
  67.     }
  68. }
  69. int main()
  70. {
  71.     long long n, m, f;
  72.     std::cin >> n >> m >> f;
  73.     adj.resize(n + 1);
  74.     d.assign(n + 1, LONG_LONG_MAX);
  75.     c.resize(n + 1);
  76.     for (size_t i = 1; i <= n; i++)
  77.         std::cin >> c[i];
  78.     while (m--)
  79.     {
  80.         long long u, v, w;
  81.         std::cin >> u >> v >> w;
  82.         adj[u].push_back({v, w});
  83.         adj[v].push_back({u, w});
  84.     }
  85.     long long k;
  86.     std::cin >> k;
  87.     dijktras(1);
  88.     std::multiset<long long> ml;
  89.     std::vector<std::pair<long long, long long>> v;
  90.     for (size_t i = 1; i <= n; i++)
  91.         v.push_back({d[i], i});
  92.     std::sort(v.begin(), v.end());
  93.     for (size_t i = 0; i < v.size(); i++)
  94.     {
  95.         int u = v[i].second;
  96.         for (int j = 0; j < c[u]; j++)
  97.         {
  98.             ml.insert(v[i].first);
  99.             if (ml.size() == k)
  100.                 break;
  101.         }
  102.         if (ml.size() == k)
  103.             break;
  104.     }
  105.     for (auto &u : ml)
  106.         std::cout << u + f << " ";
  107.     if (k > ml.size())
  108.         for (size_t i = 0; i < k - ml.size(); i++)
  109.             std::cout << -1 << " ";
  110.     return EXIT_SUCCESS;
  111. }
Add Comment
Please, Sign In to add comment