Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 88cfdca

Browse files
committed
Checking my algorthims
1 parent cf4a8ba commit 88cfdca

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

dining.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
4 5 1
2+
1 4 10
3+
2 1 20
4+
4 2 3
5+
2 3 5
6+
4 3 2
7+
2 7

dining.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import java.io.*;
22
import java.util.*;
33
public class dining {
4-
static final int V=9;
5-
static int minDistance(int dist[], boolean[] set1) {
4+
static int V;
5+
static int minDist(int dist[], boolean[] set1) {
66
int min = Integer.MAX_VALUE, min_index=-1; // Computers are too stupid to understand infinite
77
for (int v = 0; v < V; v++)
88
if (set1[v] == false && dist[v] <= min) {
@@ -11,21 +11,25 @@ static int minDistance(int dist[], boolean[] set1) {
1111
}
1212
return min_index;
1313
}
14-
final int OFFSET = -1; // Offset by 1 for adjancey matirix because index begins at 0
14+
static final int OFFSET = -1; // Offset by 1 for adjancey matirix because index begins at 0
15+
public static int[] root;
1516
static int[] dijkstra(int graph[][], int src) {
17+
V = graph.length;
1618
int dist[] = new int[V];
19+
root = new int[V];
1720
boolean set1[] = new boolean[V];
1821
for (int i = 0; i < V; i++) {
1922
dist[i] = Integer.MAX_VALUE;
2023
set1[i] = false;
2124
}
2225
dist[src] = 0;
2326
for (int count = 0; count < V-1; count++) {
24-
int u = minDistance(dist, set1);
27+
int u = minDist(dist, set1);
2528
set1[u] = true;
2629
for (int v = 0; v < V; v++)
2730
if (!set1[v] && graph[u][v]!=0 && dist[u] != Integer.MAX_VALUE && dist[u]+graph[u][v] < dist[v]){
2831
dist[v] = dist[u] + graph[u][v];
32+
root[v] = u;
2933
}
3034
}
3135
return dist;
@@ -36,6 +40,18 @@ public static void main(String[] args) throws IOException{
3640
int N = Integer.parseInt(st.nextToken());
3741
int M = Integer.parseInt(st.nextToken());
3842
int K = Integer.parseInt(st.nextToken());
39-
43+
int[][] matrix = new int[N][N];
44+
for(int i = 0; i < N; i++){
45+
//Arrays.fill(matrix[i], Integer.MAX_VALUE);
46+
}
47+
for(int i = 0; i < M; i ++) {
48+
st = new StringTokenizer(f.readLine());
49+
int x = Integer.parseInt(st.nextToken()),y = Integer.parseInt(st.nextToken()),z = Integer.parseInt(st.nextToken());
50+
matrix[x + OFFSET][y + OFFSET] = z;
51+
matrix[y + OFFSET][x + OFFSET] = z;
52+
}
53+
int[] out = dijkstra(matrix, N - 1);
54+
System.out.println(Arrays.toString(root));
55+
System.out.println(Arrays.toString(out));
4056
}
4157
}

0 commit comments

Comments
 (0)