Raslboyy

113213

Nov 9th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. //#include <chrono>
  3. //#include <fstream>
  4. //#include <bits/stdc++.h>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <list>
  8.  
  9. #define mp make_pair
  10. #define pb push_back
  11. #define eb emplace_back
  12. #define f first
  13. #define s second
  14. #define sz(x) (int)x.size()
  15. #define all(x) begin(x), end(x)
  16. #define rall(x) rbegin(x), rend(x)
  17.  
  18. using namespace std;
  19.  
  20. typedef unsigned long long ull;
  21. typedef long long ll;
  22. typedef long double ld;
  23. typedef pair<int, int> pi;
  24. typedef pair<ll, ll> pl;
  25.  
  26. const int INF = 2 * 1e9;
  27.  
  28. int main() {
  29.     /*ifstream cin("in.txt");
  30.     ofstream cout("out.txt");
  31.     auto start_time = chrono::high_resolution_clock::now();*/
  32.  
  33.     ios::sync_with_stdio(false);
  34.     cin.tie(0);
  35.     cout.tie(0);
  36.  
  37.     int n, m;
  38.     cin >> n;
  39.     vector<int> cost(n);
  40.     for (int i = 0; i < n; i++) cin >> cost[i];
  41.     cin >> m;
  42.     vector<vector<bool>> vec(n, vector<bool>(n));
  43.     for (int i = 0; i < m; i++) {
  44.         int a, b; cin >> a >> b;
  45.         vec[--a][--b] = true;
  46.         vec[b][a] = true;
  47.     }
  48.     vector<int> d(n, INF);
  49.     d[0] = 0;
  50.     vector<bool> used(n);
  51.     for (int i = 0; i < n; i++) {
  52.         int v = -1;
  53.         for (int j = 0; j < n; j++)
  54.             if (!used[j] && (v == -1 || d[j] < d[v]))
  55.                 v = j;
  56.         if (d[v] == INF) break;
  57.  
  58.         used[v] = true;
  59.         for (int j = 0; j < n; j++)
  60.             if (vec[v][j]) {
  61.                 d[j] = min(d[j], d[v] + cost[v]);
  62.                 for (int k = 0; k < n; k++)
  63.                     if (vec[j][k])
  64.                         d[k] = min(d[k], d[v] + 2*cost[v]);
  65.             }
  66.     }
  67.     cout << (d[n - 1] == INF ? -1 : d[n-1]) << "\n";
  68.    
  69.     /*auto end_time = chrono::high_resolution_clock::now();
  70.     chrono::duration<double> duration = end_time - start_time;
  71.     cout << duration.count() << "\n";
  72.     cin.close();
  73.     cout.close();*/
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment