結果

問題 No.1301 Strange Graph Shortest Path
ユーザー yudedakoyudedako
提出日時 2022-01-11 09:08:44
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,551 ms / 3,000 ms
コード長 2,675 bytes
コンパイル時間 15,877 ms
コンパイル使用メモリ 456,904 KB
実行使用メモリ 148,600 KB
最終ジャッジ日時 2024-04-26 20:31:38
合計ジャッジ時間 66,877 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 310 ms
55,212 KB
testcase_01 AC 322 ms
55,308 KB
testcase_02 AC 1,551 ms
143,540 KB
testcase_03 AC 1,335 ms
135,604 KB
testcase_04 AC 1,515 ms
147,108 KB
testcase_05 AC 1,349 ms
137,632 KB
testcase_06 AC 1,362 ms
139,260 KB
testcase_07 AC 1,428 ms
142,360 KB
testcase_08 AC 1,474 ms
136,312 KB
testcase_09 AC 1,417 ms
139,720 KB
testcase_10 AC 1,242 ms
135,928 KB
testcase_11 AC 1,376 ms
143,724 KB
testcase_12 AC 1,506 ms
148,600 KB
testcase_13 AC 1,533 ms
143,716 KB
testcase_14 AC 1,367 ms
138,900 KB
testcase_15 AC 1,343 ms
139,048 KB
testcase_16 AC 1,365 ms
143,548 KB
testcase_17 AC 1,422 ms
143,160 KB
testcase_18 AC 1,506 ms
140,428 KB
testcase_19 AC 1,386 ms
142,676 KB
testcase_20 AC 1,273 ms
140,568 KB
testcase_21 AC 1,485 ms
143,640 KB
testcase_22 AC 1,396 ms
140,356 KB
testcase_23 AC 1,517 ms
147,392 KB
testcase_24 AC 1,374 ms
142,724 KB
testcase_25 AC 1,526 ms
146,452 KB
testcase_26 AC 1,376 ms
142,456 KB
testcase_27 AC 1,340 ms
143,708 KB
testcase_28 AC 1,290 ms
136,348 KB
testcase_29 AC 1,394 ms
147,572 KB
testcase_30 AC 1,540 ms
146,240 KB
testcase_31 AC 1,445 ms
146,044 KB
testcase_32 AC 310 ms
55,308 KB
testcase_33 AC 1,241 ms
141,424 KB
testcase_34 AC 1,488 ms
148,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*

class Edge(val to: Int, val pair: Int, var flow: Boolean, val cost: Long) {
    override fun toString(): String {
        return "Edge(to: $to, pair: $pair, flow: $flow, cost: $cost)"
    }
}
class MinCostFlow(val size: Int) {
    val vec = Array(size){ mutableListOf<Edge>()}
    fun addEdge(from: Int, to: Int, cost: Long) {
        vec[from].add(Edge(to, vec[to].size, true, cost))
        vec[to].add(Edge(from, vec[from].size - 1, false, -cost))
    }
    fun push(source: Int, sink: Int, flowLimit: Int = Int.MAX_VALUE): List<Long> {
        val potential = LongArray(size)
        val minDistance = LongArray(size)
        val queue = PriorityQueue(compareBy(Pair<Int, Long>::second))
        val prevEdge = Array(size){Edge(-1, -1, false, -1)}
        val result = mutableListOf(0L)
        repeat(flowLimit){
            minDistance.fill(Long.MAX_VALUE)
            minDistance[source] = 0
            queue.clear()
            queue.add(source to 0L)
            while (queue.isNotEmpty()) {
                val (current, cost) = queue.poll()
                if (minDistance[current] < cost) continue
                for (next in vec[current]) {
                    if (!next.flow) continue
                    val e = potential[current] - potential[next.to] + next.cost
                    if (minDistance[next.to] > cost + e) {
                        minDistance[next.to] = cost + e
                        prevEdge[next.to] = next
                        queue.add(next.to to cost + e)
                    }
                }
            }
            if (minDistance[sink] == Long.MAX_VALUE) return result
            for (i in minDistance.indices) {
                potential[i] += minDistance[i]
            }
            result.add(result.last() + potential[sink])
            var last = sink
            while (last != source) {
                val e = prevEdge[last]
                val r = vec[e.to][e.pair]
                e.flow = false
                r.flow = true
                last = r.to
            }
        }
        return result
    }
}
fun main() {
    val (n, m) = readLine()!!.trim().split(' ').map(String::toInt)
    val edges = List(m){
        val (u, v, c, d) = readLine()!!.trim().split(' ').map(String::toInt)
        (u - 1 to v - 1) to (c to d)
    }
    val graph = MinCostFlow(n)
    for ((vertices, costs) in edges) {
        val (u, v) = vertices
        val (c, d) = costs
        graph.addEdge(u, v, c.toLong())
        graph.addEdge(u, v, d.toLong())
        graph.addEdge(v, u, c.toLong())
        graph.addEdge(v, u, d.toLong())
    }
    val result = graph.push(0, n - 1, 2)[2]
    println(result)
}
0