Submission #2120354


Source Code Expand

import java.util.*

val DEBUG = false

val P = 1000000007L

data class FiniteField(val x: Long) {
  operator fun plus(o: FiniteField) = FiniteField((x + o.x) % P)
  operator fun minus(o: FiniteField) = FiniteField((x + P - o.x) % P)
  operator fun times(o: FiniteField) = FiniteField((x * o.x) % P)
}

data class Edge(val from: Int, val to: Int, val cost: Long)

data class State(val v: Int, val cost: Long)

fun compareState(s1: State, s2: State): Int {
  if (s1.cost > s2.cost) return 1
  if (s1.cost < s2.cost) return -1
  return 0
}

fun readIntList(s: String): List<Int> {
  var values = mutableListOf<Int>()
  var count = 0
  var currentInt = 0
  for (i in 0 until s.length) {
    if (s[i] == ' ') {
      values.add(currentInt)
      currentInt = 0
      count++
    } else {
      currentInt *= 10
      currentInt += s[i] - '0'
    }
  }
  values.add(currentInt)
  return values
}


fun main(args: Array<String>) {
  val (n, m) = readIntList(readLine()!!)
  val (sRaw, tRaw) = readIntList(readLine()!!)
  val s = sRaw - 1
  val t = tRaw - 1
  val graph = Array(n, { mutableListOf<Edge>() })
  repeat(m) {
    val line = readLine()!!
    val (uRaw, vRaw, dInt) = readIntList(line)
    val u = uRaw - 1
    val v = vRaw - 1
    val d = dInt.toLong()
    graph[u].add(Edge(u, v, d))
    graph[v].add(Edge(v, u, d))
  }

  // Dijkstra from s
  // Kotlin 1.0 workaround.
  val pq = PriorityQueue<State>(11, { s1: State, s2: State -> compareState(s1, s2) })
  val dist = LongArray(n, { Long.MAX_VALUE })
  dist[s] = 0
  pq.add(State(s, 0L))
  while (pq.isNotEmpty()) {
    val state = pq.poll()!!
    if (dist[state.v] < state.cost) continue
    for (e in graph[state.v]) {
      if (state.cost + e.cost < dist[e.to]) {
        dist[e.to] = state.cost + e.cost
        pq.add(State(e.to, state.cost + e.cost))
      }
    }
  }

  val sGraph = Array(n, { mutableListOf<Int>() })
  val tGraph = Array(n, { mutableListOf<Int>() })

  val stack = ArrayDeque<Int>()
  stack.offerLast(t)
  val visited = BooleanArray(n)
  while (stack.isNotEmpty()) {
    val v = stack.pollLast()
    if (visited[v]) continue
    visited[v] = true
    for (e in graph[v]) {
      if (dist[v] == dist[e.to] + e.cost) {
        tGraph[v].add(e.to)
        sGraph[e.to].add(v)
        stack.offerLast(e.to)
      }
    }
  }

  val sCount = Array(n, { FiniteField(0) })
  sCount[s] = FiniteField(1)
  val sSorted = dist.withIndex().sortedBy { it.value }.map { it.index }
  fillCount(sGraph, sSorted, sCount)

  val tCount = Array(n, { FiniteField(0) })
  tCount[t] = FiniteField(1)
  val tSorted = sSorted.reversed()
  fillCount(tGraph, tSorted, tCount)

  var ans = sCount[t] * tCount[s]
  val totalDist = dist[t]
  // Meet on vertex
  for (v in 0 until n) {
    if (dist[v] * 2 == totalDist) {
      ans -= sCount[v] * sCount[v] * tCount[v] * tCount[v]
    }
  }

  // Meet on edge
  for (v in 0 until n) {
    for (e in graph[v]) {
      if (dist[v] + e.cost == dist[e.to] && dist[v] * 2 < totalDist && dist[e.to] * 2 > totalDist) {
        ans -= sCount[v] * sCount[v] * tCount[e.to] * tCount[e.to]
      }
    }
  }

  println(ans.x)
}

fun fillCount(graph: Array<MutableList<Int>>, tSorted: List<Int>, count: Array<FiniteField>) {
  for (v in tSorted) {
    for (u in graph[v]) {
      count[u] = (count[u] + count[v])
    }
  }
}

Submission Info

Submission Time
Task E - Avoiding Collision
User ymatsux
Language Kotlin (1.0.0)
Score 700
Code Size 3444 Byte
Status AC
Exec Time 1694 ms
Memory 158516 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 4
AC × 22
Set Name Test Cases
Sample sample01.txt, sample02.txt, sample03.txt, sample04.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, sample01.txt, sample02.txt, sample03.txt, sample04.txt
Case Name Status Exec Time Memory
01.txt AC 1379 ms 136340 KB
02.txt AC 1406 ms 138156 KB
03.txt AC 1646 ms 152616 KB
04.txt AC 1546 ms 153172 KB
05.txt AC 1437 ms 134348 KB
06.txt AC 1555 ms 152624 KB
07.txt AC 1577 ms 158516 KB
08.txt AC 1450 ms 147848 KB
09.txt AC 1496 ms 151164 KB
10.txt AC 1563 ms 154588 KB
11.txt AC 1684 ms 149220 KB
12.txt AC 1694 ms 151032 KB
13.txt AC 1625 ms 157800 KB
14.txt AC 1616 ms 156736 KB
15.txt AC 1573 ms 152016 KB
16.txt AC 1591 ms 156100 KB
17.txt AC 1560 ms 155856 KB
18.txt AC 1655 ms 157060 KB
sample01.txt AC 235 ms 36012 KB
sample02.txt AC 236 ms 36044 KB
sample03.txt AC 244 ms 34108 KB
sample04.txt AC 237 ms 32096 KB