Cod sursa(job #2350205)

Utilizator EclipseTepes Alexandru Eclipse Data 21 februarie 2019 10:16:00
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>
#include <vector>
#include <cstring>
#include <set>
#include <fstream>
#define dmax 10005
#define INF 10000009

using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

int n, m, x, y, w;
vector<pair<int, int> > graf[dmax];
int dist[dmax];
bool viz[dmax];

void Dijkstra() {
    dist[1] = 0;
    //viz[1] = true;
    multiset<pair<int, int> > ms;
    pair<int, int> pVerif;
    int newV, cost, cur_x, cur_c;
    ms.insert({0, 1});
    while (!ms.empty()) {
        pVerif = *ms.begin();
        ms.erase(ms.begin());
        cur_x = pVerif.second;
        cur_c = pVerif.first;
        if (viz[cur_x]) continue;
        viz[cur_x] = true;
        //cout << "hi";
        for (int i = 0; i < graf[cur_x].size(); i++) {
            newV = graf[cur_x][i].first;
            cost = graf[cur_x][i].second;
            if (dist[cur_x] + cost < dist[newV]) {
                dist[newV] = dist[cur_x] + cost;
                ms.insert({dist[newV], newV});
            }
        }
    }
}

int main()
{
    int i, j;
    fin >> n >> m;
    for (i = 1; i <= m; i++) {
        fin >> x >> y >> w;
        graf[x].push_back({y, w});
        graf[y].push_back({x, w});
    }
    for (i = 1; i <= n; i++) dist[i] = INF;
    Dijkstra();
    for (i = 2; i <= n; i++) {
        fout << dist[i] << ' ';
    }
    return 0;
}