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
Latest commit
135 lines (134 loc) · 4.32 KB
/
Copy pathdining.java
File metadata and controls
135 lines (134 loc) · 4.32 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
131
132
133
134
135
importjava.io.*;
importjava.util.*;
publicclassdining {
publicstaticint[] dijkstra(Map<Pair, Integer> cost, List<List<Integer>> graph, finalintN, intsource) {
int[] dist = newint[N];
PriorityQueue<Integer> visited = newPriorityQueue<>(N,newComparator<Integer>() {
@Override
publicintcompare(Integerarg0, Integerarg1) {
returnInteger.compare(dist[arg0], dist[arg1]);
//return 0;
}
});
Arrays.fill(dist, Integer.MAX_VALUE);
dist[N-1] = 0; // Starting value
//nodeQueue.add(N-1);
visited.add(source);
intu;
while(!visited.isEmpty()) {
//int u = nodeQueue.remove(); // Get next node
u = visited.remove();
List<Integer> adj = graph.get(u);
for(intnode:adj) {
//if(dist[node] == Integer.MAX_VALUE)
{ // If this node has not been visited yet
intedgeDist = cost.get(newPair(u,node));
inttotalDist = dist[u] + edgeDist;
if (totalDist < dist[node])
{
dist[node] = totalDist;
visited.add(node);
}
//nodeQueue.add(node);
}
}
}
returndist;
}
publicstaticvoidmain(String[] args) throwsIOException {
BufferedReaderf = newBufferedReader(newFileReader("1.in"));
StringTokenizerst = newStringTokenizer(f.readLine());
intN,M,K;
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
finalintnumOfCows = N - 1;
List<List<Integer>> graph = newArrayList<>(M+1);
for(inti = 0; i < M+1; i ++) {
graph.add(newArrayList<>());
}
Map<Pair,Integer> cost = newHashMap<>();
for(inti =0 ; i < M; i ++) {
st = newStringTokenizer(f.readLine());
intstart = Integer.parseInt(st.nextToken());
intend = Integer.parseInt(st.nextToken());
intedgeCost = Integer.parseInt(st.nextToken());
graph.get(start-1).add(end-1);
graph.get(end-1).add(start-1);
cost.put(newPair(start-1,end-1), edgeCost);
cost.put(newPair(end-1,start-1), edgeCost);
}
//System.out.println("Cost : "+cost);
Map<Pair,Integer> costWithHaybales = newHashMap<>(cost);
System.out.println("Graph as an edgelist: "+graph);
int[] taste = newint[K];
List<Integer> bales = newArrayList<>();
for(inti = 0; i < K; i ++) {
st = newStringTokenizer(f.readLine());
intindex = Integer.parseInt(st.nextToken()) - 1;
bales.add(index);
intvalue = Integer.parseInt(st.nextToken());
taste[i] = value;
}
f.close();
//int[] empty = new int[N];
//Arrays.fill(empty, Integer.MAX_VALUE);
//empty[N-1] = 0;
int[] distOrig,distTo;
distOrig = dijkstra(cost, graph, N, N - 1);
//for(int bale: graph.get(N-1)) {
// graph.get(bale).remove((Object) (N-1));
//}
//graph.get(N-1).clear();
for(inti = 0 ; i < K; i++) {
inttarget = bales.get(i);
graph.get(target).add(N);
graph.get(N).add(target);
//System.out.println("Dist: "+distOrig[target]+" Taste: "+taste[i]);
//System.out.println(target+" -- "+(N-1) + " connected with weight "+(distOrig[target] - taste[i]));
costWithHaybales.put(newPair(target,N), distOrig[target] - taste[i]);
//costWithHaybales.put(new Pair(N,target), distOrig[target] - taste[i]);
}
//System.out.println("Modified New Graph : "+graph);
System.out.println("Cost : "+cost);
System.out.println("Cost with haybales: "+costWithHaybales);
distTo = dijkstra(costWithHaybales, graph, N+1, N);
System.out.println("Output :" + Arrays.toString(distOrig));
System.out.println("Output(haybales):" + Arrays.toString(distTo));
PrintWriterpw = newPrintWriter(newBufferedWriter(newFileWriter("dining.out")));
for(inti =0 ; i < numOfCows; i ++) {
if(distTo[i] <= distOrig[i]) {
pw.println(1);
}else {
pw.println(0);
}
}
pw.close();
}
}
// Order does matter pair
classPair{
intx,y;
publicPair(intx,inty) {
this.x =x;
this.y =y;
}
@Override
publicStringtoString() {
return"("+this.x+","+this.y+")";
}
@Override
publicbooleanequals(Objectobj) {
if(objinstanceofPair) {
Pairp = (Pair) obj;
if((p.x == this.x && p.y == this.y)) {
returntrue;
}
}
returnfalse;
}
@Override
publicinthashCode(){
returnInteger.hashCode(this.x) * (Integer.hashCode(this.y)+1);
}
}