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

Commit cdc1384

Browse files
committed
Test new approach
This is probably not the best way to do it
1 parent 4d7da44 commit cdc1384

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

dining.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
1
3+
1

src/dining.java

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ static int minDist(int dist[], boolean[] set1) {
1313
}
1414
static final int OFFSET = -1; // Offset by 1 for adjancey matirix because index begins at 0
1515
public static int[] root;
16+
public static int status = -1;
17+
public static int[] arr;
18+
public static int[] pasture;
1619
private static int[] dijkstra(int[][] graph1, int startVertex) {
20+
status = 0;
1721
int N = graph1.length;
1822
int[] dists = new int[N];
1923
boolean[] added = new boolean[N];
@@ -35,18 +39,42 @@ private static int[] dijkstra(int[][] graph1, int startVertex) {
3539
}
3640
added[nearestVertex] = true;
3741
for (int vertexIndex = 0; vertexIndex < N; vertexIndex++) {
42+
3843
int edgeDistance = graph1[nearestVertex][vertexIndex];
39-
if (edgeDistance > 0 && ((shortestDistance + edgeDistance) < dists[vertexIndex])) {
44+
// Used to be >
45+
if (edgeDistance != 0 && ((shortestDistance + edgeDistance) < dists[vertexIndex])) {
4046
parents[vertexIndex] = nearestVertex;
4147
dists[vertexIndex] = shortestDistance +
4248
edgeDistance;
49+
50+
}
51+
if(edgeDistance<0) {
52+
System.out.println("Info: "+nearestVertex+" "+vertexIndex);
53+
arr[nearestVertex-1] = 1;
54+
arr[vertexIndex-1] = 1;
55+
if(pasture[nearestVertex] > 0) {
56+
for(int j = 0; j < N; j++) {
57+
58+
graph1[nearestVertex][j] = 0;
59+
graph1[j][nearestVertex] = 0;
60+
61+
}
62+
}
63+
if(pasture[vertexIndex] > 0) {
64+
for(int j = 0; j < N; j++) {
65+
66+
graph1[j][vertexIndex] = 0;
67+
graph1[vertexIndex][j] = 0;
68+
69+
}
70+
}
71+
graph1[nearestVertex][vertexIndex] = 0;
72+
graph1[vertexIndex][nearestVertex] = 0;
4373
}
4474
}
4575
}
4676
root = parents;
4777
return dists;
48-
49-
5078
}
5179
public static void main(String[] args) throws IOException{
5280
BufferedReader f = new BufferedReader(new FileReader("dining.in"));
@@ -55,6 +83,8 @@ public static void main(String[] args) throws IOException{
5583
int M = Integer.parseInt(st.nextToken());
5684
int K = Integer.parseInt(st.nextToken());
5785
int[][] matrix = new int[N][N];
86+
arr = new int[N-1];
87+
pasture = new int[N];
5888
for(int i = 0; i < N; i++){
5989
//Arrays.fill(matrix[i], Integer.MAX_VALUE);
6090
}
@@ -64,8 +94,37 @@ public static void main(String[] args) throws IOException{
6494
matrix[x + OFFSET][y + OFFSET] = z;
6595
matrix[y + OFFSET][x + OFFSET] = z;
6696
}
67-
int[] out = dijkstra(matrix, N - 1);
68-
System.out.println(Arrays.toString(root));
69-
System.out.println(Arrays.toString(out));
97+
//System.out.println(Arrays.deepToString(matrix).replaceAll("],*", "],\n"));
98+
// Dijkstra Modification begins here
99+
for(int i = 0; i < K; i++) {
100+
st = new StringTokenizer(f.readLine());
101+
int x = Integer.parseInt(st.nextToken());
102+
int y = Integer.parseInt(st.nextToken());
103+
pasture[x-1] = y;
104+
x = x + OFFSET;
105+
for(int j = 0; j < N; j++) {
106+
if(matrix[j][x] != 0) {
107+
//System.out.println("Override 1 "+j+" "+x+" "+y);
108+
matrix[j][x] = matrix[j][x] - y;
109+
}
110+
if(matrix[x][j] != 0) {
111+
//System.out.println("Override 2 "+x+" "+j+" "+y);
112+
matrix[x][j] = matrix[x][j] - y;
113+
}
114+
}
115+
}
116+
// End modification
117+
//System.out.println("Modifacation Complete");
118+
//System.out.println(Arrays.deepToString(matrix).replaceAll("],*", "],\n"));
119+
int[] out = dijkstra(matrix, N-1);
120+
121+
//System.out.println(Arrays.toString(root));
122+
//System.out.println(Arrays.toString(out));
123+
PrintWriter pw = new PrintWriter(new FileWriter("dining.out"));
124+
for(int k:arr) {
125+
pw.println(k);
126+
}
127+
pw.close();
128+
70129
}
71130
}

0 commit comments

Comments
 (0)