Advertisement
Raslboyy

113213v2

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