import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collections; import java.util.LinkedList; import java.util.StringTokenizer; /** * Created by tom on 2016-11-26. */ public class sze { int nTasks; int nProcessors; int nSegments; Task[] allTasks; Segment[] allSegments; private Node source; private Node sink; void readInput() { nTasks = nextInt(); nProcessors = nextInt(); allTasks = new Task[nTasks]; for (int i = 0; i < nTasks; i++) { allTasks[i] = new Task(i, nextInt(), nextInt(), nextInt()); } } boolean solve() { int required = 0; for (Task t : allTasks) { required += t.required; } return required == computeMaximumFlow(); } String solve1() { return solve() ? "TAK" : "NIE"; } int computeMaximumFlow() { clear(); createSegments(); createNetwork(); return fordFulkerson(); } private int fordFulkerson() { int result = 0; int extended; do { for (Node n : allTasks) { n.visited = false; } for (Node n : allSegments) { n.visited = false; } source.visited = false; sink.visited = false; extended = findResidualPath(source, Integer.MAX_VALUE); result += extended; } while (extended > 0); return result; } private int findResidualPath(Node node, int flow) { if (node == sink) { return flow; } node.visited = true; for (int i=0; i<node.allEdges.size(); ++i) { Edge e = node.allEdges.get(0); if (e.usedCapacity < e.capacity && e.to.visited == false) { int extended = getExtended(flow, e); if (extended > 0) { return extended; } } node.allEdges.addLast(e); node.allEdges.removeFirst(); } return 0; } private int getExtended(int flow, Edge e) { int nextFlow = Math.min(flow, (e.capacity - e.usedCapacity)); e.usedCapacity += nextFlow; if (e.sibling != null) { e.sibling.usedCapacity -= nextFlow; } int extended = findResidualPath(e.to, nextFlow); int unusedFlow = nextFlow - extended; e.usedCapacity -= unusedFlow; if (e.sibling != null) { e.sibling.usedCapacity += unusedFlow; } return extended; } private void createNetwork() { this.source = new Node(); this.sink = new Node(); // segments -> tasks for (Segment s : allSegments) { for (Task t : allTasks) { if (overlaps(t.from, t.to, s.from, s.to)) { Edge edge = new Edge(s, t, s.length()); Edge reversed = new Edge(t, s, s.length()); reversed.usedCapacity = s.length(); edge.sibling = reversed; reversed.sibling = edge; s.allEdges.addFirst(edge); t.allEdges.addLast(reversed); } } } // source -> segments for (Segment s : allSegments) { source.allEdges.addFirst(new Edge(source, s, nProcessors * s.length())); } // tasks -> sink for (Task t : allTasks) { t.allEdges.addFirst(new Edge(t, sink, t.required)); } } private boolean overlaps(int from, int to, int a, int b) { return from <= a && b <= to; } private void createSegments() { LinkedList<Integer> segmentPoints = new LinkedList<>(); for (Task t : allTasks) { segmentPoints.add(t.from); segmentPoints.add(t.to); } Collections.sort(segmentPoints); for (int i = 1; i < segmentPoints.size(); i++) { if (segmentPoints.get(i) == segmentPoints.get(i - 1)) { segmentPoints.remove(i); i--; } } nSegments = segmentPoints.size() - 1; allSegments = new Segment[nSegments]; for (int i = 1; i <= nSegments; i++) { allSegments[i - 1] = new Segment(segmentPoints.get(i - 1), segmentPoints.get(i)); } } void clear() { for (Task t : allTasks) { t.allEdges.clear(); } } private static class Node { boolean visited = false; int id; LinkedList<Edge> allEdges = new LinkedList<>(); public String toString() { return "(" + id + ")"; } } private static class Edge { Node from, to; int usedCapacity, capacity; Edge sibling; public Edge(Node from, Node to, int capacity) { this.from = from; this.to = to; this.capacity = capacity; usedCapacity = 0; } public String toString() { return from.toString() + "->" + to.toString() + "] cap=" + usedCapacity + "/" + capacity; } } private static class Segment extends Node { int from, to; public Segment(int from, int to) { this.from = from; this.to = to; } public int length() { return to - from; } public String toString() { return "[" + from + "," + to + "]"; } } static class Task extends Node { int from, to, required; public Task(int nr, int from, int to, int required) { id = nr; this.from = from; this.to = to; this.required = required; } public String toString() { return "Task: [" + from + "," + to + "]"; } } public static void main(String[] args) { sze sze = new sze(); sze.readInput(); System.out.println(sze.solve1()); } /********** reader ***********/ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); @SuppressWarnings("Duplicates") private String next() { try { while (!st.hasMoreTokens()) { String str; str = br.readLine(); if (str == null) return null; st = new StringTokenizer(str); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } }
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collections; import java.util.LinkedList; import java.util.StringTokenizer; /** * Created by tom on 2016-11-26. */ public class sze { int nTasks; int nProcessors; int nSegments; Task[] allTasks; Segment[] allSegments; private Node source; private Node sink; void readInput() { nTasks = nextInt(); nProcessors = nextInt(); allTasks = new Task[nTasks]; for (int i = 0; i < nTasks; i++) { allTasks[i] = new Task(i, nextInt(), nextInt(), nextInt()); } } boolean solve() { int required = 0; for (Task t : allTasks) { required += t.required; } return required == computeMaximumFlow(); } String solve1() { return solve() ? "TAK" : "NIE"; } int computeMaximumFlow() { clear(); createSegments(); createNetwork(); return fordFulkerson(); } private int fordFulkerson() { int result = 0; int extended; do { for (Node n : allTasks) { n.visited = false; } for (Node n : allSegments) { n.visited = false; } source.visited = false; sink.visited = false; extended = findResidualPath(source, Integer.MAX_VALUE); result += extended; } while (extended > 0); return result; } private int findResidualPath(Node node, int flow) { if (node == sink) { return flow; } node.visited = true; for (int i=0; i<node.allEdges.size(); ++i) { Edge e = node.allEdges.get(0); if (e.usedCapacity < e.capacity && e.to.visited == false) { int extended = getExtended(flow, e); if (extended > 0) { return extended; } } node.allEdges.addLast(e); node.allEdges.removeFirst(); } return 0; } private int getExtended(int flow, Edge e) { int nextFlow = Math.min(flow, (e.capacity - e.usedCapacity)); e.usedCapacity += nextFlow; if (e.sibling != null) { e.sibling.usedCapacity -= nextFlow; } int extended = findResidualPath(e.to, nextFlow); int unusedFlow = nextFlow - extended; e.usedCapacity -= unusedFlow; if (e.sibling != null) { e.sibling.usedCapacity += unusedFlow; } return extended; } private void createNetwork() { this.source = new Node(); this.sink = new Node(); // segments -> tasks for (Segment s : allSegments) { for (Task t : allTasks) { if (overlaps(t.from, t.to, s.from, s.to)) { Edge edge = new Edge(s, t, s.length()); Edge reversed = new Edge(t, s, s.length()); reversed.usedCapacity = s.length(); edge.sibling = reversed; reversed.sibling = edge; s.allEdges.addFirst(edge); t.allEdges.addLast(reversed); } } } // source -> segments for (Segment s : allSegments) { source.allEdges.addFirst(new Edge(source, s, nProcessors * s.length())); } // tasks -> sink for (Task t : allTasks) { t.allEdges.addFirst(new Edge(t, sink, t.required)); } } private boolean overlaps(int from, int to, int a, int b) { return from <= a && b <= to; } private void createSegments() { LinkedList<Integer> segmentPoints = new LinkedList<>(); for (Task t : allTasks) { segmentPoints.add(t.from); segmentPoints.add(t.to); } Collections.sort(segmentPoints); for (int i = 1; i < segmentPoints.size(); i++) { if (segmentPoints.get(i) == segmentPoints.get(i - 1)) { segmentPoints.remove(i); i--; } } nSegments = segmentPoints.size() - 1; allSegments = new Segment[nSegments]; for (int i = 1; i <= nSegments; i++) { allSegments[i - 1] = new Segment(segmentPoints.get(i - 1), segmentPoints.get(i)); } } void clear() { for (Task t : allTasks) { t.allEdges.clear(); } } private static class Node { boolean visited = false; int id; LinkedList<Edge> allEdges = new LinkedList<>(); public String toString() { return "(" + id + ")"; } } private static class Edge { Node from, to; int usedCapacity, capacity; Edge sibling; public Edge(Node from, Node to, int capacity) { this.from = from; this.to = to; this.capacity = capacity; usedCapacity = 0; } public String toString() { return from.toString() + "->" + to.toString() + "] cap=" + usedCapacity + "/" + capacity; } } private static class Segment extends Node { int from, to; public Segment(int from, int to) { this.from = from; this.to = to; } public int length() { return to - from; } public String toString() { return "[" + from + "," + to + "]"; } } static class Task extends Node { int from, to, required; public Task(int nr, int from, int to, int required) { id = nr; this.from = from; this.to = to; this.required = required; } public String toString() { return "Task: [" + from + "," + to + "]"; } } public static void main(String[] args) { sze sze = new sze(); sze.readInput(); System.out.println(sze.solve1()); } /********** reader ***********/ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); @SuppressWarnings("Duplicates") private String next() { try { while (!st.hasMoreTokens()) { String str; str = br.readLine(); if (str == null) return null; st = new StringTokenizer(str); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } } |