Skip to content

Commit aba4479

Browse files
authored
Add files via upload
Simple djikstra example
1 parent 43d66c7 commit aba4479

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

teswisnu.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <iostream>
3+
using namespace std;
4+
#define inf 999
5+
#define n 5
6+
#define mem 1
7+
#define nonm 0
8+
9+
int main(){
10+
int bobot[n][n]={0,12,5,999,17,1,0,999,15,999,5,999,0,999,10,999,15,999,0,6,17,999,10,6,0};
11+
int s,t,pre[n];
12+
int dist[n],perm[n];
13+
int cur,i,k,dc,z;
14+
int smal,newd;
15+
cout<<"simpul asal(0-4):";
16+
cin>>s;
17+
cout<<"simpul tujuan (0-4):";
18+
cin>>t;
19+
20+
for(i=0;i<=4;i++){
21+
perm[i]=nonm;
22+
dist[i]=inf;
23+
}
24+
25+
perm[s]=mem;
26+
dist[s]=0;
27+
cur=s;
28+
while(cur!=t){
29+
smal=inf;
30+
dc=dist[cur];
31+
for(i=0;i<=4;i++){
32+
if(perm[i]==nonm){
33+
(newd=dc+bobot[cur][i]);
34+
if(newd<dist[i]){
35+
dist[i]=newd;
36+
pre[i]=cur;
37+
}
38+
if(dist[i]<smal){
39+
smal=dist[i];
40+
k=i;
41+
}
42+
}
43+
}
44+
cur=k;
45+
perm[cur]=mem;
46+
}
47+
cout<<endl<<endl;
48+
cout<<"graphnya adalah "<<dist[t];
49+
}

0 commit comments

Comments
 (0)