Cod sursa(job #1688146)

Utilizator TimoteiCopaciu Timotei Timotei Data 13 aprilie 2016 12:01:56
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>
#include <bitset>
#define INF 1<<30
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
bitset <50005> viz;
int n, m, costMin[50005], y1, y2, node, new_node;
struct element{
  int vecin, cost;
}x;
vector <element> v[50005];
queue <int> C;
void read(){
 f >> n >> m;
 for(int i = 1; i <= m; ++i){
    f >> y1 >> y2 >> x.cost;
    x.vecin = y1;
    v[y2].push_back(x);
    x.vecin = y2;
    v[y1].push_back(x);
 }
}
void bfs(int i){
  node = i;
  viz[i] = 1;
  C.push(node);
  while(!C.empty()){
    node = C.front();
    for(int i = 0; i < v[node].size(); ++i){
        new_node = v[node][i].vecin;
        if(costMin[node] + v[node][i].cost <= costMin[new_node]){
        costMin[new_node] = costMin[node] + v[node][i].cost;
        if(!viz[new_node]) {
                C.push(new_node);
                viz[new_node] = 1;
        }
      }
    }
   viz[node] = 0;
   C.pop();
  }
}
void write(){
  for(int i = 2; i <= n; ++i)
    g << costMin[i] << ' ';
}
int main()
{
    read();
    for(int i = 2; i <= n; ++i)
        costMin[i] = INF;
    bfs(1);
    write();
    return 0;
}