Cod sursa(job #3313639)

Utilizator CimpoesuFabianCimpoesu Fabian George CimpoesuFabian Data 5 octombrie 2025 17:49:59
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

int n, m, cost[50001], viz[50001];
vector <pair<int, int>> G[250001];
priority_queue<pair<int, int>> q;

void Dijkstra(int start)
{
    int i, curr;
    for (i = 1 ; i <= n ; i++)
        cost[i] = 2e9;
    cost[start] = 0;
    q.push({0, start});
    while (!q.empty())
    {
        curr = q.top().second;
        q.pop();
        if (viz[curr] == 0)
        {
            for (auto e : G[curr])
            {
                i = e.first;
                if (cost[i] > cost[curr] + e.second)
                {
                    cost[i] = cost[curr] + e.second;
                    q.push({-cost[i], i});
                }
            }
        }
    }
}

int main()
{
    int i, x, y, c;
    fin >> n >> m;
    for (i = 1 ; i <= m ; i++)
    {
        fin >> x >> y >> c;
        G[x].push_back({y, c});
    }
    Dijkstra(1);
    for (i = 2 ; i <= n ; i++)
    {
        if (cost[i] == 2e9)
            fout << 0 << " ";
        else
            fout << cost[i] << " ";
    }
}