ARC061E 题解

Link: https://atcoder.jp/contests/arc061/tasks/arc061_c

Wrong Solution

First of all, we should know that the whole map should be considered as a graph with stations as vertices and railways as edges. Now a solution should appear in your brain: run 0-1 BFS with an edge weighted:

  • \(1\) if the endpoints aren’t managed by the same company, and
  • \(0\) otherwise.

Unfortunately, it failed at 21 (out of 59) test cases.

Correct Solution

While we store the graph (be sure to make it directed), we split the node \(u\) into multiple nodes: \((u, c_i)\) with \(c_i\) from all companies of every edges node \(u\) be connected to. For instance, we can split node \(1\) in Sample Input 1 into \((1,1)\) and \((1,2)\). Then we connect every pair of nodes \((u_i, c_i)\) and \((v_i, c_i)\) if, in the original graph, \(u_i\) and \(v_i\) have an edge weighted \(c_i\).

Now another problem appears: how do we do transfers? We can set up nodes like \((u_i, 0)\) and connect every \((u_i, c_i)\) with it, with edges weighted \(0\), and connect \((u_i, 0)\) to every \((u_i, c_i)\) with edges weighted \(1\).

At last, we only need to run Dijkstra on the new graph that begins at \((1, 0)\) and ends at \((n, 0)\) and the problem is solved.

Time complexity: \(O((N+M) \log M)\)

posted @ 2025-08-04 08:40  David9006  阅读(11)  评论(0)    收藏  举报