2020-02-02 19:00:50 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#define MAX 20
|
|
|
|
#define INF 999
|
|
|
|
|
|
|
|
int mat[MAX][MAX];
|
|
|
|
int V;
|
|
|
|
|
|
|
|
int dist[MAX];
|
|
|
|
|
|
|
|
int q[MAX];
|
|
|
|
int qp = 0;
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
void enqueue(int v) { q[qp++] = v; }
|
2020-02-02 19:00:50 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int cf(void *a, void *b)
|
|
|
|
{
|
|
|
|
int *x = (int *)a;
|
|
|
|
int *y = (int *)b;
|
|
|
|
return *y - *x;
|
2020-02-02 19:00:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int dequeue()
|
|
|
|
{
|
|
|
|
qsort(q, qp, sizeof(int), cf);
|
|
|
|
return q[--qp];
|
2020-02-02 19:00:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int queue_has_something() { return (qp > 0); }
|
2020-02-02 19:00:50 +03:00
|
|
|
|
|
|
|
int visited[MAX];
|
|
|
|
int vp = 0;
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
void dijkstra(int s)
|
|
|
|
{
|
|
|
|
dist[s] = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < V; ++i)
|
|
|
|
{
|
|
|
|
if (i != s)
|
|
|
|
{
|
|
|
|
dist[i] = INF;
|
|
|
|
}
|
|
|
|
enqueue(i);
|
|
|
|
}
|
|
|
|
while (queue_has_something())
|
|
|
|
{
|
|
|
|
int u = dequeue();
|
|
|
|
visited[vp++] = u;
|
|
|
|
for (i = 0; i < V; ++i)
|
|
|
|
{
|
|
|
|
if (mat[u][i])
|
|
|
|
{
|
|
|
|
if (dist[i] > dist[u] + mat[u][i])
|
|
|
|
{
|
|
|
|
dist[i] = dist[u] + mat[u][i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 19:00:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int main(int argc, char const *argv[])
|
|
|
|
{
|
|
|
|
printf("Enter the number of vertices: ");
|
|
|
|
scanf(" %d", &V);
|
|
|
|
printf("Enter the adj matrix: ");
|
|
|
|
int i, j;
|
|
|
|
for (i = 0; i < V; ++i)
|
|
|
|
{
|
|
|
|
for (j = 0; j < V; ++j)
|
|
|
|
{
|
|
|
|
scanf(" %d", &mat[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dijkstra(0);
|
2020-02-02 19:00:50 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("\nNode\tDist\n");
|
|
|
|
for (i = 0; i < V; ++i)
|
|
|
|
{
|
|
|
|
printf("%d\t%d\n", i, dist[i]);
|
|
|
|
}
|
2020-02-02 19:00:50 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
return 0;
|
2020-02-02 19:00:50 +03:00
|
|
|
}
|