Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <string.h>
- #include <cstring>
- #include <math.h>
- #include <cmath>
- #include <vector>
- #include <utility>
- #include <cstdlib>
- #include <deque>
- #include <queue>
- #include <iomanip>
- #include <stack>
- #include <map>
- #include <set>
- #include <cmath>
- #include <fstream>
- #include <list>
- using namespace std;
- typedef long long ll;
- const ll INF = 1e18;
- ll n, s = 0, f, b[220], m;
- vector <vector<pair<ll, ll> > > g(120);
- vector<ll> d(120, INF), p(120);
- set <pair<ll, ll> > q;
- int main() {
- cin >> n;
- f = n - 1;
- for (int i = 0; i < n; i++)
- cin >> b[i];
- cin >> m;
- for (int i = 0; i < m; i++)
- {
- int t, y;
- cin >> t >> y;
- t--; y--;
- g[t].push_back(make_pair(y, b[t]));
- g[y].push_back(make_pair(t, b[y]));
- }
- d[s] = 0;
- q.insert(make_pair(d[s], s));
- while (!q.empty()) {
- int v = q.begin()->second;
- q.erase(q.begin());
- for (int j = 0; j < g[v].size(); ++j) {
- int to = g[v][j].first,
- len = g[v][j].second;
- if (d[v] + len < d[to]) {
- q.erase(make_pair(d[to], to));
- d[to] = d[v] + len;
- p[to] = v;
- q.insert(make_pair(d[to], to));
- }
- }
- }
- if(d[f] == INF)return cout << -1, 0;
- else cout << d[f];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement