#include #include #include #include #include using namespace std; const long long INF = 1e18; struct Edge { int to; long long weight; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; if (!(cin >> n >> m)) return 0; vector p(n + 1); for (int i = 1; i <= n; ++i) { cin >> p[i]; } vector> adj(n + 1); vector in_degree(n + 1, 0); for (int i = 0; i < m; ++i) { int u, v; long long t; cin >> u >> v >> t; adj[u].push_back({v, t}); in_degree[v]++; } long long min_cost = INF; long long ways = 0; // N <= 2500, M <= 5000 なので、各始点からSPFA (Shortest Path Faster Algorithm) を回す for (int start = 1; start <= n; ++start) { vector dist(n + 1, INF); vector in_queue(n + 1, false); queue q; dist[start] = 0; q.push(start); in_queue[start] = true; while (!q.empty()) { int u = q.front(); q.pop(); in_queue[u] = false; for (const auto& edge : adj[u]) { if (dist[u] + edge.weight < dist[edge.to]) { dist[edge.to] = dist[u] + edge.weight; if (!in_queue[edge.to]) { q.push(edge.to); in_queue[edge.to] = true; } } } } // コストの最小値とその数を更新 for (int target = 1; target <= n; ++target) { if (start == target || dist[target] == INF) continue; long long current_cost = dist[target] + p[start] + p[target]; if (current_cost < min_cost) { min_cost = current_cost; ways = 1; } else if (current_cost == min_cost) { ways++; } } } if (min_cost == INF) { std::println("-1"); } else { std::println("{} {}", min_cost, ways); } return 0; }