سوال ۶۱ کنکور ارشد مهندسی کامپیوتر ۱۴۰۱
گراف جهتدار و وزندار با رأس و یال و زیرمجموعه از رأسهای گراف با اندازه حداقل داده شده است. فرض کنید وزن تمام یالهای گراف مثبت است. بهازای هر رأس از گراف، فاصله رأس از مجموعه که آن را با نمایش میدهیم عبارت است از:
که در آن برابر با طول کوتاهترین مسیر جهتدار از به در گراف است. در چه مرتبه زمانی میتوان مقادیر را بهازای تمام رأسهای از گراف محاسبه کرد؟ دقت کنید که خروجی شامل مقدار بهازای تمام رأسهای از گراف است. (بهترین گزینه را انتخاب کنید.)
پاسخ صحیح: گزینهٔ D —
The single-source-to-a-set trick
We must output for every vertex — that is values. A naive plan (run Dijkstra from each , or from each ) costs many shortest-path computations. The exam wants the one-shot construction.
Step 1 — reverse the edges
Distances are from to a sink in . Build the reverse graph (flip the direction of every edge, keeping weights). Then so now we need, for each , the shortest distance to from the nearest source in .
Step 2 — add a super-source
Add one new vertex and a -weight edge for every . In the shortest distance from to is So a single single-source shortest-path run from produces all answers simultaneously.
Step 3 — pick the algorithm and count
All weights are positive, so Dijkstra applies. The augmented graph has edges — the graph is sparse. Dijkstra with a binary heap runs in
Hence all values are computed in — option 4.
Exam trick: "shortest distance to the nearest vertex of a set" = reverse the edges + a single -weight super-source + one Dijkstra. With edges the binary-heap Dijkstra is ; do not multiply by or by .