#arc177f. F - Two Airlines
F - Two Airlines
Score: points
问题陈述
AtCoder共和国由个东西向排列的岛屿组成,从西向东编号为到。这些岛屿通过空中航线相连,对于每个,岛屿和之间有一条双向空中航线,如图中所示。每条航线由A公司或J公司拥有,岛屿和之间的航线由公司拥有。
这个国家有名居民,编号为到。居民目前位于岛屿。
每位居民都有一张公司的优惠券。具体来说,居民有一张公司的优惠券。凭借优惠券,居民可以无限次免费乘坐该公司的航线,但乘坐另一家公司的航线每次需要支付1枚硬币。
现在,岛屿上有一个宝箱。居民们希望合作将其运送到首都,首都位于岛屿。确定实现此目标所需的最少总硬币数。
居民们可以相互传递宝箱,但不能传递他们的优惠券。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
The Republic of AtCoder consists of islands aligned east-west, numbered to from west to east. The islands are connected by air routes, where for each , there is a bidirectional air route between islands and , as shown in the figure below. Each air route is owned by company A or company J, and the route between islands and is owned by company .
The nation has residents, numbered to . Resident is currently on island .
Each resident has a coupon of one of the companies. Specifically, resident has a coupon of company . With a coupon, a resident can take that company's routes for free any number of times, but taking a route of the other company costs coin per trip.
Now, there is a treasure chest on island . The residents want to cooperate to carry it to the capital, which is on island . Determine the minimum total number of coins required to achieve this goal.
The residents can pass the treasure chest to each other, but not their coupons.
Constraints
- is
A
orJ
. - is
A
orJ
. - , , and are integers.
Input
The input is given from Standard Input in the following format:
Note that the second line is given as a string of length .
Output
Print the answer.
Sample Input 1
4 3
AAJJ
3 A
1 J
1 J
Sample Output 1
2
The following procedure carries the treasure chest to island for a total of coins.
- Resident moves from island to island . The route is not covered by the coupon, so coin is paid.
- Resident moves from island to island . The route is covered by the coupon, so no coin is needed.
- Resident moves from island to island . The route is covered by the coupon, so no coin is needed.
- Resident picks up the treasure chest.
- Resident , carrying the treasure chest, moves from island to island . The route is covered by the coupon, so no coin is needed.
- Resident passes the treasure chest to resident .
- Resident , carrying the treasure chest, moves from island to island . The route is not covered by the coupon, so coin is paid.
- Resident , carrying the treasure chest, moves from island to island . The route is covered by the coupon, so no coin is needed.
- Resident , carrying the treasure chest, moves from island to island . The route is covered by the coupon, so no coin is needed.
Sample Input 2
8 3
JJAAJJAJ
2 A
6 A
8 J
Sample Output 2
6
Sample Input 3
8 6
JJAAJJAJ
2 A
6 A
8 J
8 J
8 J
8 J
Sample Output 3
4