1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

#include <algorithm>
#include <stdio.h>
#include <limits.h>
// Number of vertices in the graph
#define V 5
#define T 10
int src= 0;
int etichetta( int dist[],bool spt[])
{
for ( int i=0 ; i <= V; i++) //inizializzazione di dist
{
spt[i]= false;
dist[i]= INT_MAX;
dist[src]=0;
}
int S=0,G = V;
int min = dist[src];
int min_index;
while ( S <= V)
{
for ( int i =0; i <= V; i++)
{
if ( dist[i] <= min && spt[i]== false)
{
min_index =dist[i];
S += i;
G = i ;
spt[i]= true;
}
}
}
return min_index;
}
int printSolution(int dist[], int n)
{
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
void dijkstra( int C[T],int adj[V][V])
{
int dist[V];
int t[V];
for(int i=0; i<V; i++)
{
t[i]=0;
}
int pred[V];
for(int i=0; i< V; i++)
{
pred[i]=0;
}
bool spt[V];
int p;
int c = etichetta(dist,spt);
int a= pred[c];
t[c]=t[src]+t[a]+ adj[a][c];
if(t[c]<=T)
{
p=t[c];
}
for(int i=0; i<=V; i++)
{
if( adj[c][i]!= 0 && dist[i]> dist[c] + C[p])
{
spt[i]= true;
pred[i]=c;
dist[i] = dist[c] + C[p];
}
}
printSolution(dist,V);
}
// driver program to test above function
int main()
{
/* Let us create the example graph discussed above */
int adj[V][V] = {{0, 4, 3, 1, 0},
{0, 0, 2, 0, 0},
{0, 0, 0, 0, 3},
{0, 0, 4, 0, 2},
{0, 1, 0, 0, 0},
};
int C[T]= {1,2,3,4,1,2,8,1,1,1};
dijkstra(C,adj);
fflush(stdin);getchar();
return 0;
}
