Cod sursa(job #1453528)

Utilizator rangerChihai Mihai ranger Data 23 iunie 2015 19:52:24
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include<fstream>
#include<set>
#include<vector>

using namespace std;


ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");

const int N = 100003;
const int inf = 1000000000;
int n,m,x,y,z;
set< pair <int,int> > s;
vector<int> d(N,inf);


struct nod
{
    int info, cost;
    nod * next;
};


void add(int x, int cost, nod * &y)
{
    nod * p = new nod;
    p->info = x;
    p->cost = cost;
    p->next = y;
    y = p;
}

nod * lda[N];

int main()
{
    cin>>n>>m;
    while (m--)
    {
        cin>>x>>y>>z;
        add(x,z,lda[y]);
        add(y,z,lda[x]);
    }
      d[1]=0;
     s.insert(make_pair(d[1],1));

     while (s.size())
     {
         int v = s.begin()->second;
         s.erase(s.begin());

         for (nod * p=lda[v];p;p=p->next)
         {
             int to = p->info, cost = p->cost;
             if (d[to] > d[v] + cost)
             {
                 s.erase(make_pair(d[to],to));
                 d[to] = d[v] + cost;
                 s.insert(make_pair(d[to],to));
             }
         }
     }
     for (int i=2;i<=n;i++)
        cout << (d[i] == (int)1e9  ? 0 : d[i]) << " ";
     return 0;
}