結果
問題 | No.3013 ハチマキ買い星人 |
ユーザー |
![]() |
提出日時 | 2025-01-25 12:46:28 |
言語 | Nim (2.2.0) |
結果 |
AC
|
実行時間 | 255 ms / 2,000 ms |
コード長 | 17,732 bytes |
コンパイル時間 | 3,556 ms |
コンパイル使用メモリ | 80,392 KB |
実行使用メモリ | 23,472 KB |
最終ジャッジ日時 | 2025-01-25 22:20:52 |
合計ジャッジ時間 | 11,326 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 45 |
ソースコード
import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2]) ImportExpand "cplib/tmpl/sheep.nim" <=== "when not declared CPLIB_TMPL_SHEEP:\n const CPLIB_TMPL_SHEEP* = 1\n {.warning[UnusedImport]: off.}\n {.hint[XDeclaredButNotUsed]: off.}\n import algorithm\n import sequtils\n import tables\n import macros\n import math\n import sets\n import strutils\n import strformat\n import sugar\n import heapqueue\n import streams\n import deques\n import bitops\n import std/lenientops\n import options\n #入力系\n proc scanf(formatstr: cstring){.header: \"<stdio.h>\", varargs.}\n proc getchar(): char {.importc: \"getchar_unlocked\", header: \"<stdio.h>\", discardable.}\n proc ii(): int {.inline.} = scanf(\"%lld\\n\", addr result)\n proc lii(N: int): seq[int] {.inline.} = newSeqWith(N, ii())\n proc si(): string {.inline.} =\n result = \"\"\n var c: char\n while true:\n c = getchar()\n if c == ' ' or c == '\\n' or c == '\\255':\n break\n result &= c\n #chmin,chmax\n template `max=`(x, y) = x = max(x, y)\n template `min=`(x, y) = x = min(x, y)\n #bit演算\n proc `%`*(x: int, y: int): int =\n result = x mod y\n if y > 0 and result < 0: result += y\n if y < 0 and result > 0: result += y\n proc `//`*(x: int, y: int): int{.inline.} =\n result = x div y\n if y > 0 and result * y > x: result -= 1\n if y < 0 and result * y < x: result -= 1\n proc `%=`(x: var int, y: int): void = x = x%y\n proc `//=`(x: var int, y: int): void = x = x//y\n proc `**`(x: int, y: int): int = x^y\n proc `**=`(x: var int, y: int): void = x = x^y\n proc `^`(x: int, y: int): int = x xor y\n proc `|`(x: int, y: int): int = x or y\n proc `&`(x: int, y: int): int = x and y\n proc `>>`(x: int, y: int): int = x shr y\n proc `<<`(x: int, y: int): int = x shl y\n proc `~`(x: int): int = not x\n proc `^=`(x: var int, y: int): void = x = x ^ y\n proc `&=`(x: var int, y: int): void = x = x & y\n proc `|=`(x: var int, y: int): void = x = x | y\n proc `>>=`(x: var int, y: int): void = x = x >> y\n proc `<<=`(x: var int, y: int): void = x = x << y\n proc `[]`(x: int, n: int): bool = (x and (1 shl n)) != 0\n #便利な変換\n proc `!`(x: char, a = '0'): int = int(x)-int(a)\n #定数\n #[ include cplib/utils/constants ]#\n when not declared CPLIB_UTILS_CONSTANTS:\n const CPLIB_UTILS_CONSTANTS* = 1\n const INF32*: int32 = 100100111.int32\n const INF64*: int = int(3300300300300300491)\n const INF = INF64\n #converter\n\n #range\n iterator range(start: int, ends: int, step: int): int =\n var i = start\n if step < 0:\n while i > ends:\n yield i\n i += step\n elif step > 0:\n while i < ends:\n yield i\n i += step\n iterator range(ends: int): int = (for i in 0..<ends: yield i)\n iterator range(start: int, ends: int): int = (for i in\n start..<ends: yield i)\n\n #joinが非stringでめちゃくちゃ遅いやつのパッチ\n proc join*[T: not string](a: openArray[T], sep: string = \"\"): string = a.mapit($it).join(sep)\n\n proc dump[T](arr:seq[seq[T]])=\n for i in 0..<len(arr):\n echo arr[i]\n" ImportExpand "cplib/graph/graph.nim" <=== "when not declared CPLIB_GRAPH_GRAPH:\n const CPLIB_GRAPH_GRAPH* = 1\n\n import sequtils\n import math\n type DynamicGraph*[T] = ref object of RootObj\n edges*: seq[seq[(int32, T)]]\n len*: int\n type StaticGraph*[T] = ref object of RootObj\n src*, dst*: seq[int32]\n cost*: seq[T]\n elist*: seq[(int32, T)]\n start*: seq[int32]\n len*: int\n\n type WeightedDirectedGraph*[T] = ref object of DynamicGraph[T]\n type WeightedUnDirectedGraph*[T] = ref object of DynamicGraph[T]\n type UnWeightedDirectedGraph* = ref object of DynamicGraph[int]\n type UnWeightedUnDirectedGraph* = ref object of DynamicGraph[int]\n type WeightedDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n type WeightedUnDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n type UnWeightedDirectedStaticGraph* = ref object of StaticGraph[int]\n type UnWeightedUnDirectedStaticGraph* = ref object of StaticGraph[int]\n\n type GraphTypes*[T] = DynamicGraph[T] or StaticGraph[T]\n type DirectedGraph* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph\n type UnDirectedGraph* = WeightedUnDirectedGraph or UnWeightedUnDirectedGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n type WeightedGraph*[T] = WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T] or WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T]\n type UnWeightedGraph* = UnWeightedDirectedGraph or UnWeightedUnDirectedGraph or UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n type DynamicGraphTypes* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedUnDirectedGraph or UnWeightedUnDirectedGraph\n type StaticGraphTypes* = WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n\n proc add_edge_dynamic_impl*[T](g: DynamicGraph[T], u, v: int, cost: T, directed: bool) =\n g.edges[u].add((v.int32, cost))\n if not directed: g.edges[v].add((u.int32, cost))\n\n proc initWeightedDirectedGraph*(N: int, edgetype: typedesc = int): WeightedDirectedGraph[edgetype] =\n result = WeightedDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n proc add_edge*[T](g: var WeightedDirectedGraph[T], u, v: int, cost: T) =\n g.add_edge_dynamic_impl(u, v, cost, true)\n\n proc initWeightedUnDirectedGraph*(N: int, edgetype: typedesc = int): WeightedUnDirectedGraph[edgetype] =\n result = WeightedUnDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n proc add_edge*[T](g: var WeightedUnDirectedGraph[T], u, v: int, cost: T) =\n g.add_edge_dynamic_impl(u, v, cost, false)\n\n proc initUnWeightedDirectedGraph*(N: int): UnWeightedDirectedGraph =\n result = UnWeightedDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n proc add_edge*(g: var UnWeightedDirectedGraph, u, v: int) =\n g.add_edge_dynamic_impl(u, v, 1, true)\n\n proc initUnWeightedUnDirectedGraph*(N: int): UnWeightedUnDirectedGraph =\n result = UnWeightedUnDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n proc add_edge*(g: var UnWeightedUnDirectedGraph, u, v: int) =\n g.add_edge_dynamic_impl(u, v, 1, false)\n\n proc len*[T](G: WeightedGraph[T]): int = G.len\n proc len*(G: UnWeightedGraph): int = G.len\n\n iterator `[]`*[T](g: WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T], x: int): (int, T) =\n for e in g.edges[x]: yield (e[0].int, e[1])\n iterator `[]`*(g: UnWeightedDirectedGraph or UnWeightedUnDirectedGraph, x: int): int =\n for e in g.edges[x]: yield e[0].int\n\n proc add_edge_static_impl*[T](g: StaticGraph[T], u, v: int, cost: T, directed: bool) =\n g.src.add(u.int32)\n g.dst.add(v.int32)\n g.cost.add(cost)\n if not directed:\n g.src.add(v.int32)\n g.dst.add(u.int32)\n g.cost.add(cost)\n\n proc build_impl*[T](g: StaticGraph[T]) =\n g.start = newSeqWith(g.len + 1, 0.int32)\n for i in 0..<g.src.len:\n g.start[g.src[i]] += 1\n g.start.cumsum\n g.elist = newSeq[(int32, T)](g.start[^1])\n for i in countdown(g.src.len - 1, 0):\n var u = g.src[i]\n var v = g.dst[i]\n g.start[u] -= 1\n g.elist[g.start[u]] = (v, g.cost[i])\n proc build*(g: StaticGraphTypes) = g.build_impl()\n\n proc initWeightedDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedDirectedStaticGraph[edgetype] =\n result = WeightedDirectedStaticGraph[edgetype](\n src: newSeqOfCap[int32](capacity),\n dst: newSeqOfCap[int32](capacity),\n cost: newSeqOfCap[edgetype](capacity),\n elist: newSeq[(int32, edgetype)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*[T](g: var WeightedDirectedStaticGraph[T], u, v: int, cost: T) =\n g.add_edge_static_impl(u, v, cost, true)\n\n proc initWeightedUnDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedUnDirectedStaticGraph[edgetype] =\n result = WeightedUnDirectedStaticGraph[edgetype](\n src: newSeqOfCap[int32](capacity*2),\n dst: newSeqOfCap[int32](capacity*2),\n cost: newSeqOfCap[edgetype](capacity*2),\n elist: newSeq[(int32, edgetype)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*[T](g: var WeightedUnDirectedStaticGraph[T], u, v: int, cost: T) =\n g.add_edge_static_impl(u, v, cost, false)\n\n proc initUnWeightedDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedDirectedStaticGraph =\n result = UnWeightedDirectedStaticGraph(\n src: newSeqOfCap[int32](capacity),\n dst: newSeqOfCap[int32](capacity),\n cost: newSeqOfCap[int](capacity),\n elist: newSeq[(int32, int)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*(g: var UnWeightedDirectedStaticGraph, u, v: int) =\n g.add_edge_static_impl(u, v, 1, true)\n\n proc initUnWeightedUnDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedUnDirectedStaticGraph =\n result = UnWeightedUnDirectedStaticGraph(\n src: newSeqOfCap[int32](capacity*2),\n dst: newSeqOfCap[int32](capacity*2),\n cost: newSeqOfCap[int](capacity*2),\n elist: newSeq[(int32, int)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*(g: var UnWeightedUnDirectedStaticGraph, u, v: int) =\n g.add_edge_static_impl(u, v, 1, false)\n\n proc static_graph_initialized_check*[T](g: StaticGraph[T]) = assert g.start.len > 0, \"Static Graph must be initialized before use.\"\n\n iterator `[]`*[T](g: WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T], x: int): (int, T) =\n g.static_graph_initialized_check()\n for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n iterator `[]`*(g: UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph, x: int): int =\n g.static_graph_initialized_check()\n for i in g.start[x]..<g.start[x+1]: yield g.elist[i][0].int\n\n iterator to_and_cost*[T](g: DynamicGraph[T], x: int): (int, T) =\n for e in g.edges[x]: yield (e[0].int, e[1])\n iterator to_and_cost*[T](g: StaticGraph[T], x: int): (int, T) =\n g.static_graph_initialized_check()\n for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n \n import tables\n\n type UnWeightedUnDirectedTableGraph*[T] = object \n toi* : Table[T,int]\n v* : seq[T]\n graph* : UnWeightedUnDirectedGraph\n\n type UnWeightedDirectedTableGraph*[T] = object \n toi* : Table[T,int]\n v* : seq[T]\n graph* : UnWeightedDirectedGraph\n\n type WeightedUnDirectedTableGraph*[T,S] = object \n toi* : Table[T,int]\n v* : seq[T]\n graph* : WeightedUnDirectedGraph[S]\n\n type WeightedDirectedTableGraph*[T,S] = object \n toi* : Table[T,int]\n v* : seq[T]\n graph* : WeightedDirectedGraph[S]\n\n type UnWeightedTableGraph*[T] = UnWeightedUnDirectedTableGraph[T] or UnWeightedDirectedTableGraph[T]\n type WeightedTableGraph*[T,S] = WeightedUnDirectedTableGraph[T,S] or WeightedDirectedTableGraph[T,S]\n\n proc initUnWeightedUnDirectedTableGraph*[T](V:seq[T]):UnWeightedUnDirectedTableGraph[T]=\n for i in 0..<len(V):\n result.toi[V[i]] = i\n result.graph = initUnWeightedUnDirectedGraph(len(V))\n result.v = V\n\n proc initUnWeightedDirectedTableGraph*[T](V:seq[T]):UnWeightedDirectedTableGraph[T]=\n for i in 0..<len(V):\n result.toi[V[i]] = i\n result.graph = initUnWeightedDirectedGraph(len(V))\n result.v = V\n\n proc initWeightedUnDirectedTableGraph*[T](V:seq[T],S:typedesc = int):WeightedUnDirectedTableGraph[T,S]=\n for i in 0..<len(V):\n result.toi[V[i]] = i\n result.graph = initWeightedUnDirectedGraph(len(V),S)\n result.v = V\n\n proc initWeightedDirectedTableGraph*[T](V:seq[T],S:typedesc = int):WeightedDirectedTableGraph[T,S]=\n for i in 0..<len(V):\n result.toi[V[i]] = i\n result.graph = initWeightedDirectedGraph(len(V),S)\n result.v = V\n\n proc add_edge*[T](g: var UnWeightedTableGraph[T],u,v:int)=\n g.graph.add_edge(g.toi[u],g.toi[v])\n\n proc add_edge*[T,S](g: var WeightedTableGraph[T,S],u,v:int,cost:S)=\n g.graph.add_edge(g.toi[u],g.toi[v],cost)\n\n iterator `[]`*[T,S](g: WeightedDirectedTableGraph[T,S] or WeightedUnDirectedTableGraph[T,S], x: T): (T, S) = \n for (x,y) in g.graph[g.toi[x]]:\n yield (g.v[x],y)\n iterator `[]`*[T](g: UnWeightedDirectedTableGraph[T] or UnWeightedUnDirectedTableGraph[T], x: T): T = \n for x in g.graph[g.toi[x]]:\n yield g.v[x]\n\n" ImportExpand "cplib/graph/dijkstra.nim" <=== "when not declared CPLIB_GRAPH_DIJKSTRA:\n const CPLIB_GRAPH_DIJKSTRA* = 1\n #[ import cplib/graph/graph ]#\n #[ import cplib/utils/constants ]#\n #[ import cplib/graph/restore_shortest_path_from_prev ]#\n when not declared CPLIB_GRAPH_RESTORE_SHORTESTPATH_FROM_PREV:\n const CPLIB_GRAPH_RESTORE_SHORTESTPATH_FROM_PREV* = 1\n import algorithm\n proc restore_shortest_path_from_prev*(prev: seq[int], goal: int): seq[int] =\n var i = goal\n while i != -1:\n result.add(i)\n i = prev[i]\n result.reverse\n import std/heapqueue\n import macros\n proc restore_dijkstra_impl[T](G: DynamicGraph[T] or StaticGraph[T], start: int or seq[int], ZERO, INF: T): tuple[costs: seq[T], prev: seq[int]] =\n var\n queue = initHeapQueue[(T, int)]()\n costs = newSeq[T](len(G))\n prev = newseq[int](len(G))\n costs.fill(INF)\n prev.fill(-1)\n when start is int:\n queue.push((ZERO, start))\n costs[start] = ZERO\n else:\n for s in start:\n queue.push((ZERO, s))\n costs[s] = ZERO\n while len(queue) != 0:\n var (cost, i) = queue.pop()\n if cost > costs[i]:\n continue\n for (j, c) in G.to_and_cost(i):\n var temp = costs[i] + c\n if temp < costs[j]:\n prev[j] = i\n costs[j] = temp\n queue.push((temp, j))\n return (costs, prev)\n macro declareDijkstra(name, t, zero, inf) =\n let impl_name = ident($`name` & \"_impl\")\n quote do:\n proc `name`*(G: DynamicGraph[`t`] or StaticGraph[`t`], start: int or seq[int], ZERO: `t` = `zero`, INF: `t` = `inf`): auto =\n `impl_name`(G, start, ZERO, INF)\n declareDijkstra(restore_dijkstra, int, 0, INF64)\n declareDijkstra(restore_dijkstra, int32, 0i32, INF32)\n declareDijkstra(restore_dijkstra, float, 0.0, 1e100)\n proc restore_dijkstra*[T](G: DynamicGraph[T] or StaticGraph[T], start: int or seq[int], ZERO, INF: T): auto =\n restore_dijkstra_impl(G, start, ZERO, INF)\n proc dijkstra_impl[T](G: DynamicGraph[T] or StaticGraph[T], start: int or seq[int], ZERO, INF: T): seq[T] =\n var (costs, _) = restore_dijkstra(G, start, ZERO, INF)\n return costs\n declareDijkstra(dijkstra, int, 0, INF64)\n declareDijkstra(dijkstra, int32, 0i32, INF32)\n declareDijkstra(dijkstra, float, 0.0, 1e100)\n proc dijkstra*[T](G: DynamicGraph[T] or StaticGraph[T], start: int or seq[int], ZERO, INF: T): auto =\n dijkstra_impl(G, start, ZERO, INF)\n proc shortest_path_dijkstra_impl[T](G: DynamicGraph[T] or StaticGraph[T], start: int, goal: int, ZERO: T, INF: T): tuple[path: seq[int], cost: T] =\n var (costs, prev) = restore_dijkstra(G, start, ZERO, INF)\n result.path = prev.restore_shortest_path_from_prev(goal)\n result.cost = costs[goal]\n proc shortest_path_dijkstra*(G: DynamicGraph[int] or StaticGraph[int], start: int, goal: int, ZERO: int = 0, INF: int = INF64): tuple[path: seq[int], cost: int] =\n shortest_path_dijkstra_impl(G, start, goal, ZERO, INF)\n proc shortest_path_dijkstra*(G: DynamicGraph[int32] or StaticGraph[int32], start: int, goal: int, ZERO: int32 = 0.int32, INF: int32 = INF32): tuple[path: seq[int], cost: int32] =\n shortest_path_dijkstra_impl(G, start, goal, ZERO, INF)\n proc shortest_path_dijkstra*[T](G: DynamicGraph[T] or StaticGraph[T], start: int, goal: int, ZERO: T, INF: T): tuple[path: seq[int], cost: T] =\n shortest_path_dijkstra_impl(G, start, goal, ZERO, INF)\n" var N,M,P,Y = ii() var G = initWeightedUnDirectedGraph(N) for i in range(M): var A,B,C = ii() G.addEdge(A-1,B-1,C) var res = G.dijkstra(0) var ans = 0 for i in range(P): var D,E = ii() D-=1 ans.max = max(0,Y-res[D])//E echo ans