Cod sursa(job #2113838)

Utilizator netfreeAndrei Muntean netfree Data 25 ianuarie 2018 09:29:42
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#define pii pair<int, int>
#include <bits/stdc++.h>

using namespace std;

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

const int N_MAX = 50000 + 5;
const int inf = 0x3f3f3f3f;
int n, m, a, b, c;

int cost[N_MAX];
vector<pii> vec[N_MAX];
priority_queue<pii, vector<pii>, greater<pii> > q;


void dijkstra(){
    cost[1] = 0;
    q.push({0,1});
    while(q.size()){
      int nod = q.top().second;
      q.pop();
      for(auto v : vec[nod]){
        int new_nod = v.first; int muchie_cost = v.second;
        if(cost[nod] + muchie_cost < cost[new_nod]){
          cost[new_nod] = cost[nod] + muchie_cost;
          q.push({cost[new_nod], new_nod});
        }
      }
    }
}

int main(){

  fin >> n >> m;
  while(m--){
    fin >> a >> b >> c;
    vec[a].push_back({b,c});
    vec[b].push_back({a,c});
  }

  //init_cost();
  memset(cost, inf, sizeof(cost));
  dijkstra();
  for(int i = 2; i<=n; ++i)
        fout << (cost[i] == inf ? 0 : cost[i]) << " ";
	return 0;
}

//Andrei Muntean, 2018