This repository was archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdining.java
More file actions
130 lines (122 loc) · 4.69 KB
/
dining.java
File metadata and controls
130 lines (122 loc) · 4.69 KB
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
import java.io.*;
import java.util.*;
public class dining {
static int V;
static int minDist(int dist[], boolean[] set1) {
int min = Integer.MAX_VALUE, min_index=-1; // Computers are too stupid to understand infinite
for (int v = 0; v < V; v++)
if (set1[v] == false && dist[v] <= min) {
min = dist[v];
min_index = v;
}
return min_index;
}
static final int OFFSET = -1; // Offset by 1 for adjancey matirix because index begins at 0
public static int[] root;
public static int status = -1;
public static int[] arr;
public static int[] pasture;
private static int[] dijkstra(int[][] graph1, int startVertex) {
status = 0;
int N = graph1.length;
int[] dists = new int[N];
boolean[] added = new boolean[N];
for (int vertexIndex = 0; vertexIndex < N; vertexIndex++) {
dists[vertexIndex] = Integer.MAX_VALUE;
added[vertexIndex] = false;
}
dists[startVertex] = 0;
int[] parents = new int[N];
parents[startVertex] = -1;
for (int i = 1; i < N; i++) {
int nearestVertex = -1;
int shortestDistance = Integer.MAX_VALUE;
for (int vertexIndex = 0; vertexIndex < N; vertexIndex++) {
if (!added[vertexIndex] && dists[vertexIndex] < shortestDistance) {
nearestVertex = vertexIndex;
shortestDistance = dists[vertexIndex];
}
}
added[nearestVertex] = true;
for (int vertexIndex = 0; vertexIndex < N; vertexIndex++) {
int edgeDistance = graph1[nearestVertex][vertexIndex];
// Used to be >
if (edgeDistance != 0 && ((shortestDistance + edgeDistance) < dists[vertexIndex])) {
parents[vertexIndex] = nearestVertex;
dists[vertexIndex] = shortestDistance +
edgeDistance;
}
if(edgeDistance<0) {
System.out.println("Info: "+nearestVertex+" "+vertexIndex);
arr[nearestVertex-1] = 1;
arr[vertexIndex-1] = 1;
if(pasture[nearestVertex] > 0) {
for(int j = 0; j < N; j++) {
graph1[nearestVertex][j] = 0;
graph1[j][nearestVertex] = 0;
}
}
if(pasture[vertexIndex] > 0) {
for(int j = 0; j < N; j++) {
graph1[j][vertexIndex] = 0;
graph1[vertexIndex][j] = 0;
}
}
graph1[nearestVertex][vertexIndex] = 0;
graph1[vertexIndex][nearestVertex] = 0;
}
}
}
root = parents;
return dists;
}
public static void main(String[] args) throws IOException{
BufferedReader f = new BufferedReader(new FileReader("dining.in"));
StringTokenizer st = new StringTokenizer(f.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int[][] matrix = new int[N][N];
arr = new int[N-1];
pasture = new int[N];
for(int i = 0; i < N; i++){
//Arrays.fill(matrix[i], Integer.MAX_VALUE);
}
for(int i = 0; i < M; i ++) {
st = new StringTokenizer(f.readLine());
int x = Integer.parseInt(st.nextToken()),y = Integer.parseInt(st.nextToken()),z = Integer.parseInt(st.nextToken());
matrix[x + OFFSET][y + OFFSET] = z;
matrix[y + OFFSET][x + OFFSET] = z;
}
//System.out.println(Arrays.deepToString(matrix).replaceAll("],*", "],\n"));
// Dijkstra Modification begins here
for(int i = 0; i < K; i++) {
st = new StringTokenizer(f.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
pasture[x-1] = y;
x = x + OFFSET;
for(int j = 0; j < N; j++) {
if(matrix[j][x] != 0) {
//System.out.println("Override 1 "+j+" "+x+" "+y);
matrix[j][x] = matrix[j][x] - y;
}
if(matrix[x][j] != 0) {
//System.out.println("Override 2 "+x+" "+j+" "+y);
matrix[x][j] = matrix[x][j] - y;
}
}
}
// End modification
//System.out.println("Modifacation Complete");
//System.out.println(Arrays.deepToString(matrix).replaceAll("],*", "],\n"));
int[] out = dijkstra(matrix, N-1);
//System.out.println(Arrays.toString(root));
//System.out.println(Arrays.toString(out));
PrintWriter pw = new PrintWriter(new FileWriter("dining.out"));
for(int k:arr) {
pw.println(k);
}
pw.close();
}
}