Cod sursa(job #2269753)

Utilizator Cristi_ChiraChira Cristian Cristi_Chira Data 26 octombrie 2018 15:30:01
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
#define dm 50005
#define inf 0x3f3f3f3f
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, x, y, a;
struct str{
    int node, cost;
    bool operator < (const str &other) const {
        return cost > other.cost;
    }
};
vector <str> v[dm];
priority_queue <str> pq;
int drum[dm];
void dijkstra(int start)
{
    pq.push({start, 0});
    drum[start] = 0;
    while(!pq.empty())
    {
        str x = pq.top();
        pq.pop();
        for(auto it:v[x.node])
        {
            int nbr = it.node;
            int cost = it.cost;
            if(drum[x.node] + cost < drum[nbr])
            {
                drum[nbr] = drum[x.node] + cost;
                pq.push({nbr, drum[nbr]});
            }
        }
    }
}
int main()
{
    fin >> n >> m;
    memset(drum, inf, sizeof(drum));
    for(int i = 1; i <= m; i++)
    {
        fin >> x >> y >> a;
        v[x].push_back({y, a});
    }
    dijkstra(1);
    for(int i = 2; i <= n; i++)
        if(drum[i]!=inf)
        fout << drum[i] << " ";
        else fout << 0 << " ";
    return 0;
}