#include #include #include #include #include using namespace std; struct Node { Node(int distance, int node) : distance(distance), node(node) {} int distance; int node; }; struct DistanceGreater : binary_function < Node, Node, bool > { bool operator()(const Node& lhs, const Node& rhs) const { return lhs.distance > rhs.distance; } }; int shortest(const vector >& costs, size_t start, size_t end) { size_t n = costs.size(); vector distance(n, numeric_limits::max()); distance[start] = 0; priority_queue, DistanceGreater> undetermined; undetermined.push(Node(distance[start], start)); vector determined(n, false); while (!undetermined.empty()) { Node node = undetermined.top(); undetermined.pop(); determined[node.node] = true; for (size_t adjNode = 0; adjNode < n; ++adjNode) { if (determined[adjNode]) { continue; } int cost = costs[node.node][adjNode]; if (cost == numeric_limits::max()) { continue; } int newDistance = distance[node.node] + cost; if (newDistance < distance[adjNode]) { distance[adjNode] = newDistance; undetermined.push(Node(newDistance, adjNode)); } } } return distance[end]; } int main() { int n; cin >> n; vector s(n); for (int i = 0; i < n; ++i) { cin >> s[i]; } vector > costs(n, vector(n, numeric_limits::max())); int m; cin >> m; for (int i = 0; i < m; ++i) { int a, b, c; cin >> a >> b >> c; costs[a][b] = c; costs[b][a] = c; } int minCost = numeric_limits::max(); for (int stay1 = 1; stay1 <= n - 2; ++stay1) { for (int stay2 = 1; stay2 <= n - 2; ++stay2) { if (stay1 == stay2) { continue; } int cost = s[stay1] + s[stay2]; cost += shortest(costs, 0, stay1); cost += shortest(costs, stay1, stay2); cost += shortest(costs, stay2, n - 1); minCost = min(minCost, cost); } } cout << minCost << endl; return 0; }