Академический Документы
Профессиональный Документы
Культура Документы
h>
int distance[1000];
int visit[1000];
int previous[1000];
int bykKota;
int bykEdge[1000];
struct graph {
int to;
int cost;
};
graph adjList[1000][11];
struct tdata {
int cost;
int node;
};
tdata heap[2000];
int count;
// Cek dulu apakah node kiri lebih kecil dari node sekarang
// 2*idx <= count untuk mengecek apakah kita punya anak kiri.
if (2*idx <= count && compare(heap[2*idx], curValue) < 0) {
curIdx = 2*idx;
curValue = heap[2*idx];
}
// lalu cek apakah node kanan lebih kecil dari node kiri dan node skrg
// 2*idx+1 <= count untuk mengecek apakah kita punya anak kanan.
if ( (2*idx+1) <= count && compare(heap[2*idx+1], curValue) < 0) {
curIdx = 2*idx+1;
curValue = heap[2*idx+1];
}
// gak perlu heapdown ke bawah lagi..
// kalau yang sekarang sudah yang paling kecil
if (curIdx == idx) {
return;
}
tdata extractMinimum() {
return rootValue;
}
// naik ke atas
curIdx = curIdx / 2;
}
// dijkstra O(n^2)
int dijkstra(int from, int to) {
distance[from] = 0;
insert(distance[from], from);
}
return distance[to];
int main() {
count = 0;
for(int i=0; i<1000; i++) {
bykEdge[i] = 0;
}
/*bykEdge[0] = 2;
bykEdge[1] = 2;
bykEdge[2] = 3;
bykEdge[3] = 3;
bykEdge[4] = 1;
bykEdge[5] = 1;
// adjMatrix[0][1] = 30;
adjList[0][0].cost = 30;
adjList[0][0].to = 1;
adjList[1][0].cost = 30;
adjList[1][0].to = 0;
// adjMatrix[0][2] = 10;
adjList[0][1].cost = 10;
adjList[0][1].to = 2;
adjList[2][0].cost = 10;
adjList[2][0].to = 0;
// adjMatrix[1][3] = 10;
adjList[1][1].cost = 10;
adjList[1][1].to = 3;
adjList[3][0].cost = 10;
adjList[3][0].to = 1;
// adjMatrix[2][3] = 50;
adjList[2][1].cost = 50;
adjList[2][1].to = 3;
adjList[3][1].cost = 50;
adjList[3][1].to = 2;
// adjMatrix[2][4] = 15;
adjList[2][2].cost = 15;
adjList[2][2].to = 4;
adjList[4][0].cost = 15;
adjList[4][0].to = 2;
// adjMatrix[3][5] = 17;
adjList[3][2].cost = 17;
adjList[3][2].to = 5;
adjList[5][0].cost = 17;
adjList[5][0].to = 3;
bykKota = 6;
*/
bykEdge[0] = 3;
bykEdge[1] = 4;
bykEdge[2] = 5;
bykEdge[3] = 3;
bykEdge[4] = 3;
bykEdge[5] = 3;
// adjMatrix[A][B] = 2;
adjList[0][0].cost = 2;
adjList[0][0].to = 1;
adjList[1][0].cost = 2;
adjList[1][0].to = 0;
// adjMatrix[A][C] = 1;
adjList[0][1].cost = 1;
adjList[0][1].to = 2;
adjList[2][0].cost = 1;
adjList[2][0].to = 0;
// adjMatrix[A][F] = 4;
adjList[0][2].cost = 4;
adjList[0][2].to = 5;
adjList[5][0].cost = 4;
adjList[5][0].to = 0;
// adjMatrix[B][C] = 5;
adjList[1][1].cost = 5;
adjList[1][1].to = 2;
adjList[2][1].cost = 5;
adjList[2][1].to = 1;
// adjMatrix[B][D] = 3;
adjList[1][2].cost = 3;
adjList[1][2].to = 3;
adjList[3][0].cost = 3;
adjList[3][0].to = 1;
// adjMatrix[B][E] = 4;
adjList[1][3].cost = 4;
adjList[1][3].to = 4;
adjList[4][0].cost = 4;
adjList[4][0].to = 1;
// adjMatrix [C][D] = 1
adjList[2][1].cost = 1;
adjList[2][1].to = 3;
adjList[3][1].cost = 1;
adjList[3][1].to = 1;
// adjMatrix [C][E] = 2
adjList[2][2].cost = 2;
adjList[2][2].to = 4;
adjList[4][1].cost = 2;
adjList[4][1].to = 2;
// adjMatrix [C][F] = 3
adjList[2][3].cost = 3;
adjList[2][3].to = 6;
adjList[5][1].cost = 3;
adjList[5][1].to = 2;
// adjMatrix [D][E] = 2
adjList[3][2].cost = 2;
adjList[3][2].to = 4;
adjList[4][2].cost = 2;
adjList[4][2].to = 3;
// adjMatrix [E][F] = 1
adjList[4][3].cost = 1;
adjList[4][3].to = 5;
adjList[5][2].cost = 1;
adjList[5][2].to = 3;
bykKota = 6;
int kota;
while(1){
printf("Masukkan Kota:");
scanf("%d",&kota);
printf("%d\n", dijkstra(0, kota));
tracePath(kota);
puts("");