Cod sursa(job #1558069)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 28 decembrie 2015 17:46:06
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <cstdio>

#define NMax 2005
#define INT_MAX 1000000000
using namespace std;

int n,m,x,y,c;
short int a[NMax][NMax];

int gaseste_nod(bool vizitat[],int dist[]){
    int minim = INT_MAX,gasit;

    for(int i = 1; i <= n; i++){
        if(!vizitat[i] && dist[i] <= minim){
            minim = dist[i];
            gasit = i;
        }
    }
    return gasit;
}

void print(int dist[]){
    for(int i = 2; i <= n; i++){
        printf("%d ",dist[i]);
    }
}
void dijkstra(short int a[NMax][NMax],int x0){
    bool vizitat[NMax];

    int dist[NMax];

    for(int i = 1; i <= n; i++){
        vizitat[i] = 0;
        dist[i] = INT_MAX;
    }
    dist[x0] = 0;

    for(int i = 1; i <= n; i++){
        int curent = gaseste_nod(vizitat,dist);

        vizitat[curent] = 1;

        for(int j = 1; j <= n; j++){
            if(!vizitat[j] && dist[curent] != INT_MAX && a[curent][j] &&
               dist[curent] + a[curent][j] < dist[j])
                dist[j] = dist[curent] + a[curent][j];
        }
    }

    print(dist);
}
int main()
{
    freopen("dijkstra.in","r",stdin);
    freopen("dijkstra.out","w",stdout);
    scanf("%d%d",&n,&m);
   // printf("%f",1.0*(sizeof(a)) /1024/1024);
    for(int i = 1; i <= m; i++){
        scanf("%d%d%d",&x,&y,&c);
        a[x][y] = c;
        a[y][x] = c;
    }
    dijkstra(a,1);
    return 0;
}