Submission #17661476
Source Code Expand
Copy
func readInt3(line: Int = #line, file: String = #file) -> (Int, Int, Int) {guard let string = readLine() else {preconditionFailure("No input (at line \(line) in \(file))")}let values: [Int] = string.split(separator: " ").map { part inguard let value = Int(part) else {preconditionFailure("Illegal input value: \(string) (at line \(line) in \(file))")}return value}precondition(values.count == 3, "Illegal number of input values: count = \(values.count), values = \(values) (at line \(line) in \(file))")return (values[0], values[1], values[2])}func readIntN(line: Int = #line, file: String = #file) -> [Int] {guard let string = readLine() else {preconditionFailure("No input (at line \(line) in \(file))")}let values: [Int] = string.split(separator: " ").map { part inguard let value = Int(part) else {preconditionFailure("Illegal input value: \(string) (at line \(line) in \(file))")
func readInt3(line: Int = #line, file: String = #file) -> (Int, Int, Int) { guard let string = readLine() else { preconditionFailure("No input (at line \(line) in \(file))") } let values: [Int] = string.split(separator: " ").map { part in guard let value = Int(part) else { preconditionFailure("Illegal input value: \(string) (at line \(line) in \(file))") } return value } precondition(values.count == 3, "Illegal number of input values: count = \(values.count), values = \(values) (at line \(line) in \(file))") return (values[0], values[1], values[2]) } func readIntN(line: Int = #line, file: String = #file) -> [Int] { guard let string = readLine() else { preconditionFailure("No input (at line \(line) in \(file))") } let values: [Int] = string.split(separator: " ").map { part in guard let value = Int(part) else { preconditionFailure("Illegal input value: \(string) (at line \(line) in \(file))") } return value } return values } private struct _PriorityQueue<Element> { private var elements: [Element] = [] private let areInIncreasingOrder: (Element, Element) -> Bool init<S>(_ elements: S, by areInIncreasingOrder: @escaping (Element, Element) -> Bool) where S: Sequence, S.Element == Element { self.areInIncreasingOrder = areInIncreasingOrder for element in elements { append(element) } } init(by areInIncreasingOrder: @escaping (Element, Element) -> Bool) { self.init(EmptyCollection(), by: areInIncreasingOrder) } var isEmpty: Bool { elements.isEmpty } var count: Int { elements.count } var first: Element? { elements.first } mutating func append(_ element: Element) { var i = elements.count elements.append(element) elements.withUnsafeMutableBufferPointer { elements in while i > 0 { let parentIndex = (i - 1) >> 1 let parent = elements[parentIndex] guard areInIncreasingOrder(element, parent) else { break } elements[parentIndex] = element elements[i] = parent i = parentIndex } } } mutating func popFirst() -> Element? { guard let element = elements.popLast() else { return nil } guard let first = elements.first else { return element } elements.withUnsafeMutableBufferPointer { elements in elements[0] = element var i = 0 while true { var childIndex: Int = (i << 1) + 1 guard childIndex < elements.count else { break } var child: Element = elements[childIndex] let rightIndex = childIndex + 1 if rightIndex < elements.count { let right = elements[rightIndex] if areInIncreasingOrder(right, child) { childIndex = rightIndex child = right } } if areInIncreasingOrder(element, child) { break } elements[childIndex] = element elements[i] = child i = childIndex } } return first } } protocol HasInfinity { static var infinity: Self { get } } extension Int: HasInfinity { static var infinity: Int { .max } } extension UInt: HasInfinity { static var infinity: UInt { .max } } extension Float: HasInfinity {} extension Double: HasInfinity {} func dijkstra<Distance>(graph: [[(index: Int, distance: Distance)]], startedAt start: Int) -> [Distance] where Distance: Comparable, Distance: AdditiveArithmetic, Distance: HasInfinity { var result: [Distance] = .init(repeating: .infinity, count: graph.count) result[start] = .zero var used = Array(repeating:false, count: graph.count) var queue = _PriorityQueue<(Distance, Int)>(by: <=) queue.append((.zero, start)) while let (_, from) = queue.popFirst() { if used[from] { continue } used[from] = true for (to, distance) in graph[from]{ if result[from] + distance < result[to] { result[to] = result[from] + distance queue.append((result[to], to)) } } } return result } let (n, m, t) = readInt3() let aa = readIntN() var graph1: [[(index: Int, distance: Int)]] = .init(repeating: [], count: n) var graph2: [[(index: Int, distance: Int)]] = .init(repeating: [], count: n) for _ in 1 ... m { let (a, b, c) = readInt3() graph1[a - 1].append((index: b - 1, distance: c)) graph2[b - 1].append((index: a - 1, distance: c)) } let distances1 = dijkstra(graph: graph1, startedAt: 0) let distances2 = dijkstra(graph: graph2, startedAt: 0) var maxMoney: Int = .min for i in 0 ..< n { if distances1[i] == Int.max || distances2[i] == Int.max{ continue } let money = aa[i] * (t - distances1[i] - distances2[i]) if money > maxMoney { maxMoney = money } } print(maxMoney)
Submission Info
Submission Time | |
---|---|
Task | D - トレジャーハント |
User | tardigrade |
Language | Swift (5.2.1) |
Score | 100 |
Code Size | 5297 Byte |
Status | AC |
Exec Time | 509 ms |
Memory | 24724 KB |
Judge Result
Set Name | Sample | Subtask1 | All | ||||||
---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 50 / 50 | 50 / 50 | ||||||
Status |
|
|
|
Set Name | Test Cases |
---|---|
Sample | 00_example_01.txt, 00_example_02.txt, 00_example_03.txt |
Subtask1 | 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 50_small_01.txt, 50_small_02.txt, 50_small_03.txt, 50_small_04.txt, 50_small_05.txt, 50_small_06.txt, 50_small_07.txt, 50_small_08.txt, 50_small_09.txt, 50_small_10.txt, 60_hand_01.txt, 60_hand_02.txt, 60_hand_03.txt, 60_hand_04.txt, 70_max_01.txt, 70_max_02.txt, 70_max_03.txt |
All | 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 10_rand_01.txt, 10_rand_02.txt, 10_rand_03.txt, 10_rand_04.txt, 10_rand_05.txt, 10_rand_06.txt, 10_rand_07.txt, 10_rand_08.txt, 10_rand_09.txt, 10_rand_10.txt, 10_rand_12.txt, 10_rand_13.txt, 30_max_01.txt, 30_max_02.txt, 30_max_03.txt, 30_max_04.txt, 30_max_05.txt, 30_max_06.txt, 40_corner_01.txt, 40_corner_02.txt, 40_corner_03.txt, 40_corner_04.txt, 50_small_01.txt, 50_small_02.txt, 50_small_03.txt, 50_small_04.txt, 50_small_05.txt, 50_small_06.txt, 50_small_07.txt, 50_small_08.txt, 50_small_09.txt, 50_small_10.txt, 60_hand_01.txt, 60_hand_02.txt, 60_hand_03.txt, 60_hand_04.txt, 70_max_01.txt, 70_max_02.txt, 70_max_03.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
00_example_01.txt | AC | 14 ms | 8380 KB |
00_example_02.txt | AC | 6 ms | 8228 KB |
00_example_03.txt | AC | 12 ms | 8416 KB |
10_rand_01.txt | AC | 40 ms | 8980 KB |
10_rand_02.txt | AC | 124 ms | 11224 KB |
10_rand_03.txt | AC | 48 ms | 9624 KB |
10_rand_04.txt | AC | 22 ms | 8620 KB |
10_rand_05.txt | AC | 51 ms | 9632 KB |
10_rand_06.txt | AC | 51 ms | 9912 KB |
10_rand_07.txt | AC | 67 ms | 10644 KB |
10_rand_08.txt | AC | 32 ms | 8972 KB |
10_rand_09.txt | AC | 61 ms | 10440 KB |
10_rand_10.txt | AC | 19 ms | 8760 KB |
10_rand_12.txt | AC | 200 ms | 12452 KB |
10_rand_13.txt | AC | 188 ms | 12224 KB |
30_max_01.txt | AC | 428 ms | 15648 KB |
30_max_02.txt | AC | 474 ms | 18288 KB |
30_max_03.txt | AC | 360 ms | 14320 KB |
30_max_04.txt | AC | 499 ms | 21588 KB |
30_max_05.txt | AC | 509 ms | 21504 KB |
30_max_06.txt | AC | 505 ms | 21660 KB |
40_corner_01.txt | AC | 413 ms | 24596 KB |
40_corner_02.txt | AC | 462 ms | 24644 KB |
40_corner_03.txt | AC | 483 ms | 24472 KB |
40_corner_04.txt | AC | 509 ms | 24724 KB |
50_small_01.txt | AC | 13 ms | 8196 KB |
50_small_02.txt | AC | 5 ms | 8080 KB |
50_small_03.txt | AC | 6 ms | 7988 KB |
50_small_04.txt | AC | 10 ms | 8268 KB |
50_small_05.txt | AC | 24 ms | 8484 KB |
50_small_06.txt | AC | 14 ms | 8504 KB |
50_small_07.txt | AC | 5 ms | 8460 KB |
50_small_08.txt | AC | 7 ms | 8148 KB |
50_small_09.txt | AC | 8 ms | 8148 KB |
50_small_10.txt | AC | 10 ms | 8120 KB |
60_hand_01.txt | AC | 6 ms | 8284 KB |
60_hand_02.txt | AC | 5 ms | 8416 KB |
60_hand_03.txt | AC | 5 ms | 8416 KB |
60_hand_04.txt | AC | 5 ms | 8088 KB |
70_max_01.txt | AC | 125 ms | 10328 KB |
70_max_02.txt | AC | 127 ms | 10188 KB |
70_max_03.txt | AC | 126 ms | 9992 KB |