結果

問題 No.1601 With Animals into Institute
ユーザー 👑 箱
提出日時 2021-07-10 14:16:21
言語 Kotlin
(1.9.10)
結果
AC  
実行時間 2,031 ms / 3,000 ms
コード長 3,876 bytes
コンパイル時間 18,727 ms
コンパイル使用メモリ 428,848 KB
実行使用メモリ 123,108 KB
最終ジャッジ日時 2023-09-14 19:19:20
合計ジャッジ時間 56,041 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 325 ms
57,112 KB
testcase_01 AC 327 ms
56,808 KB
testcase_02 AC 326 ms
56,972 KB
testcase_03 AC 521 ms
60,968 KB
testcase_04 AC 512 ms
60,508 KB
testcase_05 AC 513 ms
60,868 KB
testcase_06 AC 1,764 ms
119,528 KB
testcase_07 AC 1,786 ms
119,460 KB
testcase_08 AC 1,841 ms
119,708 KB
testcase_09 AC 1,913 ms
122,764 KB
testcase_10 AC 1,995 ms
123,108 KB
testcase_11 AC 1,938 ms
122,780 KB
testcase_12 AC 1,929 ms
122,808 KB
testcase_13 AC 1,935 ms
123,028 KB
testcase_14 AC 1,960 ms
122,820 KB
testcase_15 AC 2,031 ms
122,668 KB
testcase_16 AC 1,983 ms
121,116 KB
testcase_17 AC 2,023 ms
122,604 KB
testcase_18 AC 516 ms
60,800 KB
testcase_19 AC 506 ms
60,680 KB
testcase_20 AC 513 ms
60,624 KB
testcase_21 AC 515 ms
60,628 KB
testcase_22 AC 515 ms
61,112 KB
testcase_23 AC 511 ms
60,692 KB
testcase_24 AC 517 ms
60,652 KB
testcase_25 AC 514 ms
60,808 KB
testcase_26 AC 513 ms
60,532 KB
testcase_27 AC 328 ms
57,036 KB
testcase_28 AC 331 ms
56,776 KB
testcase_29 AC 325 ms
56,800 KB
testcase_30 AC 328 ms
56,796 KB
testcase_31 AC 328 ms
56,836 KB
testcase_32 AC 325 ms
57,260 KB
testcase_33 AC 328 ms
57,032 KB
testcase_34 AC 329 ms
56,860 KB
testcase_35 AC 327 ms
57,108 KB
testcase_36 AC 325 ms
56,832 KB
testcase_37 AC 327 ms
56,956 KB
testcase_38 AC 325 ms
57,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

data class Edge(val node: Int, val cost: Long)
data class State(val cost: Long, val position: Int, val prenode: Int) : Comparable<State> {
    override fun compareTo(other: State): Int {
        return if (cost == other.cost) position.compareTo(other.position) else cost.compareTo(other.cost)
    }
}

fun dijkstra(adj: Array<MutableList<Edge>>, start: Int): LongArray {
    val n = adj.count()
    val dist = LongArray(n) { Long.MAX_VALUE }
    val preNodes = IntArray(n) { it }
    val heap = PriorityQueue<State>()
    dist[start] = 0
    heap.add(State(0, start, 0))
    while (heap.isNotEmpty()) {
        val state = heap.poll()
        val (cost, position, prenode) = state
        if (cost > dist[position]) continue
        preNodes[position] = prenode
        for (edge in adj[position]) {
            val next = State(cost + edge.cost, edge.node, position)
            if (next.cost < dist[next.position]) {
                heap.add(next)
                dist[next.position] = next.cost
            }
        }
    }
    return dist
}

fun PrintWriter.solve() {
    val n = nextInt()
    val m = nextInt()
    val adj = Array(2 * n) { mutableListOf<Edge>() }
    for (i in 0 until m) {
        val a = nextInt() - 1
        val b = nextInt() - 1
        val c = nextLong()
        val x = nextInt()
        if (x == 0) {
            adj[a].add(Edge(b, c))
            adj[b].add(Edge(a, c))
            adj[a + n].add(Edge(b + n, c))
            adj[b + n].add(Edge(a + n, c))
        } else {
            adj[a].add(Edge(b + n, c))
            adj[b].add(Edge(a + n, c))
            adj[a + n].add(Edge(b + n, c))
            adj[b + n].add(Edge(a + n, c))
        }
    }
    val result = dijkstra(adj, n - 1)
    println(result.slice(n until 2 * n - 1).joinToString("\n"))
}

// region ModInt
class ModInt(x: Long) {
    companion object {
        const val MOD = 1000000007L
        //const val MOD = 998244353L
    }

    constructor(y: Int) : this(y.toLong())

    val x = (x % MOD + MOD) % MOD

    operator fun plus(other: ModInt): ModInt {
        return ModInt(x + other.x)
    }

    operator fun minus(other: ModInt): ModInt {
        return ModInt(x - other.x)
    }

    operator fun times(other: ModInt): ModInt {
        return ModInt(x * other.x)
    }

    operator fun div(other: ModInt): ModInt {
        return this * other.inv()
    }

    fun pow(exp: Long): ModInt {
        if (exp == 0L) return ModInt(1L)
        var a = pow(exp shr 1)
        a *= a
        if (exp and 1L == 0L) return a
        return this * a
    }

    fun inv(): ModInt {
        return this.pow(MOD - 2)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as ModInt

        if (x != other.x) return false

        return true
    }

    override fun hashCode(): Int {
        return x.hashCode()
    }

    override fun toString(): String {
        return "$x"
    }
}

fun Long.toModInt(): ModInt {
    return ModInt(this)
}

fun Int.toModInt(): ModInt {
    return ModInt(this)
}

fun binomSimple(n: Long, k: Long): ModInt {
    var numer = ModInt(1)
    var denom = ModInt(1)
    for (i in 0 until k) {
        numer *= ModInt(n - i)
        denom *= ModInt(k - i)
    }
    return numer / denom
}
// endregion

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0