Problem list
Log in
#icpc00395 · ICPC GCPC 2025

Island Urbanism

Diamond III 7.0 s 2048 MB

On a small volcanic island far away from any major landmass, the Grand Council for Painless Cycling (GCPC) is facing regular complaints about the bad quality of the bike path network. Their budget is limited, but they would like to improve the situation for all the cyclists on their island. A survey helped to determine the most common destinations of the cyclists. The GCPC expects that the cyclists are satisfied with the bike path replacements if there is a way to cycle between any two destinations using only newly replaced bike paths. In order not to unduly favour any of the villages, the Council has decided that a maximum of $7$ destinations per village should be taken into account.

There is one main cyclic bike path going around the volcano. On its way, it traverses every village on the island exactly once. Each village may have additional bike paths. How much does the GCPC have to spend at least in order to replace bike paths to connect all destinations via new paths?

Input

The input consists of:

  • One line with four integers $n$, $m$, $v$ and $k$ ($3\leq n\leq 5000$, $n \leq m \leq 20,000$, $3 \leq v \leq n$, $1 \leq k \leq n$), the number of junctions, the number of bike paths, the number of villages, and the number of destinations.
  • One line with $v$ integers $u_i$ ($1 \leq u_i$, $\sum_{i=1}^v u_i = n$), the $i$th of which describes the number of junctions in village $i$.
  • $m$ lines each containing three integers $a$, $b$ and $c$ ($1 \leq a,b \leq n$, $1 \leq c \leq 10^9$), describing a bidirectional bike path between junction $a$ and junction $b$ with a replacement cost of $c$.
  • One line with $k$ distinct integers $t$ ($1 \leq t \leq n$), the junctions that have to be connected by the replaced bike paths.

Additionally, the following is guaranteed:

  • Village $1$ consists of junctions $1,\dots, u_1$, village $2$ consists of junctions $u_1 + 1, \dots, u_1 + u_2$, and so on. The last village consists of junctions $1+ \sum_{i = 1}^{v-1} u_i , \dots, n$. Within each village, it is possible to travel between any pair of junctions without leaving the village.
  • There is a bike path between junctions $\sum_{i=1}^{j} u_i$ and $1 + \sum_{i=1}^{j} u_i$ for each $1 \leq j \leq v-1$, as well as a bike path between junctions $n$ and $1$. There are no other bike paths between different villages.
  • There is at most one bike path between each pair of junctions and no bike path connects a junction to itself.
  • In each village there are at most $7$ junctions that need to be part of the improved cycling network.

Output

Output the minimum cost which allows replacing bike paths such that every trip between any two destinations can be done using only newly replaced paths.

Example 1

Input
3 3 3 3
1 1 1
2 1 5
2 3 4
1 3 3
2 1 3
Output
7

Example 2

Input
8 10 3 6
4 1 3
1 2 3
2 4 2
1 3 5
3 4 2
4 5 3
5 6 2
8 6 3
6 7 2
7 8 2
8 1 1
2 3 1 7 6 8
Output
12
Source
ICPC GCPC 2025
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code