#include <iostream>
#include <vector>
#include <queue>
#include <climits>

using namespace std;




int main() {
    int n, m;
    cin >> n >> m;

    vector<pair<int, int>> graph[11];

    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        graph[u].push_back({ v, w });
        graph[v].push_back({ u, w }); 
    }

    vector<int> min_cost(n + 1, INT_MAX);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;


    min_cost[1] = 0;
    pq.push({ 0, 1 }); // 비용 ,노드

    while (!pq.empty()) {
        int cost = pq.top().first;
        int current = pq.top().second;
        pq.pop();

        if (min_cost[current] < cost) continue;

        for (auto& edge : graph[current]) {
            int next = edge.first;
            int next_cost = cost + edge.second;

            if (next_cost < min_cost[next]) {
                min_cost[next] = next_cost;
                pq.push({ next_cost, next });
            }
        }
    }

    min_cost[n] == INT_MAX ? -1 : min_cost[n];

    cout << min_cost[n];

    return 0;
}

+ Recent posts