-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.c
138 lines (123 loc) · 1.74 KB
/
DFS.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdio.h>
#include <stdlib.h>
int source, dest, V, E, visited[20], G[20][20], cost = 0;
int found = 0;
void DFS(int i)
{
int j;
visited[i] = 1;
printf("%d -> ", i);
for (j = 1; j <= V; j++)
{
// printf("j: %d\n",j);
if (G[i][j] != 0 && visited[j] == 0)
{
cost = cost + G[i][j];
// printf("j yang masuk %d\n",j);
if (j != dest)
{
if (j != V)
{
DFS(j);
}
else
{
printf("%d\n", j);
cost = 0;
DFS(source);
}
}
else
{
found = 1;
break;
}
}
if (found == 1)
{
break;
}
}
}
int main()
{
int i, j, v1, v2, c;
printf("Minimum cost\n");
printf("Input total vertices: ");
scanf("%d", &V);
// Ubah isi Graph menjadi 0
for (i = 0; i <= V; i++)
{
for (j = 0; j <= V; j++)
{
G[i][j] = 0;
}
}
printf("Input total edges: ");
scanf("%d", &E);
for (i = 0; i < E; i++)
{
printf("Enter the edges(V1 V2 Cost) : ");
scanf("%d %d %d", &v1, &v2, &c);
G[v1][v2] = c;
}
printf("\nAdjacency Matrix\n");
for (i = 1; i <= V; i++)
{
for (j = 1; j <= V; j++)
{
printf(" %d ", G[i][j]);
}
printf("\n");
}
printf("\nWeight of every edges\n");
for (i = 1; i <= V; i++)
{
for (j = 1; j <= V; j++)
{
if (G[i][j] != 0)
{
printf(" %d -> %d : %d\n", i, j, G[i][j]);
}
}
}
printf("\nEnter the source: ");
scanf("%d", &source);
printf("Enter the destination: ");
scanf("%d", &dest);
dest = dest;
if (source > V || dest > V)
{
printf("Wrong input\n");
}
else
{
DFS(source);
if (found == 1)
{
printf("%d Cost: %d\n", dest, cost);
}
else
{
printf("\nThere's no way\n");
}
}
return 0;
}
/* Contoh testcase
10
11
1 2 2
1 3 3
2 4 4
2 5 5
3 6 6
3 7 7
4 8 8
5 9 9
6 10 1
8 9 9
9 10 1
2
10
*/