実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 個の座席が並んでおり、座席には 1, 2, \ldots, N の番号が付けられています。
座席の状態は #
, .
からなる長さ N の文字列 S によって与えられます。S の i 文字目が #
のとき座席 i には人が座っていることを表し、S の i 文字目が .
のとき座席 i には人が座っていないことを表します。
1 以上 N - 2 以下の整数 i であって、以下の条件を満たすものの個数を求めてください。
- 座席 i, i + 2 には人が座っており、座席 i + 1 には人が座っていない
制約
- N は 1 以上 2 \times 10^5 以下の整数
- S は
#
,.
からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
6 #.##.#
出力例 1
2
i = 1, 4 が条件を満たすので、答えは 2 です。
入力例 2
1 #
出力例 2
0
入力例 3
9 ##.#.#.##
出力例 3
3
Score : 100 points
Problem Statement
There are N seats in a row, numbered 1, 2, \ldots, N.
The state of the seats is given by a string S of length N consisting of #
and .
. If the i-th character of S is #
, it means seat i is occupied; if it is .
, seat i is unoccupied.
Find the number of integers i between 1 and N - 2, inclusive, that satisfy the following condition:
- Seats i and i + 2 are occupied, and seat i + 1 is unoccupied.
Constraints
- N is an integer satisfying 1 \leq N \leq 2 \times 10^5.
- S is a string of length N consisting of
#
and.
.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
6 #.##.#
Sample Output 1
2
i = 1 and 4 satisfy the condition, so the answer is 2.
Sample Input 2
1 #
Sample Output 2
0
Sample Input 3
9 ##.#.#.##
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
2 次元座標平面の原点に高橋くんがいます。
高橋くんが座標平面上の点 (a,b) から点 (c,d) に移動するには \sqrt{(a-c)^2+(b-d)^2} のコストがかかります。
高橋くんが原点からスタートし N 個の点 (X_1,Y_1),\ldots,(X_N,Y_N) へこの順に移動したのち原点に戻るときの、コストの総和を求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq X_i,Y_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 \vdots X_N Y_N
出力
答えを出力せよ。
真の値との相対誤差または絶対誤差が 10^{-6} 以下であれば正解とみなされる。
入力例 1
2 1 2 -1 0
出力例 1
6.06449510224597979401
移動は次の 3 行程からなります。
- (0,0) から (1,2) に移動する。\sqrt{(0-1)^2+(0-2)^2}=\sqrt{5}=2.236067977... のコストがかかる
- (1,2) から (-1,0) に移動する。\sqrt{(1-(-1))^2+(2-0)^2}=\sqrt{8}=2.828427124... のコストがかかる
- (-1,0) から (0,0) に移動する。\sqrt{(-1-0)^2+(0-0)^2}=\sqrt{1}=1 のコストがかかる
コストの総和は 6.064495102... となります。
入力例 2
7 -14142 13562 -17320 50807 -22360 67977 24494 89742 -26457 51311 28284 27124 31622 77660
出力例 2
384694.57587932075868509383
入力例 3
5 -100000 100000 100000 -100000 -100000 100000 100000 -100000 -100000 100000
出力例 3
1414213.56237309504880168872
Score : 150 points
Problem Statement
Takahashi is at the origin on a two-dimensional coordinate plane.
The cost for him to move from point (a, b) to point (c, d) is \sqrt{(a - c)^2 + (b - d)^2}.
Find the total cost when he starts at the origin, visits N points (X_1, Y_1), \ldots, (X_N, Y_N) in this order, and then returns to the origin.
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq X_i, Y_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 Y_1 \vdots X_N Y_N
Output
Print the answer.
Your output will be considered correct if its absolute or relative error from the true value is at most 10^{-6}.
Sample Input 1
2 1 2 -1 0
Sample Output 1
6.06449510224597979401
The journey consists of the following three steps:
- Move from (0, 0) to (1, 2). The cost is \sqrt{(0 - 1)^2 + (0 - 2)^2} = \sqrt{5} = 2.236067977....
- Move from (1, 2) to (-1, 0). The cost is \sqrt{(1 - (-1))^2 + (2 - 0)^2} = \sqrt{8} = 2.828427124....
- Move from (-1, 0) to (0, 0). The cost is \sqrt{(-1 - 0)^2 + (0 - 0)^2} = \sqrt{1} = 1.
The total cost is 6.064495102....
Sample Input 2
7 -14142 13562 -17320 50807 -22360 67977 24494 89742 -26457 51311 28284 27124 31622 77660
Sample Output 2
384694.57587932075868509383
Sample Input 3
5 -100000 100000 100000 -100000 -100000 100000 100000 -100000 -100000 100000
Sample Output 3
1414213.56237309504880168872
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 行 N 列のグリッドが与えられます。ここで、N は偶数です。グリッドの上から i 行目、左から j 列目のマスをマス (i, j) と表記します。
グリッドの各マスは黒か白のいずれかで塗られており、A_{i, j} = #
のときマス (i, j) は黒、A_{i, j} = .
のときマス (i, j) は白で塗られています。
i = 1, 2, \ldots, \frac{N}{2} の順に以下の操作を行った後のグリッドの各マスの色を求めてください。
- i 以上 N + 1 - i 以下の整数 x, y について、マス (y, N + 1 - x) の色をマス (x, y) の色で置き換える。この置き換えは条件を満たすすべての整数 x, y について同時に行う。
制約
- N は 2 以上 3000 以下の偶数
- A_{i, j} は
#
または.
入力
入力は以下の形式で標準入力から与えられる。
N A_{1, 1}A_{1, 2}\ldotsA_{1, N} A_{2, 1}A_{2, 2}\ldotsA_{2, N} \vdots A_{N, 1}A_{N, 2}\ldotsA_{N, N}
出力
すべての操作を終えた後、マス (i, j) の色が黒であるとき B_{i, j} = #
、マス (i, j) の色が白であるとき B_{i, j} = .
として以下の形式で出力せよ。
B_{1, 1}B_{1, 2}\ldotsB_{1, N} B_{2, 1}B_{2, 2}\ldotsB_{2, N} \vdots B_{N, 1}B_{N, 2}\ldotsB_{N, N}
入力例 1
8 .......# .......# .####..# .####..# .##....# .##....# .####### .#######
出力例 1
........ #######. #.....#. #.###.#. #.#...#. #.#####. #....... ########
操作によってグリッドの各マスの色は以下のように変化します。
.......# ........ ........ ........ ........ .......# ######.. #######. #######. #######. .####..# ######.. #....##. #.....#. #.....#. .####..# → ##..##.. → #....##. → #.##..#. → #.###.#. .##....# ##..##.. #..####. #.##..#. #.#...#. .##....# ##...... #..####. #.#####. #.#####. .####### ##...... #....... #....... #....... .####### ######## ######## ######## ########
入力例 2
6 .#.#.# ##.#.. ...### ###... ..#.## #.#.#.
出力例 2
#.#.#. .#.#.# #.#.#. .#.#.# #.#.#. .#.#.#
入力例 3
12 .......#.### #...#...#..# ###.#..##### ..#.#.#.#... .#.....#.### .......#.#.. #...#..#.... #####....... ...#...#.#.# ..###..#..## #..#.#.#.#.# .####.......
出力例 3
.#..##...##. #.#.#.#.#... ###.##..#... #.#.#.#.#... #.#.##...##. ............ ............ .###.###.### ...#...#.#.. .###...#.### ...#...#...# .###...#.###
Score : 400 points
Problem Statement
You are given a grid with N rows and N columns, where N is an even number. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
Each cell is painted black or white. If A_{i, j} = #
, cell (i, j) is black; if A_{i, j} = .
, it is white.
Find the color of each cell after performing the following operation for i = 1, 2, \ldots, \frac{N}{2} in this order.
- For all pairs of integers x, y between i and N + 1 - i, inclusive, replace the color of cell (y, N + 1 - x) with the color of cell (x, y). Perform these replacements simultaneously for all such pairs x, y.
Constraints
- N is an even number between 2 and 3000, inclusive.
- Each A_{i, j} is
#
or.
.
Input
The input is given from Standard Input in the following format:
N A_{1,1}A_{1,2}\ldots A_{1,N} A_{2,1}A_{2,2}\ldots A_{2,N} \vdots A_{N,1}A_{N,2}\ldots A_{N,N}
Output
After all operations, let B_{i, j} = #
if cell (i, j) is black, and B_{i, j} = .
if it is white. Print the grid in the following format:
B_{1,1}B_{1,2}\ldots B_{1,N} B_{2,1}B_{2,2}\ldots B_{2,N} \vdots B_{N,1}B_{N,2}\ldots B_{N,N}
Sample Input 1
8 .......# .......# .####..# .####..# .##....# .##....# .####### .#######
Sample Output 1
........ #######. #.....#. #.###.#. #.#...#. #.#####. #....... ########
The operations change the colors of the grid cells as follows:
.......# ........ ........ ........ ........ .......# ######.. #######. #######. #######. .####..# ######.. #....##. #.....#. #.....#. .####..# → ##..##.. → #....##. → #.##..#. → #.###.#. .##....# ##..##.. #..####. #.##..#. #.#...#. .##....# ##...... #..####. #.#####. #.#####. .####### ##...... #....... #....... #....... .####### ######## ######## ######## ########
Sample Input 2
6 .#.#.# ##.#.. ...### ###... ..#.## #.#.#.
Sample Output 2
#.#.#. .#.#.# #.#.#. .#.#.# #.#.#. .#.#.#
Sample Input 3
12 .......#.### #...#...#..# ###.#..##### ..#.#.#.#... .#.....#.### .......#.#.. #...#..#.... #####....... ...#...#.#.# ..###..#..## #..#.#.#.#.# .####.......
Sample Output 3
.#..##...##. #.#.#.#.#... ###.##..#... #.#.#.#.#... #.#.##...##. ............ ............ .###.###.### ...#...#.#.. .###...#.### ...#...#...# .###...#.###
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
英大文字からなる文字列 S が与えられます。
整数の組 (i, j, k) であって、以下の条件をともに満たすものの個数を求めてください。
- 1 \leq i < j < k \leq |S|
- S_i, S_j, S_k をこの順に結合して得られる長さ 3 の文字列が回文となる
ただし、|S| は文字列 S の長さ、S_x は S の x 番目の文字を指します。
制約
- S は長さ 1 以上 2 \times 10^5 以下の英大文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ABCACC
出力例 1
5
(i, j, k) = (1, 2, 4), (1, 3, 4), (3, 4, 5), (3, 4, 6), (3, 5, 6) が条件を満たします。
入力例 2
OOOOOOOO
出力例 2
56
入力例 3
XYYXYYXYXXX
出力例 3
75
Score : 400 points
Problem Statement
You are given a string S consisting of uppercase English letters.
Find the number of integer triples (i, j, k) satisfying both of the following conditions:
- 1 \leq i < j < k \leq |S|
- The length-3 string formed by concatenating S_i, S_j, and S_k in this order is a palindrome.
Here, |S| denotes the length of S, and S_x denotes the x-th character of S.
Constraints
- S is a string of length between 1 and 2 \times 10^5, inclusive, consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ABCACC
Sample Output 1
5
The triples satisfying the conditions are (i, j, k) = (1, 2, 4), (1, 3, 4), (3, 4, 5), (3, 4, 6), (3, 5, 6).
Sample Input 2
OOOOOOOO
Sample Output 2
56
Sample Input 3
XYYXYYXYXXX
Sample Output 3
75
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
N 人の人がおり、3 つのチームに分かれています。
人には 1, 2, \ldots, N の番号、チームには 1, 2, 3 の番号がついており、現在人 i はチーム A_i に所属しています。
各人には強さという値が定まっており、人 i の強さは B_i です。また、チームの強さをチームに所属する人の強さの和として定めます。
0 人以上の人が所属するチームを変更することですべてのチームの強さが等しくなるようにできるか判定してください。すべてのチームの強さが等しくなるようにできる場合は所属するチームを変更する人数として考えられる最小値を求めてください。
ただし、チーム 1, 2, 3 の他に新たにチームを作ることはできません。
制約
- 3 \leq N \leq 100
- A_i \in \lbrace 1, 2, 3 \rbrace
- 各 x \in \lbrace 1, 2, 3 \rbrace に対し、ある i が存在して A_i = x
- 1 \leq B_i
- \displaystyle\sum_{i = 1}^{N} B_i \leq 1500
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
すべてのチームの強さが等しくなるようにできる場合は所属するチームを変更する人数として考えられる最小値を出力せよ。そうでない場合は -1 を出力せよ。
入力例 1
6 1 2 2 5 1 5 3 3 1 3 3 6
出力例 1
2
人 1 がチーム 3、人 4 がチーム 2 へと所属するチームを変更することですべてのチームの強さが 8 となります。
入力例 2
4 1 1 1 2 2 3 3 4
出力例 2
-1
入力例 3
3 1 1 2 1 3 1
出力例 3
0
入力例 4
12 2 5 1 4 3 3 2 3 3 9 1 2 2 2 3 9 2 6 1 9 1 1 3 1
出力例 4
3
Score : 450 points
Problem Statement
There are N people divided into three teams.
The people are numbered 1, 2, \ldots, N, and the teams are numbered 1, 2, 3. Currently, person i belongs to team A_i.
Each person has a value called strength; person i has a strength of B_i. The strength of a team is defined as the sum of the strengths of its members.
Determine whether it is possible for zero or more people to switch teams so that all teams have equal strength. If it is possible, find the minimum number of people who need to switch teams to achieve this.
You cannot create new teams other than teams 1, 2, 3.
Constraints
- 3 \leq N \leq 100
- A_i \in \lbrace 1, 2, 3 \rbrace
- For each x \in \lbrace 1, 2, 3 \rbrace, there exists some i with A_i = x.
- 1 \leq B_i
- \displaystyle\sum_{i = 1}^{N} B_i \leq 1500
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
If it is possible to make all teams have equal strength, print the minimum number of people who need to switch teams. Otherwise, print -1
.
Sample Input 1
6 1 2 2 5 1 5 3 3 1 3 3 6
Sample Output 1
2
If person 1 switches to team 3 and person 4 switches to team 2, all teams will have a strength of 8.
Sample Input 2
4 1 1 1 2 2 3 3 4
Sample Output 2
-1
Sample Input 3
3 1 1 2 1 3 1
Sample Output 3
0
Sample Input 4
12 2 5 1 4 3 3 2 3 3 9 1 2 2 2 3 9 2 6 1 9 1 1 3 1
Sample Output 4
3
実行時間制限: 2.5 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
AtCoder国には 1 から N の番号がついた N 個の都市と、1 から M の番号がついた M 本の道路があります。
道路 i は都市 A_i と都市 B_i を双方向に結び長さは C_i です。
Q 個のクエリが与えられるので順に処理してください。クエリは以下の 2 種類のどちらかです。
1 i
:道路 i が通行止めとなる。2 x y
:通行止めでない道路のみを通るときの都市 x から都市 y への最短距離を出力する。都市 x から都市 y に到達できないときは代わりに-1
を出力する。
なお、1 種類目のクエリはテストケースごとに 300 回以下であることが保証されます。
制約
- 2 \leq N \leq 300
- 0 \leq M \leq \frac{N(N-1)}{2}
- 1\leq A_i < B_i \leq N
- (A_i,B_i) は相異なる
- 1 \leq C_i \leq 10^9
- 1 \leq Q \leq 2\times 10^5
- 1 種類目のクエリにおいて、1\leq i \leq M
- 1 種類目のクエリにおいて与えられる道路は、その時点で通行止めでない
- 1 種類目のクエリは 300 回以下である
- 2 種類目のクエリにおいて、1\leq x < y \leq N
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M Q A_1 B_1 C_1 \vdots A_M B_M C_M \mathrm{query}_1 \vdots \mathrm{query}_Q
各クエリは次の 2 種類の形式のどちらかである。
1 i
2 x y
出力
クエリを順に処理せよ。
入力例 1
3 3 5 1 2 5 1 3 10 2 3 6 2 1 3 1 2 2 1 3 1 1 2 1 3
出力例 1
10 11 -1
- 1 番目のクエリでは、都市 1 から都市 3 への最短距離である 10 を出力します。
- 2 番目のクエリにより、道路 2 が通行止めとなりました。
- 3 番目のクエリでは、都市 1 から都市 3 への最短距離である 11 を出力します。
- 4 番目のクエリにより、道路 1 が通行止めとなりました。
- 5 番目のクエリでは、都市 1 から都市 3 には到達できないので、
-1
を出力します。
入力例 2
4 6 6 2 3 1 2 4 1 3 4 1 1 2 1 1 3 1 1 4 1 1 4 1 5 1 6 2 1 2 2 1 3 2 1 4
出力例 2
-1 -1 -1
Score : 550 points
Problem Statement
In the nation of AtCoder, there are N cities numbered 1 to N, and M roads numbered 1 to M.
Road i connects cities A_i and B_i bidirectionally and has a length of C_i.
You are given Q queries to process in order. The queries are of the following two types.
1 i
: Road i becomes closed.2 x y
: Print the shortest distance from city x to city y, using only roads that are not closed. If city y cannot be reached from city x, print-1
instead.
It is guaranteed that each test case contains at most 300 queries of the first type.
Constraints
- 2 \leq N \leq 300
- 0 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq A_i < B_i \leq N
- All pairs (A_i, B_i) are distinct.
- 1 \leq C_i \leq 10^9
- 1 \leq Q \leq 2 \times 10^5
- In the queries of the first type, 1 \leq i \leq M.
- The road given in a query of the first type is not already closed at that time.
- The number of queries of the first type is at most 300.
- In the queries of the second type, 1 \leq x < y \leq N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M Q A_1 B_1 C_1 \vdots A_M B_M C_M \mathrm{query}_1 \vdots \mathrm{query}_Q
Each query is in one of the following two formats:
1 i
2 x y
Output
Process the queries in order.
Sample Input 1
3 3 5 1 2 5 1 3 10 2 3 6 2 1 3 1 2 2 1 3 1 1 2 1 3
Sample Output 1
10 11 -1
- In the first query, print the shortest distance from city 1 to city 3, which is 10.
- In the second query, road 2 becomes closed.
- In the third query, print the shortest distance from city 1 to city 3, which is 11.
- In the fourth query, road 1 becomes closed.
- In the fifth query, city 3 cannot be reached from city 1, so print
-1
.
Sample Input 2
4 6 6 2 3 1 2 4 1 3 4 1 1 2 1 1 3 1 1 4 1 1 4 1 5 1 6 2 1 2 2 1 3 2 1 4
Sample Output 2
-1 -1 -1
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 575 点
問題文
AtCoder国には 1 から N の番号がついた N 個の都市と、1 から M の番号がついた M 本の道路があります。
道路 i は都市 A_i と都市 B_i を双方向に結び長さは C_i です。
各 i=1,\ldots,M について、以下の 2 つの値が異なるかどうか判定してください。
- 全ての道路が通行可能なときの、都市 1 から都市 N への最短距離
- 道路 i 以外の M-1 本の道路が通行可能なときの、都市 1 から都市 N への最短距離
ただし、一方では都市 1 から都市 N に到達可能で、他方では到達不可能なとき、両者の値は異なるとみなします。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1\leq A_i < B_i \leq N
- (A_i,B_i) は相異なる
- 1 \leq C_i \leq 10^9
- 全ての道路が通行可能なとき、都市 1 から都市 N へは到達可能
- 入力は全ては整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 \vdots A_M B_M C_M
出力
M 行出力せよ。i 行目には、「全ての道路が通行可能なときの、都市 1 から都市 N への最短距離」と「道路 i 以外の M-1 本の道路が通行可能なときの、都市 1 から都市 N への最短距離」が異なるとき Yes
、同じとき No
と出力せよ。
ただし、一方では都市 1 から都市 N に到達可能で、他方では到達不可能なとき、両者の値は異なるとみなす。
入力例 1
3 3 1 2 5 1 3 10 2 3 6
出力例 1
No Yes No
全ての道路が通行可能なとき、都市 1 から都市 3 への最短距離は 10 です。
- 道路 1 以外の 2 本の道路が通行可能なときの、都市 1 から都市 3 への最短距離 は 10 です
- 道路 2 以外の 2 本の道路が通行可能なときの、都市 1 から都市 3 への最短距離 は 11 です
- 道路 3 以外の 2 本の道路が通行可能なときの、都市 1 から都市 3 への最短距離 は 10 です
入力例 2
4 6 2 3 1 2 4 1 3 4 1 1 2 1 1 3 1 1 4 1
出力例 2
No No No No No Yes
全ての道路が通行可能なとき、都市 1 から都市 4 への最短距離は 1 です。
道路 6 以外の 5 本の道路が通行可能なときの、都市 1 から都市 4 への最短距離 は 2 です。
入力例 3
2 1 1 2 1
出力例 3
Yes
道路 1 以外の 0 本の道路が通行可能なとき、都市 1 から都市 2 へは到達できません。
Score : 575 points
Problem Statement
In the nation of AtCoder, there are N cities numbered 1 to N, and M roads numbered 1 to M.
Road i connects cities A_i and B_i bidirectionally and has a length of C_i.
For each i = 1, \ldots, M, determine whether the following two values are different.
- The shortest distance from city 1 to city N when all roads are passable
- The shortest distance from city 1 to city N when the M - 1 roads other than road i are passable
If city N can be reached from city 1 in one of these cases but not the other, the two values are considered different.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i < B_i \leq N
- All pairs (A_i, B_i) are distinct.
- 1 \leq C_i \leq 10^9
- City N can be reached from city 1 when all roads are passable.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 C_1 \vdots A_M B_M C_M
Output
Print M lines. The i-th line should contain Yes
if the shortest distance from city 1 to city N when all roads are passable is different from the shortest distance when the M - 1 roads other than road i are passable, and No
otherwise.
If city N can be reached from city 1 in one of these cases but not the other, the two values are considered different.
Sample Input 1
3 3 1 2 5 1 3 10 2 3 6
Sample Output 1
No Yes No
When all roads are passable, the shortest distance from city 1 to city 3 is 10.
- When the two roads other than road 1 are passable, the shortest distance is 10.
- When the two roads other than road 2 are passable, the shortest distance is 11.
- When the two roads other than road 3 are passable, the shortest distance is 10.
Sample Input 2
4 6 2 3 1 2 4 1 3 4 1 1 2 1 1 3 1 1 4 1
Sample Output 2
No No No No No Yes
When all roads are passable, the shortest distance from city 1 to city 4 is 1.
When the five roads other than road 6 are passable, the shortest distance is 2.
Sample Input 3
2 1 1 2 1
Sample Output 3
Yes
When the zero roads other than road 1 are passable, city 2 cannot be reached from city 1.