Last active
October 18, 2015 15:15
-
-
Save sungkmi/b8a8dac01d979cf98f73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collection.SortedSet | |
object GCampus extends App { | |
case class Road(u: Int, v: Int, c: Int) | |
def inefficientRoads(n: Int, roads: IndexedSeq[Road]): Set[Int] = { | |
def inefficientRoadsFrom(from: Int): Set[Int] = { | |
val g = (Map.empty[Int, Set[(Int, Int)]].withDefaultValue(Set.empty[(Int, Int)]) /: (roads.zipWithIndex)) { | |
case (graph, (Road(u, v, c), index)) => | |
graph updated (u, graph(u) + ((v, index))) updated (v, graph(v) + ((u, index))) | |
} | |
def dijkstra(visited: Map[Int, (Int, Set[Int])], visitable: SortedSet[(Int, Int)]) = { | |
} | |
dijkstra(Map.empty, SortedSet((0,from))) | |
Set(0) | |
} | |
0 until n map inefficientRoadsFrom reduce (_ intersect _) | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
} | |
val filename = "B-large-practice" | |
val writer = new java.io.PrintWriter(filename + ".out") | |
try { | |
process(io.Source.fromFile(filename + ".in").getLines) { s => | |
writer.println(s); writer.flush() | |
} | |
} finally { | |
writer.flush(); writer.close() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalatest._ | |
import GCampus._ | |
class GCampusTest extends FunSuite with Matchers { | |
test("sample #1") { | |
assert(inefficientRoads(3, IndexedSeq(Road(0, 1, 10), Road(1, 2, 3), Road(2, 0, 3))) === Set(0)) | |
} | |
ignore("sample case") { | |
val input = """2 | |
2 2 | |
1 4 | |
0 0 | |
0 1 | |
3 2 | |
1 2 3 | |
0 1 | |
1 2""".lines | |
val expected = """Case #1: | |
1.000000000 | |
2.000000000 | |
Case #2: | |
1.414213562 | |
2.449489743""".lines | |
lineComparison(input, expected) | |
} | |
ignore("full small case") { | |
val input = io.Source.fromFile("B-small-practice.in").getLines() | |
val expected = io.Source.fromFile("B-small-practice.out").getLines() | |
lineComparison(input, expected) | |
} | |
ignore("full large case") { | |
val input = io.Source.fromFile("A-large-practice.in").getLines() | |
val expected = io.Source.fromFile("A-large-practice.out").getLines() | |
lineComparison(input, expected) | |
} | |
def lineComparison(input: Iterator[String], expected: Iterator[String]) { | |
process(input) { s => | |
for (line <- s.lines) assert(line.trim === expected.next().trim) | |
} | |
assert(expected.hasNext === false, "Finished too fast.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment