Cod sursa(job #2330298)

Utilizator Cristi_ChiraChira Cristian Cristi_Chira Data 28 ianuarie 2019 10:55:06
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>
std :: ifstream fin("dijkstra.in");
#define inf 0x3f3f3f3f
std :: ofstream fout("dijkstra.out");
#define dm 50005
using namespace std;
int n, m;
struct str{
   int node;
   int cost;
   bool operator < ( const str &other)const
   {
       return cost > other.cost;
   }
};
vector <pair<int, int>> v[dm];
priority_queue <str> pq;
int road[dm];
void dijkstra()
{
    pq.push({1,0});
    road[1] = 0;
    while(!pq.empty())
    {
        int nodc = pq.top().node;
        int costc = pq.top().cost;
        pq.pop();
        if(costc!= road[nodc])continue;
        for(auto it = v[nodc].begin(); it != v[nodc].end(); it++)
        {
            int vecin = (*it).first;
            int costvecin = (*it).second;

            if(road[nodc] + costvecin < road[vecin])
            {
                road[vecin] = road[nodc] + costvecin;
                pq.push({vecin, road[vecin]});
                cout << road[vecin] << " ";
            }
        }
    }
}
int main()
{
    int x, y ,z;
    fin >> n >> m;
    memset(road, inf, sizeof(road));
    for(int i = 1; i <= m; i++)
    {
        fin >> x >> y >> z;
        v[x].push_back({y, z});
    }
    dijkstra();
    for(int i = 2; i <= n; i++)
    {
        if(road[i] != inf)
            fout << road[i] << " ";
        else fout << 0 << " ";
    }
    return 0;
}