Cod sursa(job #735340)

Utilizator visanrVisan Radu visanr Data 16 aprilie 2012 10:23:24
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

#define nmax 50010
#define muchie pair<long, pair<long,long> >
#define cost first
#define start second.first
#define end second.second
struct other
{
       long endd,costt;
};
struct edge
{
       vector<other> neighbour;
};
edge nodes[nmax];
vector<long> used(nmax);
vector<long> dist(nmax);

long n,m,a,b,c;
priority_queue<muchie, vector<muchie>, greater<muchie> >q;

void read_input()
{
     scanf("%ld %ld", &n,&m);
     for(int i=0;i<m;i++)
     {
             scanf("%ld %ld %ld", &a,&b,&c);
             other New;
             New.endd=b;
             New.costt=c;
             nodes[a].neighbour.push_back(New);
     }
}

void Dijkstra(long st)
{
     dist[st]=0;
     int nr=1;
     do
     {
         used[st]=1;
         for(int i=0;i<nodes[st].neighbour.size();i++)
         {
                 if(used[nodes[st].neighbour[i].endd]==0)
                 {
                          q.push(make_pair(nodes[st].neighbour[i].costt,make_pair(st,nodes[st].neighbour[i].endd)));
                 }
         }
         muchie old=q.top();
         q.pop();
         dist[old.end]=dist[old.start]+old.cost;
         st=old.end;
         nr++;
     }while(nr<n);
     for(int i=2;i<=n;i++) printf("%ld ", dist[i]);
     printf("\n");
}

int main()
{
    freopen("dijkstra.in","r",stdin);
    freopen("dijkstra.out","w",stdout);
    //int i;
    read_input();
    Dijkstra(1);
    //scanf("%i", &i);
    return 0;
}