The island where JOI lives has been invaded by zombies. JOI decided to escape to the shelter, which is set as the safest shelter on the island.
The island where JOI lives consists of N towns from town 1 to town N, and the towns are connected by roads. There are M roads on the island, all of which connect two different towns. JOI can move freely on the road in both directions, but he cannot go from one town to another through anything other than the road.
Some towns are dominated by zombies and cannot be visited. A town that can be reached from a town controlled by zombies using roads of S or less is called a dangerous town. Other towns are called non-dangerous towns.
JOI's house is in town 1 and the shelter for evacuation is in town N. Towns 1 and N are not dominated by zombies. The roads on the island take a long time to move, so every time JOI moves from one town to another, he has to stay overnight in the destination town. If you stay in a non-dangerous town, you will stay in a cheap inn with a low accommodation cost of P yen, but if you stay in a dangerous town, you will stay in a luxury inn with excellent security services and an accommodation cost of Q yen. JOI wants to move to the town N so that the accommodation fee is as cheap as possible and evacuate to town N. There is no need to stay in town 1 or town N.
Find the minimum total accommodation costs that JOI will need to travel from town 1 to town N.
input
The input consists of 2 + K + M lines.
On the first line, four integers N, M, K, S (2 ≤ N ≤ 100000, 1 ≤ M ≤ 200000, 0 ≤ K ≤ N-2, 0 ≤ S ≤ 100000) are written separated by blanks. ing. It consists of N towns and M roads on the island, K of the N towns are zombie-dominated, and S or less roads are used from the zombie-controlled towns. A town that can be reached is called a dangerous town.
On the second line, two integers P and Q (1 ≤ P <Q ≤ 100000) are written with a blank as a delimiter. This means that in a town where JOI is not dangerous, you will stay in an inn with an accommodation cost of P yen, and in a dangerous town, you will stay in an inn with an accommodation cost of Q yen.
The integer Ci (2 ≤ Ci ≤ N-1) is written on the i-th line (1 ≤ i ≤ K) of the following K lines. This means that the town Ci is dominated by zombies. C1, ..., CK are all different.
In the jth line (1 ≤ j ≤ M) of the following M lines, two integers Aj and Bj (1 ≤ Aj <Bj ≤ N) are written with a blank as a delimiter. This means that there is a road between town Aj and town Bj. The same (Aj, Bj) pair has never been written more than once.
Given the input data, it is guaranteed to be able to travel from town 1 to town N only through non-zombie-controlled towns.
output
JOI Output the minimum total accommodation cost required when you move from town 1 to town N in one line.
Note that the output does not always fall within the range of 32-bit signed integers.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse this problem: given an undirected graph with N towns, M edges, K zombie towns (Ci, which are between 2 and N-1, not including 1 or N). Towns that are within distance S (in terms of number of edges) from any zombie town are considered dangerous. All other towns are non-dangerous. Towns 1 and N are not zombie towns and presumably can be dangerous or not? They are not zombie, but they could be dangerous if within S of a zombie. However, the problem states: "Some towns are dominated by zombies and cannot be visited." So we cannot ever go into a zombie town. Also, towns 1 and N are not zombie. But we can only travel through non-zombie towns? The problem says: "Given the input data, it is guaranteed to be able to travel from town 1 to town N only through non-zombie-controlled towns." So the path must avoid zombie towns entirely. So the graph we can traverse excludes the zombie nodes. Also, dangerous towns are defined based on distance from any zombie town, but they are still visitable (but with higher cost). Non-dangerous towns are safe (cheap). So we need to find a path from 1 to N that uses only non-zombie nodes (i.e., we cannot include zombie nodes in the path). However, note: some nodes may be dangerous due to proximity to zombies, but they are allowed. So effectively we have a graph with nodes that are not zombie nodes. Among those nodes, each has a cost: P if it's non-dangerous, Q if it's dangerous. The cost is incurred when you stay overnight at the destination town. So if you start at town 1, you don't pay for staying at 1. Then you move to some neighbor, you pay the cost of that neighbor (unless that neighbor is N, then you don't need to stay). Then from there you move to another neighbor, paying its cost (if not N), and so on. Ultimately you end at N without paying its cost. So total cost = sum of costs of intermediate nodes visited (including possibly 1? No, 1 is start, no stay; N is end, no stay). So we need to find a path from 1 to N that minimizes sum of node costs (excluding endpoints). The path is in the graph after removing zombie nodes. So it's a weighted graph where each non-endpoint node has weight (cost) depending on its danger status, and we want the shortest path in terms of sum of node weights. However, note that we also have to pay for nodes that we stay in even if they are dangerous, but we don't pay for start and end. Also, the path cost is not additive on edges but on nodes. This can be transformed to an edge-weighted problem by assigning to each edge (u,v) the cost of node v (if v is not N) plus maybe some extra? Actually typical transformation: we can think of cost of traversing an edge from u to v as paying the cost of staying at v (except if v = N, then no cost). But then starting from 1, we don't pay cost for 1. So we need to find a path minimizing sum of costs of visited nodes (excluding 1 and N). This is equivalent to: assign each node i a cost c_i: c_i = P if safe (non-dangerous) and i != 1,N; c_i = Q if dangerous and i != 1,N; c_1 = 0; c_N = 0. Then total cost of a path 1 -> v1 -> v2 -> ... -> vk -> N is sum_{i=1..k} c_{vi}. But we also might pay cost for nodes visited multiple times? But we presumably don't revisit nodes; but even if we do, we'd have to pay each time we stay? The problem: "every time JOI moves from one town to another, he has to stay overnight in the destination town." So if you revisit a town, you'd pay again. But shortest path would avoid cycles because all costs are positive (P, Q > 0). So the optimal path is simple. So we can model as a shortest path problem where edge weight from u to v is c_v, except when v = N then weight 0. But careful: If we go from u to N, we don't pay for N. So we can set edge weight to c_v for v != N, and 0 for v == N. Then the total cost of path is sum of edge weights along the path. However, start node 1 cost is not included, which is fine because we don't have an incoming edge to 1.
But this graph is directed? Since moving from u to v yields cost c_v. But moving from v to u yields cost c_u. So the graph is undirected but edges have weights that depend on direction. So we need to find the shortest path from 1 to N in a directed graph where each undirected edge becomes two directed edges with possibly different weights: for edge (u,v), the directed edge u->v has weight cost(v) if v != N else 0; v->u has weight cost(u) if u != N else 0. Then run Dijkstra on this directed graph with positive weights.
Alternatively, we could assign node costs to edges in a more symmetric way: assign to each node a weight (except 1 and N). Then path cost = sum of weights of internal nodes visited = sum of weights of nodes on path except start and end. This is like we want to minimize sum of node weights, which can be solved by Dijkstra on an undirected graph if we transform each node weight into half of something? Not straightforward. But we can use the directed approach: For each node i, we define w_i = (i==1 or i==N?0: cost_i). Then for each undirected edge (u,v), we add directed edges: u->v with weight w_v, v->u with weight w_u. Then from 1 to N we want the shortest directed path. This is valid because each step adds the cost of the destination node (if not N) which matches the accommodation fee. And starting at 1 adds nothing. So the total cost is sum_{edges along path} w_{dest}. That equals sum of w_i for all intermediate nodes visited, with possible duplicates if we revisit, but duplicates would increase cost. So Dijkstra will find optimal simple path (since no negative weights). So that's straightforward.
The main challenge is to compute which towns are dangerous. We are given K zombie towns. Danger classification: A town is dangerous if its distance (in terms of number of edges) from any zombie town is <= S. This distance is computed in the original graph? Or only using roads, but note that zombie towns themselves are "dominated by zombies and cannot be visited." However, does danger propagation consider the entire graph including zombie towns? Usually yes: distance from zombie towns is measured via any roads, even those that go through other zombie towns? But since zombie towns cannot be visited, but the concept of distance is purely graph-theoretic: a town is dangerous if there exists a path from a zombie town to that town of length at most S (edges) without any restriction? Usually yes; they say "A town that can be reached from a town controlled by zombies using roads of S or less is called a dangerous town." It doesn't say we can't pass through other towns, even zombie ones? But if a town is zombie, we can't be there, but for distance measurement, it's just graph distance. Typically, in such problems (like JOI 2016 qual?), the propagation goes through any towns. So we need to compute for each town, the minimum distance (in edges) to any zombie town. Then if that distance <= S, the town is dangerous. But note: Zombie towns themselves are not visitable, but they are also obviously dangerous? However, we don't care because they are forbidden. But the definition likely includes the zombie towns themselves as dangerous? But they are also unreachable, but anyway. The dangerous status applies to other towns; but for the purpose of staying costs, if we cannot visit zombie towns, we don't need to assign a cost to them. So we only need to compute danger for non-zombie towns (including 1 and N? They are not zombie but could be dangerous; and they are visitable. So we need to assign cost for 1 and N? The problem says: "If you stay in a non-dangerous town, you will stay in a cheap inn... If you stay in a dangerous town, you will stay in a luxury inn... JOI wants to move to the town N so that the accommodation fee is as cheap as possible... There is no need to stay in town 1 or town N." So towns 1 and N are never stayed in, so we don't care about their danger status for cost. However, they might be used as intermediate nodes in Dijkstra? Actually they are nodes we can be at, but we only pay when we stay overnight at destination, so we never pay for staying at start or end. For a path that goes 1 -> ... -> N, we don't pay for N. So for node N, its cost is irrelevant; for node 1, we never "arrive" there, so we don't pay. But we may pass through other nodes, paying each time we arrive. So in our directed edge weight transformation, we set w_N = 0, w_1 = 0, and for other nodes i, w_i = P if i is non-dangerous, Q if dangerous.
Thus, we need to compute for each node i (1..N) whether it is dangerous (distance <= S from any zombie). But note: Zombie nodes themselves have distance 0 to themselves, but they are not used. However, they can propagate danger to neighbors, etc.
So steps:
Read N, M, K, S.
Read P, Q.
Read list of K zombie towns: Ci.
Read M edges.
Compute dangerous towns:
We can run a multi-source BFS from all zombie towns, considering the entire graph (including all nodes). The BFS distance d[i] = minimum number of edges from any zombie town to i.
Then town i is dangerous if 1 <= d[i] <= S? But also if i is a zombie? d[i]=0 for zombie towns, but they are not allowed to visit; we might still mark them as dangerous but not used. But for non-zombie towns, if d[i] <= S and d[i] >= 1? Actually if a town is not zombie but is within S steps, then it is dangerous. But what about zombie towns? They are dangerous but we can't visit. So for marking cost, we only care about non-zombie towns. So we can compute for all nodes: dangerous if 0 < d[i] <= S (since distance 0 means it is a zombie, but we will exclude these nodes anyway). However, what about a town that is not a zombie but distance 0? That would mean it's a zombie, impossible. So we can set dangerous if 0 <= d[i] <= S but i not in zombie set? But it's easier: for each node, if d[i] <= S and i is not a zombie and i != 1,N? Actually for 1 and N, we don't care about cost, but they could be dangerous, but we will set w=0 for them anyway. So we can compute danger flag for all nodes, then assign costs: if i == 1 or i == N: w=0. Else if i is a zombie: we exclude from graph (or not consider as a vertex). But better: we will construct a graph for Dijkstra that only includes non-zombie nodes (since we cannot visit zombies). So we need to know which nodes are allowed. Allowed nodes = all nodes except zombie towns. That includes 1, N, and other towns (dangerous or not). For each allowed node i (i not zombie), its cost w_i = P if not dangerous, Q if dangerous. But for i=1,N, w_i=0 regardless? Actually the problem says: "There is no need to stay in town 1 or town N." So we should not pay for staying at these towns. So we set w_1 = w_N = 0. However, note that if we follow the directed edge weight rule, for a move from some node to N, we don't pay for N; that's fine. For moves from 1 to somewhere, we pay for the somewhere. So setting w_N=0 works. For w_1, we never use it as destination because we start at 1, no incoming edge to 1. So w_1 can be anything but we set 0 for consistency.
But careful: Could we pass through 1 again? Not needed; if we ever return to 1, we would pay for staying there? Actually if we move to 1 from some node, we would have to stay overnight at 1, and that would be a cost. But that seems suboptimal. However, it's allowed? Since 1 is not a zombie and we could revisit it, but we would have to pay. The problem statement doesn't forbid visiting 1 again, but logically it's unnecessary because costs are positive and it would increase cost. So optimal path will not revisit 1. Similarly, we might visit N before final arrival? Actually if we reach N, we are done, so no need to leave. So we can ignore cycles.
Thus, we construct the allowed graph: all nodes except zombie towns. Then run Dijkstra on the directed version with edge weights as w_dest.
But note that Dijkstra on directed graph with up to N=100k, M=200k is fine.
Now we need to compute BFS distances from zombie towns. That BFS is on the entire graph (including all nodes and edges). N up to 100k, M up to 200k, so BFS is O(N+M). Use adjacency list.
Implementation details:
Input: first line: N, M, K, S.
Second line: P, Q.
K lines: each Ci.
M lines: Aj, Bj.
We'll store edges in adjacency list.
BFS: initialize dist array of size N+1 with INF. For each zombie node, set dist[z]=0 and push into queue. Then BFS. Since graph is undirected, BFS will compute min distance to any zombie.
After BFS, determine for each node i (1..N) dangerous flag: danger[i] = (dist[i] <= S). But note: zombie nodes have dist=0 <= S, so they are dangerous, but we will not include them in the allowed set. So we need to differentiate. We'll define allowed[i] = (i not in zombie set). Actually we might also want to exclude nodes that are dangerous? No, they are allowed, just with higher cost.
For each allowed node i (i not zombie), assign weight:
if i == 1 or i == N: w[i] = 0
else if danger[i] is true: w[i] = Q
else: w[i] = P
But is there any nuance: The problem states: "If you stay in a non-dangerous town, you will stay in a cheap inn with a low accommodation cost of P yen, but if you stay in a dangerous town, you will stay in a luxury inn with excellent security services and an accommodation cost of Q yen." So dangerous towns cost Q, safe towns cost P. However, note that towns 1 and N we never stay in, so we set w=0, but also if we consider that moving into N does not require staying, so indeed no cost.
Build adjacency list for allowed nodes only: we need to consider only edges where both endpoints are allowed (non-zombie). Because if an edge connects to a zombie town, that town cannot be visited, so we cannot traverse that edge (since it would require visiting the zombie town). However, is it allowed to pass through a zombie town? The problem says: "Some towns are dominated by zombies and cannot be visited." So any path that includes a zombie town is not allowed. So we must exclude edges incident to zombie towns from the traversable graph. Therefore, when constructing the graph for Dijkstra, we only include edges where both u and v are not zombie towns. So we need to filter.
Then Dijkstra: directed edges: for each undirected edge (u,v) in filtered edges, we add:
For node u, neighbor v: weight = w[v]
For node v, neighbor u: weight = w[u]
But note: if v == N, then w[v] = 0; so that's fine.
Then run Dijkstra from start node 1. The distance array d[i] = minimum cost to reach node i. Initially d[1] = 0. For each neighbor v of u, if d[u] + w[v] < d[v], update.
The answer is d[N] (since we end at N, we don't pay for N). But we should verify: what about cost of staying at towns along the way: This sum includes all intermediate nodes, including possibly the node just before N? Yes, we pay for that node when we arrive there from previous, then move to N and not pay. So correct.
Potential pitfalls: The costs are large and N up to 100k, Q up to 100k, P up to 100k, path length up to N, so total cost up to 1e10, which fits in 64-bit. Use long long.
Complexity: O(N + M) for BFS, O(M log N) for Dijkstra.
Let's test with given examples.
Example 1:
N=13, M=21, K=1, S=1; P=1000, Q=6000; zombie: 7.
We need to compute distances from zombie 7:
Graph edges as given. Let's manually compute dangerous towns: distance <=1 from town 7. So neighbors of 7 are? From edges: (3,7), (4,7), (6,7), (7,12), (7,8) maybe? In input list: we have edges: 3 7, 4 7, 6 7, 7 12, 7 8? Let's check input:
list of edges:
1 2
3 7
2 4
5 8
8 9
2 5
3 4
4 7
9 10
10 11
5 9
7 12
3 6
4 5
1 3
11 12
6 7
8 11
6 13
7 8
12 13
So edges involving 7: 3-7, 4-7, 7-12, 6-7, 7-8. So neighbors: 3,4,6,12,8. So dangerous towns: all towns at distance 1 from zombie: 3,4,6,12,8 (and also maybe 7 itself but it's zombie). Also towns at distance 0? Only zombie. So dangerous: 3,4,6,8,12. That matches sample description: "town 3, town 4, town 6, town 8, and town 12 are dangerous towns." Good.
Allowed nodes: all except 7. Costs: non-dangerous: P=1000; dangerous: Q=6000; 1 and 13 (N) have cost 0.
Now Dijkstra: Path sample: 1->2->5->9->10->11->12->13. Node costs: 2 (safe? distance from zombie 7: 2 is not listed dangerous; compute: distance from 7 to 2: 2? path 7-3-1-2? or 7-4-2? Actually 7-4-2: 2 edges, distance 2 >1 => safe. So w[2]=1000; 5? distance from 7: 7-4-5 distance 2 => safe -> 1000; 9? 7-8-9 distance 2 => safe -> 1000; 10? 9-10 distance 3 from 7? 7-8-9-10 distance 3 => safe; 11? 7-8-11? 7-8-11 distance 2? Actually 7-8-11 is two edges? 7-8 and 8-11 => distance 2; but S=1 so safe. So 11 safe -> 1000; 12 dangerous -> 6000; 13 safe? 13 is N, cost 0. So total sum: 1000+1000+1000+1000+1000+6000 = 11000. matches.
So algorithm works.
Now Example 2:
We can test quickly: N=21, M=26, K=2, S=2; P=1000, Q=2000; zombies: 5, 16.
Compute dangerous: distance <=2 from any zombie. Then run Dijkstra.
But we trust.
Edge Cases: S=0. Then dangerous towns are those at distance 0 from zombies, i.e., only zombie towns. But zombie towns are not allowed, so all allowed towns are safe (cost P). So just shortest path in terms of number of steps? Actually with node costs all P (except endpoints 0), we effectively want to minimize number of intermediate nodes (since each has cost P). That is equivalent to finding shortest path in terms of number of edges (since cost = P * (number of nodes visited except start and end) = P * (path length in edges). So minimize number of edges, i.e., BFS. But Dijkstra with uniform weights will also work.
If S is large (>= N), then all towns within distance N from zombies are dangerous? Actually all towns reachable from zombies would be dangerous. But zombies might not be connected to all? But if graph is connected? Not necessarily, but towns 1 and N are guaranteed to be connected via non-zombie towns. However, some towns may be unreachable from zombies? Possibly, if they are in another component not containing zombies. Those would be safe. So we need to compute distances; if unreachable, distance = INF > S, safe.
Now about BFS: need to handle multiple sources.
Implementation Steps:
Read N, M, K, S.
Read P, Q.
Create vector zombies(K). Read them.
Build adjacency list (vector<vector> adj(N+1)) for all edges.
BFS:
vector dist(N+1, -1 or INF). Use -1 or large number.
queue q.
For each zombie z: dist[z] = 0; q.push(z).
While q not empty: pop u, for each v in adj[u]: if dist[v]==-1: dist[v] = dist[u]+1; q.push(v).
We can use INF = 1e9.
Determine allowed and cost:
vector isZombie(N+1, false); mark zombies.
vector cost(N+1, 0); but we only need for allowed nodes. Could create separate list for Dijkstra.
For i=1..N: if isZombie[i]: continue; else: if i==1 or i==N: cost[i]=0; else if dist[i] <= S: cost[i]=Q; else: cost[i]=P.
Build adjacency for Dijkstra: only for edges where both endpoints not zombie. We'll create vector<vector> graph(N+1). For each edge (u,v) from input: if !isZombie[u] && !isZombie[v], then add u->v and v->u in Dijkstra's neighbor list? Actually for Dijkstra we need directed edges with weights, but we can store neighbor list with weights: for each node, we need a list of (neighbor, weight) where weight = cost[neighbor] (the cost of staying at neighbor). So we can construct adjacency list with edges: For each allowed edge (u,v):
graph[u].push_back({v, cost[v]});
graph[v].push_back({u, cost[u]});
But careful: cost[N] = 0, so edges into N have weight 0; edges out of N? Actually we never need to go out of N because we stop at N; but Dijkstra might consider leaving N if we add edges. That's okay; but Dijkstra will not explore leaving N if we stop when we pop N? Actually we can run Dijkstra until we pop N, but even if we add edges leaving N, they may be considered but that's fine because distances to other nodes may be updated, but we can stop when we pop N. However, it's simpler to run full Dijkstra; complexity is fine.
So we can build the directed edges as described.
while not empty:
auto [curDist, u] = pq.top(); pq.pop();
if curDist != d[u] continue;
if u == N: break? We can break early but not required.
for each [v, w] in graph[u]:
if d[v] > d[u] + w) {
d[v] = d[u] + w;
pq.push({d[v], v});
}
Output d[N].
Now, we need to consider that 1 and N are not zombie and guaranteed to be connected through allowed nodes. So Dijkstra will find a finite answer.
Potential pitfalls: The cost for nodes might be large (Q up to 100000) and path may have many nodes (up to N). So d[N] up to 100000 * 100000 = 1e10, fits in 64-bit.
Also, note that we might consider the case where N is dangerous? But we set w[N]=0, so it's okay.
Now, check sample 2 manually? Let's run through quickly with algorithm.
First from 5: distances: 5's neighbors: from edges: 2 5, 5 8? Actually edges: (2,5), (5,8). So neighbors 2,8. So distance 1: 2,8.
From 16: edges: (13,16), (15,16), (16,17), (16,19). Also (13,16) from list: 13 16, 15 16, 16 17, 16 19. So neighbors: 13,15,17,19. So distance 1: 13,15,17,19.
Now BFS multi-source: set dist[5]=0, dist[16]=0. Then propagate.
Distance 1 from any zombie: includes 2,8,13,15,17,19. Also maybe from 5 to? 5's neighbors: 2,8. From 16's neighbors: 13,15,17,19. Also check if any other node reachable within 1? No.
Distance 2: nodes adjacent to distance-1 nodes, but not already visited.
From 2: neighbors: 1,5. 1 not visited, distance 2; 5 already visited. So 1 gets distance 2 from 5 via 2? Actually 1 is neighbor of 2, so dist[1] = min(dist[1], dist[2]+1) = 2. But note that 1 is also neighbor of 10, but not yet. So 1 becomes distance 2. Also from 2 maybe other neighbors? Only 1 and 5.
From 8: neighbors: 5,10? Edges: 5 8, 8 10? Yes, edge 8 10. So neighbors: 5 (already zombie), 10. So 10 gets distance 2 (from 5 via 8). Also maybe 8? already visited? Actually 8 is distance 1, but we already processed. So 10 becomes distance 2.
From 13: neighbors: 11,12,14,16. So 11,12,14 become distance 2 (since 13 is distance 1). Actually 11 edge 11 13; 12 edge 12 13; 14 edge 13 14; 16 is zombie. So 11,12,14 get distance 2.
From 15: neighbors: 12,16,18. So 12 already maybe distance 2 from 13 as well; 18 gets distance 2. 16 zombie.
From 17: neighbors: 14,16,20. So 14 already maybe; 20 gets distance 2. 16 zombie.
From 19: neighbors: 16,18,20,21? Edges: 16 19, 18 19, 19 20, 19 21. So 18 (maybe distance 2 already from 15? Actually 18 from 15 is distance 2? 15->18 is one edge, so 18 distance 2 via 15; but via 19 also distance 2, same); 20 distance 2; 21 distance 2.
Now distance 3: nodes adjacent to distance-2 nodes not visited. But S=2, so we only care up to distance 2. So dangerous towns are those with dist <=2 except zombies themselves? Also note that towns that are zombies have dist=0 but not allowed. So dangerous towns: all nodes (except zombies) with dist <=2. That includes: 2,8,13,15,17,19 (distance 1) and 1,10,11,12,14,18,20,21 (distance 2). Also maybe other nodes? Let's check node 3? Distance from 5: 5->2->1->3? That's distance 3? Actually 5 to 3: 5-2-1-3: 3 edges (5->2,2->1,1->3) => distance 3, not dangerous. Node 4? 5->2->1->3->4? That's 4 edges? Actually alternative: 5->8->10->9->... not within 2. So 4 safe. Node 6? 5->8->10->9->... maybe distance >2. Node 7? 5->8->10->9->7? Actually 7 connects to 9? edges: 6 7, 7 9. So 7 is distance from 5: 5->8->10->9->7? 4 edges. So safe. Node 9? 5->8->10->9: 3 edges? Actually 5->8 (1), 8->10 (2), 10->9 (3) => distance 3. So safe. But also from 16: 16->19->20? Not 9. So 9 is distance 3? Possibly from 16 to 9: 16->13->11->9? 13->11 (1 from 13), 11->9 (2 from 13), so 16->13 (1), 13->11 (2), 11->9 (3). So distance 3. So safe. Node 6? distance? 16->13->14->17? Not 6. 5->8->10->? Not 6. So safe. Node 4? Not dangerous. So list matches likely.
Now allowed nodes: all except 5 and 16. Costs: dangerous towns (dist<=2) cost Q=2000, safe towns cost P=1000, with 1 and N=21 cost 0? Actually 1 is dangerous (dist 2), but cost should be 0 since it's start, not paid. So w[1]=0. N=21 is dangerous? dist[21] = 2 (from 19) so dangerous, but we set w[21]=0 as N. So okay.
Now we need shortest path from 1 to 21. Sample answer 15000. Let's see possible path: The safe route might try to avoid dangerous towns where possible, but many are dangerous. The path given? Not provided, but we can compute: cost of path: If we go through safe towns (cost 1000) and dangerous (2000). Since answer 15000, that sum of costs of intermediate nodes (excluding 1 and 21) = 15000. So either 15 safe nodes, or combination. But likely the optimal path is something like 1 -> something -> ... -> 21.
We can trust algorithm.
Now we need to consider the scenario where some nodes have cost Q but still we might need to go through them. Dijkstra with node costs works.
But wait: The transformation using directed edges with weight = cost[dest] is correct as long as we never have to pay for the starting node. But what about moving from 1 to a neighbor: we pay cost[neighbor] correctly. For subsequent moves, we pay cost[dest] each time. That matches.
But one nuance: If we go from u to v, we pay cost of v. But what about the cost of u? The cost of u was already paid when we arrived at u from its predecessor. So no double counting. Good.
Thus algorithm stands.
Now we must ensure that the graph for Dijkstra is built only on allowed nodes. Also, we should exclude zombie nodes entirely. For BFS we used the full graph to compute distances. That's fine.
Now we think about potential pitfalls: The BFS distances from zombies to nodes may be computed through zombie nodes? Yes, because BFS uses all nodes; zombie nodes are sources and also intermediate nodes. That's correct because the definition doesn't restrict travel through zombie towns for danger propagation? It says "A town that can be reached from a town controlled by zombies using roads of S or less is called a dangerous town." That implies that you can "reach" it via roads; but if that path goes through other zombie towns, is that allowed? Typically, reachable means through any sequence of roads; there is no restriction that you cannot pass through zombie towns because those towns are dominated but they are still physically there; the definition is about proximity, not about visiting. So using BFS on the full graph is correct.
Now consider the case where S = 0. Then only zombies are dangerous. But we are excluding zombies anyway, so all allowed nodes are safe. However, there is a nuance: The definition of dangerous includes the zombies themselves? But they are not allowed. So fine.
Now we need to confirm the guarantee: "Given the input data, it is guaranteed to be able to travel from town 1 to town N only through non-zombie-controlled towns." So there exists at least one path that avoids all zombie nodes. That's important for Dijkstra to have a solution.
Now complexity: N up to 100k, M up to 200k. BFS O(N+M). Dijkstra with adjacency list size up to 2 * (M filtered). In worst case, all nodes allowed, so 2M directed edges, so about 400k edges. Dijkstra O((N+M) log N) which is fine.
Memory: store adjacency list for full graph for BFS (size 2M) and also for Dijkstra (similar). We could reuse the same adjacency structure but we need the filtered edges. Since BFS and Dijkstra both need adjacency, we can store edges once and then for Dijkstra we can create a new adjacency list that only includes edges between non-zombie nodes. Or we could store the full adjacency and when we run Dijkstra, we check at runtime if neighbor is allowed; but then we would still need to know cost for neighbor, and we would need to skip if neighbor is zombie. That would avoid duplicating adjacency list. However, we need to store directed weights; we could simply have the full adjacency list (for all nodes) and in Dijkstra, for each neighbor v of u, if v is zombie then skip; else consider with weight = cost[v]. This works and saves memory, as we only need one adjacency list. But BFS also uses same adjacency. So we can just build one adjacency list of the full graph (undirected). Then:
For BFS, we use it.
For Dijkstra, we iterate over neighbors of u (from full adjacency), skip if v is zombie, and use cost[v] as edge weight.
That is simpler and reduces memory. We'll do that.
Thus steps:
Build vector<vector> adj(N+1) for all edges.
BFS multi-source using this adj.
Determine isZombie array.
Determine cost array for all nodes (cost[zombie] irrelevant but we can set something).
Dijkstra: use a priority queue. For each u popped, for each v in adj[u], if isZombie[v] continue; else tentative = d[u] + cost[v] (but careful: cost[v] is the cost to stay at v; but we only add cost when moving into v. That's correct). However, we need to treat destination N specially: cost[N] = 0. So cost array already set: for N, cost=0; for 1, cost=0; for other allowed, cost = P or Q accordingly.
Thus Dijkstra will automatically handle N: cost[N] = 0, so moving into N adds 0.
Now also note: Starting node 1: d[1] = 0. No need to pay to be at 1.
Thus algorithm:
Read input.
Build adjacency list.
BFS to compute min distance to zombies.
Determine cost array.
Dijkstra on allowed nodes (skip zombie neighbors). Output d[N].
Edge Cases: When S is negative? Not possible, S >= 0.
Potential pitfalls: The BFS distance from zombie to a node may be INF if not reachable (e.g., separate component). Those nodes are safe (since dist > S). So cost = P.
Now test with some custom scenario: Suppose a node is within distance S of zombie, but is itself a zombie? Actually we skip zombies anyway. So cost not used.
Now we need to consider that the path may need to go through node 1 or N as intermediate? Not typical. But could we have a path like 1 -> ... -> 1 -> ...? Not optimal.
Now confirm that we don't need to pay for 1 if we start there. That's correct.
Now verify sample 1 with our approach: adjacency built from full graph. BFS distances: from zombie 7, we compute distances. Then cost array: 1:0; 2: safe (dist 2 >1 => 1000); 3: dangerous => 6000; 4: dangerous => 6000; 5: safe? dist from 7 to 5: 7-4-5 distance 2 >1 => 1000; 6: dangerous => 6000; 7: zombie (skip but cost maybe irrelevant); 8: dangerous => 6000; 9: safe? 7-8-9 distance 2 >1 => 1000; 10: safe? 7-8-9-10 distance 3 >1 => 1000; 11: safe? 7-8-11 distance 2 >1? Actually 7-8 (1), 8-11 (2) => distance 2 >1 => 1000; 12: dangerous (distance 1) => 6000; 13: safe? 7-12-13 distance 2 >1 => 1000, but N=13 cost 0; So cost[13]=0.
Now Dijkstra: Starting at 1. Adj of 1: edges: 1-2, 1-3? Actually edges: 1 2, 1 3? From list: 1 2, 1 3? Yes edge "1 3" appears? In input: after "1 3" there is a line "1 3"? Actually check: In the input example provided (the second block after "Input example 1" there is a list of edges that seems to be misformatted in the problem statement? The problem statement's example input is shown as:
But it's obviously messed up with text like "twenty four" etc. Actually the original Japanese maybe used numbers but in translation they got corrupted. The actual numbers we need to parse: In the English problem statement on AtCoder perhaps it's correct. But in this text, it's garbled. However, the sample description says there is an edge 1-3 as well. Indeed in the diagram they likely have 1-3. So we trust the usual JOI problem. Anyway, we assume our algorithm works.
Now one more nuance: In the cost assignment, we set cost[1]=0. But what about the situation where we might go from 1 to some node and then back to 1? That would incur cost[1] if we set cost[1]=0? Actually if we move from some node to 1, we would add cost[1] = 0, so moving to 1 would be free. That could lead to zero-cost cycles? But moving to 1 and then out again would involve paying cost of the other nodes. Since all costs are positive except start and end, it's not beneficial to cycle through 1 because you'd still pay for other nodes. However, if we set cost[1]=0, one might think of a path that goes 1->2->1->... that would pay for 2, then 0 to go back to 1, then maybe another route, but that repeats paying for 2 again, increasing cost. So no negative cycles, Dijkstra still works.
But also consider if N has cost 0, then moving from N to another node and back to N could add cost of that other node, but again not beneficial.
Now think about the scenario where S is large enough that some nodes become dangerous, and cost Q > P. So we want to avoid dangerous nodes if possible, but sometimes must go through them. Dijkstra handles that.
Thus solution is clear.
Now we must code with care: Input reading, large numbers, using 64-bit for distances.
Potential improvements: BFS multi-source with large N, use vector dist(N+1, -1) for not visited, and queue. For multi-source BFS, set dist[zombie]=0 and push all.
Now Dijkstra: Use vector distD(N+1, LLONG_MAX). Use priority_queue with pair<long long, int>.
Now regarding skipping zombies: In Dijkstra, when we pop a node u, we only consider neighbors v that are not zombies. Also, we must ensure that u itself is not a zombie? But u is from the priority queue, and we only push nodes that are allowed? Actually we start with node 1, which is not zombie. When we relax edges, we may push nodes that are not zombie (since we only consider non-zombie neighbors). So the queue will only contain non-zombie nodes. So that's fine.
Now time complexity: O((N+M) log N).
Now let's test with custom small cases.
Case 1: N=2, M=1, edge 1-2, K=0, S=0, P=1, Q=2. There are no zombies. All towns are safe (except maybe not). cost[1]=0, cost[2]=0 (since N=2). Dijkstra: from 1, neighbor 2 with weight cost[2]=0, so d[2]=0. Answer 0. That's correct because no stay needed.
Case 2: N=3, M=2: edges 1-2, 2-3. K=0, S=0, P=5, Q=10. cost[2]=5 (since non-dangerous). Dijkstra: 1->2 weight 5, d[2]=5; 2->3 weight 0 (since N=3), so d[3] = min(inf, 5+0)=5. Answer 5. That matches: stay at town 2 cost 5.
Case 3: N=3, edges 1-2, 2-3. K=1 zombie at 2. Then allowed nodes: 1 and 3 only. But there is no direct edge between 1 and 3. Guarantee says there is a path using non-zombie towns, but there is none. So such input wouldn't be given. So OK.
Case 4: N=4, edges: 1-2, 2-3, 3-4. K=1 zombie at 3. S=1. Then dangerous towns: distance <=1 from 3: neighbors 2 and 4? Actually 3's neighbors: 2 and 4. So 2 and 4 are dangerous? 4 is N, but we don't pay. So allowed nodes: 1,2,4 (since 3 is zombie). cost[2]=Q (if dangerous) and cost[4]=0. Path: 1->2 weight Q, 2->4 weight 0? But there is no direct edge 2-4; we need 2->? Actually edge 2-3 is blocked because 3 is zombie. So we cannot go from 2 to 4 because the only connection is through 3 which is zombie. So no path. So guarantee would not have such case.
Better: with extra edges.
Thus algorithm is good.
Now we need to produce final answer.
But wait, there is a nuance: The dangerous towns definition says: "A town that can be reached from a town controlled by zombies using roads of S or less is called a dangerous town." Does this include towns that are exactly S steps away? Yes, "S or less" includes S. So condition dist <= S.
Now about the BFS: If a town is a zombie, it's distance 0, but we will not consider it as dangerous for cost because we exclude it. So it's fine.
Now think about possible pitfalls: The graph may have up to 200k edges; BFS and Dijkstra with adjacency list are fine.
Now we need to be careful with large values: dist array for BFS can be int, as max distance is N (100k). So int is fine.
Now we must handle input format: In the problem statement the numbers may be separated by blanks and lines. We'll read using standard cin.
Now final answer: Output d[N] as long long.
Now, test with sample input (if we had the exact numbers). We'll trust.
One more nuance: The problem states: "JOI can move freely on the road in both directions, but he cannot go from one town to another through anything other than the road." That's typical.
Now we need to double-check the transformation: Could there be any situation where we need to pay cost for the starting node? The problem says: "There is no need to stay in town 1 or town N." So we don't pay for staying at 1 or N. Also, we don't pay for N upon arrival because we are done. So our transformation is correct.
But wait: When we move from some node to N, we arrive at N but we do not stay overnight? Actually the description: "every time JOI moves from one town to another, he has to stay overnight in the destination town. ... There is no need to stay in town 1 or town N." This implies that when he moves to N, he does not need to stay overnight, because he has reached the shelter. So indeed, moving to N incurs no cost. Similarly, starting at 1, he hasn't moved yet, so no cost.
But what about moving from 1 to some town? That requires staying overnight at that town, so we pay its cost. So correct.
Now, could there be a case where we need to consider the cost of the node we are leaving? No.
Thus solution stands.
Now we might consider an alternative approach: assign cost to nodes and run Dijkstra with node weights, by splitting nodes into in/out or using edges weight as (cost[u] + cost[v])/2? But the directed approach is simpler.
Now we should consider that the Dijkstra graph is directed, but we treat it as such. However, when we iterate neighbors from undirected adjacency and skip zombies, we effectively consider both directions. But we need to be careful: In the directed approach, from u to v, the cost is cost[v] (if v != N, else 0). So that is exactly what we do: when we are at u, we consider moving to v and add cost[v] (which is defined as above). So that's correct. So we don't need to store a separate directed adjacency; we just use the undirected adjacency and compute cost[v] on the fly.
Now implement:
Use vector<vector> adj(N+1).
After BFS, compute cost array: vector cost(N+1, 0); // for all nodes
For i in 1..N:
if i == 1 or i == N: cost[i] = 0;
else if isZombie[i]: continue (but we can set cost[i] = INF? but not used)
else if dist[i] <= S: cost[i] = Q;
else cost[i] = P;
But we need to compute isZombie: vector isZombie(N+1, false); fill from Ci list.
Now BFS dist: vector dist(N+1, -1). Initialize queue with all zombies; set dist[z]=0.
Loop BFS.
Now Dijkstra:
vector d(N+1, LLONG_MAX);
d[1] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
pq.emplace(0, 1);
while (!pq.empty()) {
auto [cur, u] = pq.top(); pq.pop();
if (cur != d[u]) continue;
if (u == N) break; // optional early break
for (int v : adj[u]) {
if (isZombie[v]) continue; // skip zombie
long long nd = d[u] + cost[v];
if (nd < d[v]) {
d[v] = nd;
pq.emplace(nd, v);
}
}
}
cout << d[N] << endl;
Check: For edge from u to v, we add cost[v]. But note that if v == N, cost[N] = 0, so nd = d[u]. That's correct.
Now, what about the starting node: When we start at 1, we haven't paid any cost. So d[1]=0.
Now we also need to consider that the path may go through N before the end? Not possible because we stop at N. So fine.
Now let's test manually with small scenario: N=3, edges 1-2, 2-3, no zombies, S=0, P=5, Q=10. Then cost: 1=0, 2=5, 3=0. Dijkstra: start 1. adj[1]={2}. d[2]=0+5=5. pq: (5,2). Then pop 2. adj[2]={1,3}. v=1: isZombie? no but d[1] currently 0, nd=5+ cost[1]=5+0=5 > 0, no update. v=3: cost[3]=0, nd=5+0=5, d[3]=5. So answer 5. Good.
Now with zombie: N=4, edges: 1-2, 2-3, 3-4. Zombie=3, S=0. Then allowed: 1,2,4 (since 3 zombie). isZombie[3]=true. BFS: distances: from zombie 3: dist[3]=0, dist[2]=1, dist[4]=1? Actually edge 3-4, so dist[4]=1, dist[1]=2 (via 2). Since S=0, dangerous towns are those with dist<=0 => only zombie itself. So safe towns: all non-zombie are safe. cost: 1=0, 2=P, 4=0. Now graph: from 1, adj[1]={2} -> v=2 not zombie, cost[2]=P, d[2]=P. Then from 2, adj[2]={1,3}. v=1: cost[1]=0, nd=P+0=P, no update; v=3: isZombie true, skip. So cannot reach 4. No path. So guarantee prevents this.
Now with S=1: then dangerous towns: dist<=1: so 2 and 4 become dangerous. cost[2]=Q, cost[4]=0. Still no path to 4 because edge 2-3 is blocked; 3 zombie; 4 only neighbor is 3, which is zombie. So no path. So guarantee ensures connectivity through allowed nodes.
Thus algorithm is correct.
Now we need to be careful with BFS distances for nodes that are not reachable from any zombie. In that case dist[i] remains -1. We treat -1 > S, so safe.
Implementation details:
For BFS, we can use a vector dist(N+1, -1) and queue. For each zombie, set dist[z]=0 and push. Then while queue not empty, pop, for each neighbor, if dist[neighbor] == -1, set dist[neighbor] = dist[u]+1 and push.
Since graph may be disconnected, unreachable nodes have -1.
Now, is it necessary to consider that zombies are not allowed, but they can affect danger through BFS? Yes.
Now one more nuance: The definition of dangerous towns might also consider that if a town itself is a zombie, it's dangerous but not visitable. But irrelevant.
Now, we need to confirm that the cost for nodes that are dangerous but also are 1 or N should be 0, not Q or P. The problem says no need to stay at 1 or N. So cost 0.
Now what about towns that are both dangerous and zombie? Not possible because zombies are distinct from 1,N and are given; they are not considered for cost anyway.
Thus final solution.
But wait: There's a nuance: The BFS distance should be measured by number of roads (edges). That is what we did.
Now I recall a known JOI problem: JOI 2015/2016 qualifying round problem "Zombie Island" maybe? Actually there is a problem called "Zombie Island" (Mojin) that involves computing dangerous towns and then shortest path with node costs. I've seen similar problems. Usually the solution is as described.
Now, potential optimization: Because the cost for dangerous towns is higher, but we could also treat the graph with edge weights = P or Q depending on nodes? But fine.
Now, let's double-check the sample output for example 2: 15000. Let's compute quickly if our algorithm might produce that. Not necessary.
Now, we should also consider if there is any possibility that moving into a town that is dangerous might incur cost Q even if you are just passing through and staying overnight? That's what we do.
Now, confirm that we don't need to pay for the starting town if we start there. Yes.
Now, about memory: adjacency list of size N+1, each edge stored twice (200k *2 = 400k entries). That's fine.
Now implement.
Now we should consider reading K lines: each contains Ci. Ensure we read all K lines.
Now, potential pitfalls: The input might have extra spaces, but cin handles.
Now, we produce answer as long long.
Now, one more nuance: The problem states "Note that the output does not always fall within the range of 32-bit signed integers." So we must use 64-bit.
Now final code:
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M, K, S;
cin >> N >> M >> K >> S;
int P, Q;
cin >> P >> Q;
vector<bool> isZombie(N+1, false);
for (int i = 0; i < K; ++i) {
int c;
cin >> c;
isZombie[c] = true;
}
vector<vector<int>> adj(N+1);
for (int i = 0; i < M; ++i) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
// BFS from all zombies to compute min distance
vector<int> dist(N+1, -1);
queue<int> q;
for (int i = 1; i <= N; ++i) {
if (isZombie[i]) {
dist[i] = 0;
q.push(i);
}
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v : adj[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
// compute cost for staying at each town (if we ever stay there)
vector<ll> cost(N+1, 0); // default 0
for (int i = 1; i <= N; ++i) {
if (i == 1 || i == N) {
cost[i] = 0; // no stay cost
continue;
}
if (isZombie[i]) {
// not used, but set maybe large
continue;
}
// if dist[i] == -1 (unreachable from any zombie), treat as safe
if (dist[i] != -1 && dist[i] <= S) {
cost[i] = Q; // dangerous
} else {
cost[i] = P; // safe
}
}
// Dijkstra
const ll INFLL = 1e18;
vector<ll> d(N+1, INFLL);
d[1] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
pq.emplace(0, 1);
while (!pq.empty()) {
auto [curDist, u] = pq.top(); pq.pop();
if (curDist != d[u]) continue;
if (u == N) break; // optional
for (int v : adj[u]) {
if (isZombie[v]) continue; // cannot go to zombie
ll nd = d[u] + cost[v];
if (nd < d[v]) {
d[v] = nd;
pq.emplace(nd, v);
}
}
}
cout << d[N] << endl;
return 0;
}
Now let's test with sample 1 manually using this code? We can simulate but trust.
Now we must verify that for nodes unreachable from zombies (dist = -1), we set cost = P (safe). That's correct because they are not within S distance.
Now, edge case: Some node may be reachable from zombies but distance exactly S+1? Then safe, cost P.
Now, check with sample 1: distance from 7 to 2 is 2, S=1 => safe. Good.
Now one more nuance: The BFS includes the zombie nodes as sources, so distances from zombies to other nodes are computed correctly even if there are multiple zombies.
Now potential pitfalls: The graph may have up to 200k edges, adjacency list vector of vector works.
Now about using dist[v] == -1 as unvisited: That works because BFS will only visit nodes reachable from any zombie. For unreachable nodes, -1 remains.
Now Dijkstra: we used cost[v] which for v=N is 0; for v=1 is 0. So fine.
Now early break when u==N: That's okay because when we pop N, we have the shortest distance to N (since Dijkstra's property: when a node is popped from priority queue, its distance is finalized). So we can break and output d[N]. But careful: If we break, we don't process neighbors of N. That's fine because we don't need further updates. So we can add: if (u == N) break; after the pop, before processing neighbors. Actually after confirming curDist == d[u], we can check if u==N and break. That is efficient.
But if we break before processing neighbors, we might miss updating some nodes? Not needed because we only need d[N]. So we can break. However, we placed break after processing? In the code above we put if (u == N) break; after checking curDist and before processing neighbors? Actually we wrote:
code
if (curDist != d[u]) continue;
if (u == N) break; // optional
for (int v : adj[u]) {
But if we break before processing neighbors, that's fine because we already have the answer. However, we must ensure that d[N] is finalized when we pop N. Indeed in Dijkstra, when a node is popped from the priority queue, its distance is the shortest distance. So break is safe.
But is it possible that we push N with some distance, then later find a shorter distance and push again, and then pop the older larger distance first? Dijkstra's algorithm ensures that when we first pop N, it's the smallest distance because the priority queue yields smallest distance. If we later find a shorter distance to N, we would have updated d[N] and pushed a new pair, and that new pair would have smaller distance, so it would be popped before the older larger one. So the first time we pop N, it's indeed the shortest. So break safe.
Alternatively, we could not break; but it's fine.
Now, I think that's it.
But there is a subtle point: In Dijkstra, when we skip zombies, we ignore them. That's correct.
Now consider the case where the path includes a dangerous town that is also within distance S of a zombie, but we might pass through it without staying? No, you always stay overnight when you arrive. So cost incurred.
Now, is there any scenario where staying in a town might be avoided by doing multiple moves in one day? No, the problem says every time you move you must stay overnight.
Now, we should also consider that if we have to move from one town to another, the overnight stay is at the destination. So our model is correct.
Now, one more nuance: The cost for staying in a town is incurred even if you stay multiple times (if you revisit). But optimal path won't revisit.
Thus final answer.
Now, we need to confirm that we are not missing the cost of the first move: from 1 to neighbor, we pay for neighbor. That's covered.
Now, the solution appears sound.
Let's double-check with a tricky scenario: Suppose there is a direct edge from 1 to N. Then if N is safe, cost of staying at N is 0, so moving directly from 1 to N adds cost 0, so answer 0. That's correct because no stay needed. If there is an edge 1-N and N is dangerous, still cost 0. So answer 0. So that's consistent.
Now, what about node 1 being dangerous? Doesn't matter.
Now, potential oversight: The problem states: "If you stay in a non-dangerous town, you will stay in a cheap inn with a low accommodation cost of P yen, but if you stay in a dangerous town, you will stay in a luxury inn with excellent security services and an accommodation cost of Q yen." This suggests that the cost is determined by the danger status of the town where you stay. So yes.
Now, we must ensure that the "dangerous" classification includes towns that are exactly S steps away, as said.
Now, the guarantee: "Given the input data, it is guaranteed to be able to travel from town 1 to town N only through non-zombie-controlled towns." So we will only consider non-zombie nodes. Good.
Now, one more nuance: Could a town be both a zombie and dangerous? Not relevant.
Now, about the BFS: we set dist for zombie nodes to 0 and push them. Then we propagate. But what about edges between zombie nodes? They will be visited, but that's fine.
Now, memory: Using vector of vector for adj up to 400k entries, fine.
Now, test with the sample input as given in the problem (the corrupted text). But we'll trust.
Now, final answer code.
But before finalizing, let's consider if there is any alternative simpler approach: Use 0-1 BFS? Not needed.
Now, potential pitfalls: Using -1 for unvisited BFS, but when we compute cost for i where dist[i] == -1, we treat as safe. However, what if a town is not reachable from any zombie but is still considered dangerous? No, because it's not reachable. So safe.
Now, we must ensure that the BFS does not overflow or take too long.
Now, we'll write final solution in C++.
One more small detail: In the BFS, if K=0, then there are no sources. In that case, dist remains -1 for all nodes. Then all non-zombie nodes are safe, cost = P. That's correct. But we need to handle K=0: the queue would be empty, BFS does nothing. That's fine.
Now, we need to consider that zombies may be listed, but K could be 0, and then there are no lines for zombies? Actually K lines: if K=0, there are zero lines, so we should not read any. That's fine.
Now, think about the case where some towns are not reachable from 1 to N through non-zombie towns? Not guaranteed.
Now, we have to be careful about the types: P and Q up to 100000, but we store cost as long long.
Now, all good.
Now, final solution.
Wait: Could there be a scenario where the path includes a town that is dangerous but the cost Q is lower than P? The problem states 1 ≤ P < Q ≤ 100000, so Q > P always. So dangerous towns are more expensive.
Thus we want to avoid dangerous towns if possible.
Now, one more nuance: The BFS for danger uses the whole graph, including roads that go through zombie towns. But does danger propagation consider that you can travel through zombie towns? The definition: "A town that can be reached from a town controlled by zombies using roads of S or less is called a dangerous town." It does not say that you cannot pass through zombie towns; it says "can be reached". Usually, if you can reach it via any path, that includes passing through other zombie towns. So BFS on full graph is correct.
But could there be a subtlety: Suppose a town is only reachable from a zombie town by a path that goes through another town that is also a zombie? That still counts as reachable. So BFS includes all nodes.
Now, in some problems, they might define dangerous towns as those whose minimum distance to any zombie town (measured only through non-zombie towns?) But the statement says "using roads" and doesn't restrict passing through zombie towns. So full graph is correct.
But is there any official clarification? I recall a problem "Zombie Island" from JOI where the dangerous towns are those within distance S from any zombie town, measured in the whole graph. So yes.
Now, confirm with sample: The dangerous towns include 12, which is neighbor of 7; also 8 neighbor of 7; etc. All good.
Now, we can finalize.
Now, let's think about possible alternative pitfalls: The BFS distances are computed using all edges, but note that we cannot use zombie nodes for traversal, but for danger classification they are still considered for distance. So correct.
Now, about time: BFS O(N+M). Dijkstra O((N+M) log N). With N=100k, M=200k, log N ~ 17, so about 5 million operations, fine.
Now, code.
But wait: There's a subtlety in Dijkstra: we add cost[v] for each move. However, if we move from u to v, we pay cost[v]. That's correct. But what about the possibility that we might end at N and not pay. So the total cost is sum of cost of all nodes visited except start and end. That matches.
Now, consider a path: 1 -> a -> b -> N. Our Dijkstra will compute: d[1]=0, from 1 to a: d[a] = cost[a]; from a to b: d[b] = d[a] + cost[b]; from b to N: d[N] = d[b] + cost[N] = d[b] + 0 = cost[a] + cost[b]. So total = cost[a]+cost[b]. Good.
Now, we need to make sure that cost[N] is 0, so moving to N adds 0. Good.
Now, final code.
But before final, think about potential integer overflow: cost[v] up to 100k, d[u] up to maybe 1e10, sum fits in 64-bit.
Now, final answer.
Now, I'll write the solution explanation as required? The user asked to "Solve it", meaning produce code. In this environment, we are to output final answer as code. So we will provide final C++ code.
Now, we might want to include some comments but not necessary.
Now, double-check: In Dijkstra, we might push nodes that are zombies? Not because we skip them when relaxing, but we never push them because we only push v when we relax and v is not zombie. So no issue.
Now, we also need to be careful that we do not inadvertently use cost of zombie nodes; we skip them.
Now, test with K>0 but some nodes might be unreachable from zombies; dist = -1. Then we treat as safe. That's fine.
Now, early break on N: we need to check after popping that u == N, then break. However, we must ensure that when we break, d[N] is already finalized. Since we pop N from queue only when its distance is final, yes. But what if we never push N? That would be if there is no path; but guarantee says there is, so fine.
Now, final code.
I'll write it. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int N, M, K, S;
cin >> N >> M >> K >> S;
int P, Q;
cin >> P >> Q;
// mark zombie towns
vector<bool> isZombie(N + 1, false);
for (int i = 0; i < K; ++i) {
int c;
cin >> c;
isZombie[c] = true;
}
// adjacency list of the full graph
vector<vector<int>> adj(N + 1);
for (int i = 0; i < M; ++i) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
// ---------- 1. BFS from all zombies ----------
vector<int> dist(N + 1, -1); // minimum distance to a zombie
queue<int> q;
for (int i = 1; i <= N; ++i) {
if (isZombie[i]) {
dist[i] = 0;
q.push(i);
}
}
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v : adj[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
// ---------- 2. Compute stay costs ----------
vector<ll> cost(N + 1, 0); // cost of staying overnight in the town
for (int i = 1; i <= N; ++i) {
if (i == 1 || i == N) continue; // no cost for start and shelter
if (isZombie[i]) continue; // never used
// unreachable from zombies -> safe
if (dist[i] == -1 || dist[i] > S)
cost[i] = P;
else
cost[i] = Q;
}
// ---------- 3. Dijkstra on allowed towns ----------
const ll INF = 1e18;
vector<ll> d(N + 1, INF);
d[1] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
pq.emplace(0, 1);
while (!pq.empty()) {
auto [cur, u] = pq.top(); pq.pop();
if (cur != d[u]) continue;
if (u == N) break; // shortest distance to N found
for (int v : adj[u]) {
if (isZombie[v]) continue; // cannot enter a zombie town
ll nd = d[u] + cost[v];
if (nd < d[v]) {
d[v] = nd;
pq.emplace(nd, v);
}
}
}
cout << d[N] << '\n';
return 0;