問題一覧 > 通常問題

No.3561 Collect KCPC

レベル : / AI推定難易度 : 2100 / 実行時間制限 : 1ケース 6.000秒 / メモリ制限 : 1024 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 29
作問者 : HoyHoyCharhang / テスター : 👑 みうね fluorine TKTYI jastaway soryuusi0219 yaaya butsurizuki wasab1 tatesoto KumaTachiRen
お気に入りにしたユーザー ProblemId : 13379 / 自分の提出
問題文最終更新日: 2026-08-15 03:08:19
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
KCPC新歓杯2026 (順位表) の他の問題:

Problem Statement

There are $N$ locations numbered from $1$ to $N$ and $M$ roads. Using the $i\;(1\leq i\leq M)$-th road, you can travel from location $U_i$ to location $V_i$ in $C_i$ minutes. You cannot travel along this road in the reverse direction.

You are also given a string $S = S_1S_2\cdots S_N$ of length $N$ consisting of uppercase English letters. At location $i\;(1\leq i\leq N)$, there is $1$ cookie with $S_i$ written on it.

Now, you have an empty string $T$. Starting from location $1$, you may repeat the following action $0$ or more times, choosing one of the options each time.

  • If there is a cookie at your current location, eat it. Append the character written on the cookie to the end of $T$.
  • Use a road going out of your current location to move to another location.

Determine whether you can make $T=$ KCPC, and if so, output the minimum total time spent using roads.

Input

The input is given from standard input in the following format.

$N$ $M$
$U_1$ $V_1$ $C_1$
$U_2$ $V_2$ $C_2$
$\vdots$
$U_M$ $V_M$ $C_M$
$S$
  • $4\leq N\leq 10^5$
  • $1\leq M\leq 1.5\times 10^5$
  • $1\leq U_i, V_i\leq N\;\;(1\leq i\leq M)$
  • $U_i\neq V_i\;\;(1\leq i\leq M)$
  • $(U_i,V_i)$ are pairwise distinct
  • $1\leq C_i\leq 10^9\;\;(1\leq i\leq M)$
  • $N,M,U_i,V_i,C_i$ are integers
  • $S$ is a string of length $N$ consisting of uppercase English letters

Partial Score

This problem has partial scoring based on subtasks.

SubtaskScoreConstraints
部分点110% (30 pts)$N\leq 50$
部分点220% (60 pts)$S_i =$ C なる $i\;\;(1\leq i\leq N)$ の個数は $5$ 個以下
部分点320% (60 pts)$U_i<V_i\;\;(1\leq i\leq M)$
部分点450% (150 pts)追加の制約はない

Output

If you can make $T=$KCPC, output the minimum total time spent using roads on $1$ line.
Otherwise, output -1.
Print a newline at the end.

Sample

Sample 1
Input
4 5
1 2 2
2 3 2
3 1 3
2 4 1
4 2 1
PKCC
Output
10

You can achieve the goal in a total of 10 minutes by acting as follows.

  • Start from location $1$.
  • Move from location $1$ to location $2$ ($2$ minutes).
  • Eat the cookie at location $2$. Now $T=$K.
  • Move from location $2$ to location $3$ ($2$ minutes).
  • Eat the cookie at location $3$. Now $T=$KC.
  • Move from location $3$ to location $1$ ($3$ minutes).
  • Eat the cookie at location $1$. Now $T=$KCP.
  • Move from location $1$ to location $2$ ($2$ minutes).
  • Move from location $2$ to location $4$ ($1$ minute).
  • Eat the cookie at location $4$. Now $T=$KCPC.

This sample satisfies the constraints of Subtasks $1,2,4$.

Sample 2
Input
6 7
1 2 3
2 3 4
3 1 3
3 4 2
4 5 1
5 6 4
6 4 3
CPAKBC
Output
-1

No matter how you act, you cannot make $T=$KCPC.

This sample satisfies the constraints of Subtasks $1,2,4$.

Sample 3
Input
7 8
1 2 4
1 3 3
2 4 2
3 5 5
3 6 2
4 5 1
5 7 4
6 7 2
KKCCPCC
Output
11

This sample satisfies the constraints of Subtasks $1,2,3,4$.

Sample 4
Input
15 20
6 15 59680731
4 14 512184729
4 7 928045980
15 12 936533142
5 2 641980757
9 3 154750438
11 6 57032185
15 2 194588886
13 14 631990846
12 5 6215057
2 12 791775040
2 9 476629800
14 5 126298100
10 11 267993404
10 1 73997749
12 8 450877870
1 13 296054234
12 10 182601287
3 4 604956581
1 14 933531002
CCCJCCCCZCCCPCK
Output
5176583842

This sample satisfies the constraints of Subtasks $1,4$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。