-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[Kattis]Jupiter.java
More file actions
181 lines (162 loc) · 4.65 KB
/
[Kattis]Jupiter.java
File metadata and controls
181 lines (162 loc) · 4.65 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Jupiter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int W = scan.nextInt();
int Q = scan.nextInt();
int S = scan.nextInt();
int V = 2 + 2 * Q * W + W;
int[] sensorToQueue = new int[S];
for (int i = 0; i < S; i++) {
sensorToQueue[i] = scan.nextInt() - 1;
}
int[] queueSize = new int[Q];
for (int i = 0; i < Q; i++) {
queueSize[i] = scan.nextInt();
}
int[] windowSize = new int[W];
long[][] dataFlow = new long[W][Q];
for (int i = 0; i < W; i++) {
windowSize[i] = scan.nextInt();
for (int j = 0; j < S; j++) {
long data = scan.nextLong();
dataFlow[i][sensorToQueue[j]] += data;
}
}
scan.close();
AdjacencyList graph = new AdjacencyList(V);
long dataLoad = 0;
for (int w = 0; w < W; w++) {
int windowHead = windowHead(w, Q, W);
for (int q = 0; q < Q; q++) {
int head = queueHead(q, w, Q);
// System.err.print(dataFlow[w][q] + " ");
dataLoad += dataFlow[w][q];
graph.setEdge(0, head, dataFlow[w][q]);
graph.setEdge(head, head + 1, queueSize[q]);
if (w < W - 1) {
int nextHead = queueHead(q, w + 1, Q);
graph.setEdge(head + 1, nextHead, Long.MAX_VALUE);
}
graph.setEdge(head + 1, windowHead, Long.MAX_VALUE);
}
graph.setEdge(windowHead, V - 1, windowSize[w]);
// System.err.println();
}
long maxFlow = maxFlowEKLong(graph, 0, V - 1, V);
// for (long[] arr : graph) {
// for (long l : arr) {
// if (l > Integer.MAX_VALUE)
// System.err.print("i ");
// else
// System.err.print(l + " ");
// }
// System.err.println();
// }
// System.out.println(maxFlow);
// System.out.println(dataLoad);
System.out.println(maxFlow >= dataLoad ? "possible" : "impossible");
}
private static int queueHead(int q, int w, int Q) {
return 1 + Q * w * 2 + q * 2;
}
private static int windowHead(int w, int Q, int W) {
return 1 + Q * W * 2 + w;
}
static class AdjacencyList {
ArrayList<Vertex> vertexList;
AdjacencyList(int size) {
vertexList = new ArrayList<Vertex>(size);
for (int i = 0; i < size; i++) {
vertexList.add(new Vertex());
}
}
Vertex getVertex(int i) {
return vertexList.get(i);
}
void setEdge(int from, int to, long capacity) {
vertexList.get(from).outList.add(new EdgeLong(from, to, capacity));
vertexList.get(to).outList.add(new EdgeLong(to, from, 0));
}
}
static class Vertex {
ArrayList<EdgeLong> outList = new ArrayList<EdgeLong>();
}
static class Edge {
int from;
int to;
int capacity;
Edge(int f, int t, int c) {
from = f;
to = t;
capacity = c;
}
}
static class EdgeLong {
int from;
int to;
long capacity;
EdgeLong(int f, int t, long c) {
from = f;
to = t;
capacity = c;
}
}
/**
* This destroys the original graph
*
* @param residue
* @param source
* @param sink
* @param V
* @return
*/
public static long maxFlowEKLong(AdjacencyList residue, int source, int sink, final int V) {
long maxFlow = 0;
// build original
EdgeLong[] parent = new EdgeLong[V];
while (maxFlowEKBFSLong(residue, source, sink, parent, V)) {
long flow = Long.MAX_VALUE;
int current = sink;
for (; current != source; current = parent[current].from) {
EdgeLong e = parent[current];
flow = Math.min(flow, e.capacity);
}
for (current = sink; current != source; current = parent[current].from) {
EdgeLong e = parent[current];
e.capacity -= flow;
for (EdgeLong back : residue.getVertex(e.to).outList) {
if (back.to == e.from) {
back.capacity += flow;
break;
}
}
}
maxFlow += flow;
}
return maxFlow;
}
private static boolean maxFlowEKBFSLong(AdjacencyList graph, int source, int sink, EdgeLong[] parent, final int V) {
boolean[] seen = new boolean[V];
Queue<Integer> queue = new LinkedList<Integer>();
queue.offer(source);
seen[source] = true;
parent[source] = null;
while (!queue.isEmpty()) {
int current = queue.poll();
if (current == sink)
return true;
for (EdgeLong e : graph.getVertex(current).outList) {
if (e.capacity > 0 && !seen[e.to]) {
queue.offer(e.to);
parent[e.to] = e;
seen[e.to] = true;
}
}
}
return seen[sink];
}
}