Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //#include <chrono>
- //#include <fstream>
- //#include <bits/stdc++.h>
- #include <algorithm>
- #include <vector>
- #include <list>
- #define mp make_pair
- #define pb push_back
- #define eb emplace_back
- #define f first
- #define s second
- #define sz(x) (int)x.size()
- #define all(x) begin(x), end(x)
- #define rall(x) rbegin(x), rend(x)
- using namespace std;
- typedef unsigned long long ull;
- typedef long long ll;
- typedef long double ld;
- typedef pair<int, int> pi;
- typedef pair<ll, ll> pl;
- const int INF = 2 * 1e9;
- int main() {
- /*ifstream cin("in.txt");
- ofstream cout("out.txt");
- auto start_time = chrono::high_resolution_clock::now();*/
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int n, m;
- cin >> n;
- vector<int> cost(n);
- for (int i = 0; i < n; i++) cin >> cost[i];
- cin >> m;
- vector<vector<bool>> vec(n, vector<bool>(n));
- for (int i = 0; i < m; i++) {
- int a, b; cin >> a >> b;
- vec[--a][--b] = true;
- vec[b][a] = true;
- }
- vector<int> d(n, INF);
- d[0] = 0;
- vector<bool> used(n);
- for (int i = 0; i < n; i++) {
- int v = -1;
- for (int j = 0; j < n; j++)
- if (!used[j] && (v == -1 || d[j] < d[v]))
- v = j;
- if (d[v] == INF) break;
- used[v] = true;
- for (int j = 0; j < n; j++)
- if (vec[v][j]) {
- d[j] = min(d[j], d[v] + cost[v]);
- for (int k = 0; k < n; k++)
- if (vec[j][k])
- d[k] = min(d[k], d[v] + 2*cost[v]);
- }
- }
- cout << (d[n - 1] == INF ? -1 : d[n-1]) << "\n";
- /*auto end_time = chrono::high_resolution_clock::now();
- chrono::duration<double> duration = end_time - start_time;
- cout << duration.count() << "\n";
- cin.close();
- cout.close();*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment