Cod sursa(job #3238186)

Utilizator GabrielPopescu21Silitra Gabriel - Ilie GabrielPopescu21 Data 23 iulie 2024 10:56:11
Problema Car Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <bits/stdc++.h>
using namespace std;

const int MAX = 36005;
vector<pair<int,int>> graph[MAX];
vector<int> dist;
bitset<MAX> visited;
int g, ans;

void dijkstra()
{
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
    q.push({0, 1});
    dist[1] = 0;

    while (!q.empty())
    {
        pair<int,int> node = q.top();
        q.pop();
        if (!visited[node.second])
        {
            for (pair<int,int> next : graph[node.second])
            {
                if (dist[next.first] > dist[node.second] + next.second)
                {
                    dist[next.first] = dist[node.second] + next.second;
                    q.push({dist[next.first], next.first});
                }
            }
            visited[node.second] = 1;
        }
    }
}

int main()
{
    ifstream cin("car.in");
    ofstream cout("car.out");
    int n, m;
    cin >> n >> m >> g;
    dist.assign(n+1, INT_MAX);

    for (int i = 1; i <= m; ++i)
    {
        int x, y, cost;
        cin >> x >> y >> cost;

        if (cost < g)
        {
            graph[x].push_back({y, 1});
            graph[y].push_back({x, 1});
        }
        else
        {
            graph[x].push_back({y, 0});
            graph[y].push_back({x, 0});
        }
    }

    dijkstra();
    if (dist[n] == INT_MAX)
        cout << -1;
    else
        cout << dist[n];

    return 0;
}