Cod sursa(job #1237069)

Utilizator bogdanpaunFMI Paun Bogdan Gabriel bogdanpaun Data 3 octombrie 2014 02:26:01
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <cstdio>
using namespace std;
#define N 36005
#define INF 1<<30
int n , m , k,i   ,x,y,z   ,vf        ;

int cost[N],fort_corespunzator[N];
//pair<int,int> stiva[1000000];
int stiva[1000000];
int h;
struct str{
    int nod , val;
    str *next;
};

str *v[36005],*tmp;

inline void add(int &x, int &y, int &cost){
    tmp = new str;
    tmp->nod = y;
    tmp->val = cost;
    tmp->next = v[x];
    v[x] = tmp;

}


int main()
{
    FILE  *fin =fopen("dijkstra.in" , "r"),
          *fout=fopen("dijkstra.out", "w");

    fscanf(fin, "%d %d\n", &n, &m );
    for(i=1; i<=n; ++i) cost[i] = INF;
    for(i=1; i<=k; ++i){
        fscanf(fin, "%d ", &x);
        stiva[++h] = x;
        cost[x] = 0;
        fort_corespunzator[x] = x;
    }
    stiva[++h] = 1;
    cost[1] = 0;
    fort_corespunzator[1] = 1;
    for(i=1; i<=m; ++i){
        fscanf(fin, "%d %d %d", &x, &y, &z);
        add(x, y, z);
        add(y, x, z);
    }
    i = 0;

    //pair<int,int> tmp;
    while( ++i <= h ){
        tmp = v[ stiva[i] ];
        while( tmp ){
            if( cost[ tmp->nod ] > cost[ stiva[i] ] + tmp->val ){
                cost[ tmp->nod ] = cost[ stiva[i] ] + tmp->val;
                fort_corespunzator[tmp->nod] = fort_corespunzator[stiva[i]];
                stiva[++h] =  tmp->nod ;
            }
            tmp = tmp->next;
        }

    }
    for(i=2; i<=n; ++i)
            fprintf(fout,"%d ", cost[i] );




    return 0;
}