Cod sursa(job #2418135)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 3 mai 2019 19:41:52
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
void read();
int n, start, dist[105];
priority_queue<pair<int,int>, vector<pair<int,int>>, less<pair<int,int>>>q;
vector<pair<int, int>> v[105];
int main()
{
    read();
    for(int i=1; i<=n; i++)
        dist[i] = INT_MAX/2-1;
    dist[start] = 0;
    q.push({0, start});
    while(!q.empty())
    {
        int cur = q.top().second;
        q.pop();
        for(auto it : v[cur])
            if(dist[cur] + it.first < dist[it.second])
            {
                dist[it.second] = dist[cur] + it.first;
                q.push({dist[it.second], it.second});
            }
    }
    for(int i=1; i<=n; i++)
        if(dist[i]!=INT_MAX/2-1)
            fout << dist[i] << ' ';
        else
            fout << -1 << ' ';
    return 0;
}
void read()
{
    fin >> n >> start;
    int x, y, cost;
    while(fin >> x >> y >> cost)
        v[x].push_back({cost, y});
}