Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4.  
  5. using namespace std;
  6.  
  7. long long dijkstra(int s, int n, vector<long long> &station_prices, vector<vector<pair<int, long long>>> &graph) {
  8.     vector<long long> d;
  9.     d.assign(n, INT64_MAX / 10);
  10.     long long initial_price = station_prices[s];
  11.     auto copy_edges_from_s = graph[s];
  12.     for (int u = 0; u < n; ++u) {
  13.         if (u != s) {
  14.             graph[s].emplace_back(u, station_prices[u]);
  15.         }
  16.     }
  17.     d[s] = 0;
  18.     set<pair<long long, int>> queue;
  19.     queue.emplace(0, s);
  20.     while (!queue.empty()) {
  21.         int v = queue.begin()->second;
  22.         queue.erase(queue.begin());
  23.         for (pair<int, long long> edge : graph[v]) {
  24.             int u = edge.first;
  25.             long long weight = edge.second;
  26.             if (d[v] + weight < d[u]) {
  27.                 queue.erase({d[u], u});
  28.                 d[u] = d[v] + weight;
  29.                 queue.insert({d[u], u});
  30.             }
  31.         }
  32.     }
  33.     for (int price : d) {
  34.         initial_price += price;
  35.     }
  36.  
  37.     graph[s] = copy_edges_from_s;
  38.  
  39.     return initial_price;
  40. }
  41.  
  42.  
  43. int main() {
  44.     int n, k;
  45.     cin >> n;
  46.     vector<long long> station_prices;
  47.     for (int i = 0; i < n; ++i) {
  48.         long long price;
  49.         cin >> price;
  50.         station_prices.push_back(price);
  51.     }
  52.     cin >> k;
  53.     vector<vector<pair<int, long long>>> graph;
  54.     graph.resize(n);
  55.     for (int i = 0; i < k; ++i) {
  56.         int first, second, price;
  57.         cin >> first >> second >> price;
  58.         --first, --second;
  59.         graph[first].emplace_back(second, price);
  60.         graph[second].emplace_back(first, price);
  61.     }
  62.  
  63.     long long ans = dijkstra(0, n, station_prices, graph);
  64.     for (int v = 1; v < n; ++v) {
  65.         ans = min(ans, dijkstra(v, n, station_prices, graph));
  66.     }
  67.  
  68.     cout << ans;
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement