include iostream include vector using namespace std const int INF 100

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
using namespace std;
const int INF = 100;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int m;
cin >> m;
vector<int> w;
int temp3;
for (int i(0); i < m; ++i) {
cin >> temp3;
w.push_back(temp3);
}
int n;
cin >> n;
vector < vector < pair<int, int> > > g(n);
int temp, temp2;
for (int i(0); i < n; ++i) {
cin >> temp >> temp2;
g[temp - 1].push_back(make_pair(temp2 - 1, w[temp - 1]));
g[temp2 - 1].push_back(make_pair(temp - 1, w[temp2 - 1]));
}
int from, to;
from = 1;
int s = from - 1;
vector<int> d(n, INF), p(n);
d[s] = 0;
vector<char> u(n);
for (int i = 0; i<n; ++i) {
int v = -1;
for (int j = 0; j<n; ++j)
if (!u[j] && (v == -1 || d[j] < d[v]))
v = j;
if (d[v] == INF)
break;
u[v] = true;
for (size_t j = 0; j<g[v].size(); ++j) {
int to = g[v][j].first,
len = g[v][j].second;
if (d[v] + len < d[to]) {
d[to] = d[v] + len;
p[to] = v;
}
}
}
cout << (d[n - 1] == INF ? -1 : d[n - 1]);
}