Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は文化祭でスタンプラリーの係をしています。来場者には H 行 W 列のマス目が描かれた台紙が配られます。台紙の行には上から順に 1 から H の番号が、列には左から順に 1 から W の番号が付いています。最初、台紙のすべてのマスは .(空白)です。
スタンプラリーで使うスタンプは R 行 C 列のグリッドで表されます。スタンプの各マスは #(インクが付く部分)または .(インクが付かない部分)のいずれかです。スタンプの行にも上から順に 1 から R の、列にも左から順に 1 から C の番号が付いています。スタンプの第 i 行は文字列 S_i で表されます。
高橋君はある来場者の台紙に、同じスタンプを合計 N 回押します。j 回目(1 \leq j \leq N)のスタンプ押しは、整数の組 (r_j, c_j) で指定されます。ここで r_j および c_j は任意の整数であり、0 以下の値や台紙のサイズを超える値を取ることもあります。
(r_j, c_j) の意味は次の通りです。スタンプの第 1 行第 1 列(左上隅)が台紙の第 r_j 行第 c_j 列の位置に対応するようにスタンプを配置します。すなわち、スタンプの第 a 行第 b 列(1 \leq a \leq R, 1 \leq b \leq C)は、台紙の第 (r_j + a - 1) 行第 (c_j + b - 1) 列に対応します。
スタンプを押すと、スタンプの各マス(第 a 行第 b 列)について以下の処理が行われます。
- 対応する台紙上の位置 (r_j + a - 1,\; c_j + b - 1) が台紙の範囲外である場合、すなわち r_j + a - 1 < 1 または r_j + a - 1 > H または c_j + b - 1 < 1 または c_j + b - 1 > W のとき、そのマスは無視されます。
- 対応する台紙上の位置が台紙の範囲内であり、かつスタンプ上のそのマスが
#であるとき、台紙上の対応するマスを#に変更します(すでに#であればそのままです)。 - 対応する台紙上の位置が台紙の範囲内であり、かつスタンプ上のそのマスが
.であるとき、台紙上の対応するマスには何も変化を与えません。
N 回すべてのスタンプを押した後の台紙の最終的な状態を出力してください。
制約
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- 1 \leq R \leq 100
- 1 \leq C \leq 100
- S_i は
#と.からなる長さ C の文字列である(1 \leq i \leq R) - 1 \leq N \leq 1000
- -10^4 \leq r_j \leq 10^4(1 \leq j \leq N)
- -10^4 \leq c_j \leq 10^4(1 \leq j \leq N)
- 入力で与えられる値はすべて整数である(文字列を除く)
入力
H W R C S_1 S_2 \vdots S_R N r_1 c_1 r_2 c_2 \vdots r_N c_N
- 1 行目には、台紙の行数 H と列数 W がスペース区切りで与えられる。
- 2 行目には、スタンプの行数 R と列数 C がスペース区切りで与えられる。
- 続く R 行にわたって、スタンプの形状が与えられる。(2 + i) 行目(1 \leq i \leq R)には、スタンプの第 i 行の形状を表す文字列 S_i が与えられる。
- (3 + R) 行目には、スタンプを押す回数 N が与えられる。
- 続く N 行には、各スタンプ押しの位置が与えられる。(3 + R + j) 行目(1 \leq j \leq N)には、j 回目のスタンプ押しにおけるスタンプ左上隅の位置を表す整数 r_j(行番号)と c_j(列番号)がスペース区切りで与えられる。
出力
N 回のスタンプ押し後の台紙の状態を H 行で出力せよ。i 行目(1 \leq i \leq H)には、台紙の第 i 行の状態を # と . からなる長さ W の文字列として出力する。
入力例 1
5 5 2 2 #. .# 2 1 1 3 3
出力例 1
#.... .#... ..#.. ...#. .....
入力例 2
7 7 3 3 .#. ### .#. 3 1 1 5 5 0 -1
出力例 2
##..... ###.... .#..... ....... .....#. ....### .....#.
入力例 3
10 10 4 4 .##. #### #### .##. 4 -1 -1 2 7 8 3 1 1
出力例 3
###....... ####...##. ####..#### .##...#### .......##. .......... .......... ...##..... ..####.... ..####....
Score : 266 pts
Problem Statement
Takahashi is in charge of a stamp rally at a cultural festival. Visitors are given a sheet with an H \times W grid. The rows of the sheet are numbered 1 to H from top to bottom, and the columns are numbered 1 to W from left to right. Initially, all cells on the sheet are . (blank).
The stamp used in the stamp rally is represented by an R \times C grid. Each cell of the stamp is either # (inked part) or . (non-inked part). The rows of the stamp are numbered 1 to R from top to bottom, and the columns are numbered 1 to C from left to right. The i-th row of the stamp is represented by the string S_i.
Takahashi stamps a visitor's sheet a total of N times using the same stamp. The j-th stamping (1 \leq j \leq N) is specified by a pair of integers (r_j, c_j). Here, r_j and c_j can be any integers, including values that are 0 or less, or values that exceed the size of the sheet.
The meaning of (r_j, c_j) is as follows: the stamp is placed so that row 1, column 1 (the top-left corner) of the stamp corresponds to row r_j, column c_j of the sheet. That is, row a, column b of the stamp (1 \leq a \leq R, 1 \leq b \leq C) corresponds to row (r_j + a - 1), column (c_j + b - 1) of the sheet.
When the stamp is pressed, the following processing is performed for each cell (row a, column b) of the stamp:
- If the corresponding position on the sheet (r_j + a - 1,\; c_j + b - 1) is outside the sheet, i.e., r_j + a - 1 < 1 or r_j + a - 1 > H or c_j + b - 1 < 1 or c_j + b - 1 > W, that cell is ignored.
- If the corresponding position on the sheet is within bounds and the stamp cell is
#, the corresponding cell on the sheet is changed to#(if it is already#, it remains unchanged). - If the corresponding position on the sheet is within bounds and the stamp cell is
., no change is made to the corresponding cell on the sheet.
Output the final state of the sheet after all N stampings have been performed.
Constraints
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- 1 \leq R \leq 100
- 1 \leq C \leq 100
- S_i is a string of length C consisting of
#and.(1 \leq i \leq R) - 1 \leq N \leq 1000
- -10^4 \leq r_j \leq 10^4 (1 \leq j \leq N)
- -10^4 \leq c_j \leq 10^4 (1 \leq j \leq N)
- All given values are integers (except for strings)
Input
H W R C S_1 S_2 \vdots S_R N r_1 c_1 r_2 c_2 \vdots r_N c_N
- The first line contains the number of rows H and number of columns W of the sheet, separated by a space.
- The second line contains the number of rows R and number of columns C of the stamp, separated by a space.
- The following R lines give the shape of the stamp. The (2 + i)-th line (1 \leq i \leq R) contains the string S_i representing the shape of the i-th row of the stamp.
- The (3 + R)-th line contains the number of stampings N.
- The following N lines give the position of each stamping. The (3 + R + j)-th line (1 \leq j \leq N) contains the integers r_j (row number) and c_j (column number) representing the position of the top-left corner of the stamp for the j-th stamping, separated by a space.
Output
Output the state of the sheet after N stampings in H lines. The i-th line (1 \leq i \leq H) should contain a string of length W consisting of # and . representing the state of the i-th row of the sheet.
Sample Input 1
5 5 2 2 #. .# 2 1 1 3 3
Sample Output 1
#.... .#... ..#.. ...#. .....
Sample Input 2
7 7 3 3 .#. ### .#. 3 1 1 5 5 0 -1
Sample Output 2
##..... ###.... .#..... ....... .....#. ....### .....#.
Sample Input 3
10 10 4 4 .##. #### #### .##. 4 -1 -1 2 7 8 3 1 1
Sample Output 3
###....... ####...##. ####..#### .##...#### .......##. .......... .......... ...##..... ..####.... ..####....
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君のクラスでは期末テストが行われました。このテストには N 人の生徒が参加し、全部で M 問の問題が出題されました。
j 番目の問題 (1 \leq j \leq M) には配点 P_j が設定されています。各生徒の合計点は、その生徒が正解した問題の配点の総和です(不正解の問題の配点は加算されません)。
i 番目の生徒 (1 \leq i \leq N) の解答結果は文字列 S_i で表されます。S_i は長さ M の文字列であり、j 文字目が o ならば j 番目の問題に正解したことを、x ならば不正解であったことを意味します。
各生徒の合計点をもとに順位を求めてください。ある生徒の順位は「その生徒より合計点が真に高い生徒の人数 + 1」で定義されます。したがって、同じ合計点の生徒が複数いる場合は同じ順位となります。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- N \times M \leq 5 \times 10^6
- 1 \leq P_j \leq 1000 (1 \leq j \leq M)
- S_i は長さ M の文字列であり、
oとxのみからなる (1 \leq i \leq N) - N, M, P_j はすべて整数
入力
N M P_1 P_2 \ldots P_M S_1 S_2 \vdots S_N
- 1 行目には、生徒の人数を表す整数 N と問題数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各問題の配点を表す整数 P_1, P_2, \ldots, P_M が、スペース区切りで与えられる。
- (2+i) 行目 (1 \leq i \leq N) には、i 番目の生徒の解答結果を表す文字列 S_i が与えられる。
出力
N 行出力してください。i 行目 (1 \leq i \leq N) には、i 番目の生徒の順位を出力してください。
入力例 1
5 3 100 200 300 oxo ooo xxo oox xxx
出力例 1
2 1 3 3 5
入力例 2
4 4 10 20 30 40 ooxx xxoo oxox xoxo
出力例 2
4 1 3 2
入力例 3
10 5 50 30 20 80 10 ooooo oxxxx xoxox ooxoo xxxxx oxoxo oooox xxoox xooox oxxxo
出力例 3
1 9 5 3 10 7 2 6 4 8
入力例 4
20 8 15 25 35 45 55 65 75 85 oooooooo xxxxxxxx oxoxoxox xoxoxoxo ooooxxxx xxxxoooo oxxooxxo xooxxoox ooxxooxx xxooxxoo oxooxoxo xoxxoxox oxxxoooo xoooxoox oxooxxox xoxxoxxo oooxoxox xxxoxxoo oxoxooxx xoxooxoo
出力例 4
1 20 13 8 19 4 11 11 17 7 5 18 2 5 14 16 9 9 14 3
入力例 5
1 1 1000 o
出力例 5
1
Score : 300 pts
Problem Statement
A final exam was held in Takahashi's class. N students participated in this exam, and a total of M problems were given.
The j-th problem (1 \leq j \leq M) has a score of P_j. Each student's total score is the sum of the scores of the problems that the student answered correctly (scores of incorrectly answered problems are not added).
The answer result of the i-th student (1 \leq i \leq N) is represented by a string S_i. S_i is a string of length M, where the j-th character being o means the student answered the j-th problem correctly, and x means the student answered it incorrectly.
Determine the rank of each student based on their total scores. The rank of a student is defined as "the number of students whose total score is strictly higher than that student's + 1". Therefore, students with the same total score will have the same rank.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- N \times M \leq 5 \times 10^6
- 1 \leq P_j \leq 1000 (1 \leq j \leq M)
- S_i is a string of length M consisting only of
oandx(1 \leq i \leq N) - N, M, P_j are all integers
Input
N M P_1 P_2 \ldots P_M S_1 S_2 \vdots S_N
- The first line contains an integer N representing the number of students and an integer M representing the number of problems, separated by a space.
- The second line contains integers P_1, P_2, \ldots, P_M representing the score of each problem, separated by spaces.
- The (2+i)-th line (1 \leq i \leq N) contains the string S_i representing the answer result of the i-th student.
Output
Print N lines. The i-th line (1 \leq i \leq N) should contain the rank of the i-th student.
Sample Input 1
5 3 100 200 300 oxo ooo xxo oox xxx
Sample Output 1
2 1 3 3 5
Sample Input 2
4 4 10 20 30 40 ooxx xxoo oxox xoxo
Sample Output 2
4 1 3 2
Sample Input 3
10 5 50 30 20 80 10 ooooo oxxxx xoxox ooxoo xxxxx oxoxo oooox xxoox xooox oxxxo
Sample Output 3
1 9 5 3 10 7 2 6 4 8
Sample Input 4
20 8 15 25 35 45 55 65 75 85 oooooooo xxxxxxxx oxoxoxox xoxoxoxo ooooxxxx xxxxoooo oxxooxxo xooxxoox ooxxooxx xxooxxoo oxooxoxo xoxxoxox oxxxoooo xoooxoox oxooxxox xoxxoxxo oooxoxox xxxoxxoo oxoxooxx xoxooxoo
Sample Output 4
1 20 13 8 19 4 11 11 17 7 5 18 2 5 14 16 9 9 14 3
Sample Input 5
1 1 1000 o
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は N 日間の読書チャレンジに参加しています。第 i 日( 1 \leq i \leq N )には A_i ページを読む予定です。高橋君の読書速度は一定で、 1 ページ読むのに C 分かかります。
このチャレンジでは、連続する K 日以上の期間を選んで「読書期間」として報告できます。具体的には、 1 \leq l \leq r \leq N かつ r - l + 1 \geq K を満たす整数の組 (l, r) を選び、第 l 日から第 r 日までを読書期間とします。ただし、読書期間として報告できるのは、その期間の合計読書時間が制限時間 T 分以下である場合に限ります。第 l 日から第 r 日までの合計読書時間は C \times (A_l + A_{l+1} + \cdots + A_r) 分です。
報告可能な読書期間の個数を求めてください。すなわち、次の条件をともに満たす整数の組 (l, r) の個数を求めてください。
- 1 \leq l \leq r \leq N かつ r - l + 1 \geq K
- C \times (A_l + A_{l+1} + \cdots + A_r) \leq T
制約
- 1 \leq N \leq 2 \times 10^6
- 1 \leq C \leq 10^9
- 1 \leq T \leq 10^{18}
- 1 \leq K \leq N
- 1 \leq A_i \leq 10^9( 1 \leq i \leq N )
- 入力はすべて整数である
入力
N C T K A_1 A_2 \cdots A_N
- 1 行目には、日数を表す整数 N 、 1 ページあたりの所要時間(分)を表す整数 C 、制限時間(分)を表す整数 T 、読書期間の最小日数を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、第 i 日に読む予定のページ数を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
条件を満たす組 (l, r) の個数を 1 行で出力せよ。
入力例 1
5 2 20 2 3 4 5 2 1
出力例 1
5
入力例 2
4 10 50 2 3 4 2 5
出力例 2
0
入力例 3
12 3 60 3 4 8 6 10 3 7 5 12 2 9 4 6
出力例 3
7
入力例 4
50 7 1000 5 12 25 7 30 18 22 15 9 40 11 13 27 5 34 20 16 8 29 24 6 10 31 19 14 28 17 21 4 35 23 26 12 9 33 7 18 25 11 30 6 15 22 13 27 5 20 16 8 24 10
出力例 4
147
入力例 5
1 1000000000 1000000000000000000 1 1000000000
出力例 5
1
Score : 366 pts
Problem Statement
Takahashi is participating in an N-day reading challenge. On day i (1 \leq i \leq N), he plans to read A_i pages. Takahashi's reading speed is constant, taking C minutes to read one page.
In this challenge, he can select a period of K or more consecutive days and report it as a "reading period." Specifically, he chooses a pair of integers (l, r) satisfying 1 \leq l \leq r \leq N and r - l + 1 \geq K, and designates the days from day l to day r as the reading period. However, a reading period can only be reported if the total reading time during that period is at most the time limit of T minutes. The total reading time from day l to day r is C \times (A_l + A_{l+1} + \cdots + A_r) minutes.
Find the number of reading periods that can be reported. That is, find the number of pairs of integers (l, r) that satisfy both of the following conditions:
- 1 \leq l \leq r \leq N and r - l + 1 \geq K
- C \times (A_l + A_{l+1} + \cdots + A_r) \leq T
Constraints
- 1 \leq N \leq 2 \times 10^6
- 1 \leq C \leq 10^9
- 1 \leq T \leq 10^{18}
- 1 \leq K \leq N
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- All inputs are integers
Input
N C T K A_1 A_2 \cdots A_N
- The first line contains the integer N representing the number of days, the integer C representing the time (in minutes) per page, the integer T representing the time limit (in minutes), and the integer K representing the minimum number of days for a reading period, separated by spaces.
- The second line contains the integers A_1, A_2, \ldots, A_N representing the number of pages planned to be read on day i, separated by spaces.
Output
Output the number of pairs (l, r) that satisfy the conditions, on a single line.
Sample Input 1
5 2 20 2 3 4 5 2 1
Sample Output 1
5
Sample Input 2
4 10 50 2 3 4 2 5
Sample Output 2
0
Sample Input 3
12 3 60 3 4 8 6 10 3 7 5 12 2 9 4 6
Sample Output 3
7
Sample Input 4
50 7 1000 5 12 25 7 30 18 22 15 9 40 11 13 27 5 34 20 16 8 29 24 6 10 31 19 14 28 17 21 4 35 23 26 12 9 33 7 18 25 11 30 6 15 22 13 27 5 20 16 8 24 10
Sample Output 4
147
Sample Input 5
1 1000000000 1000000000000000000 1 1000000000
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は N 個のブロック塔を左から右に一列に並べて作ろうとしています。 i 番目のブロック塔の目標の高さは H_i 段です。
組み立ては 1 ステップに 1 個のブロックを積む作業で進みます。各ステップでは、まだ目標の高さに達していないブロック塔を 1 つ選び、その塔の上にブロックを 1 個載せます(高さが 1 増えます)。すべてのブロック塔は初期状態では高さ 0 であり、すべてのブロック塔が目標の高さに達したとき組み立ては完了です。
高橋君は、展示の見栄えを良くするため、次の条件を常に守る必要があります。
見栄え条件: 各ステップの直後において、ブロック塔の列を左端から眺めたときに「見える塔の本数」が K 本以上でなければならない。
ブロック塔の列を左端から眺めたとき、 i 番目の塔が「見える」とは、 i 番目の塔の現在の高さが 1 以上であり、かつ i より左にあるすべての塔の現在の高さよりも真に大きいことです。ただし i = 1 のときは、高さが 1 以上であれば見えるとします。
より厳密には、現在の各塔の高さを h_1, h_2, \ldots, h_N としたとき、塔 i が見えるとは、 h_i \geq 1 かつ h_i > \max(h_1, h_2, \ldots, h_{i-1}) を満たすことです(ただし i = 1 のときは h_1 \geq 1 であれば見えるとします)。
高橋君は、この見栄え条件を守りながら組み立てを完了できるかどうかを判定したいと考えています。さらに、完了できる場合は、条件を満たす作業手順が何通りあるかを求めたいです。
条件を満たしながらすべてのブロック塔を目標の高さまで積み上げることが可能ならば、そのような作業手順の総数を 10^9 + 7 で割った余りを出力してください。不可能ならば 0 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- 1 \leq H_i \leq 10^6
- \sum_{i=1}^{N} H_i \leq 10^6
- 入力はすべて整数である
入力
N K H_1 H_2 \ldots H_N
- 1 行目には、ブロック塔の個数を表す N と、見栄え条件で必要な見える塔の最小本数を表す K が、スペース区切りで与えられる。
- 2 行目には、各ブロック塔の目標の高さを表す H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
出力
見栄え条件を満たしながらすべてのブロック塔を目標の高さまで組み立てる作業手順の総数を 10^9 + 7 で割った余りを 1 行で出力せよ。条件を満たす手順が存在しない場合は 0 を出力せよ。
入力例 1
3 1 1 2 1
出力例 1
12
入力例 2
4 2 1 1 1 1
出力例 2
0
入力例 3
8 1 3 1 4 2 5 2 1 3
出力例 3
821889649
入力例 4
20 1 12 7 25 18 3 30 11 6 22 14 9 27 5 16 20 8 24 13 10 19
出力例 4
85505878
入力例 5
1 1 1
出力例 5
1
Score : 400 pts
Problem Statement
Takahashi is trying to build N block towers lined up in a row from left to right. The target height of the i-th block tower is H_i blocks.
The construction proceeds in steps, where one block is stacked in each step. In each step, Takahashi chooses one block tower that has not yet reached its target height, and places one block on top of it (increasing its height by 1). Initially, all block towers have a height of 0. The construction is complete when all block towers have reached their target heights.
To make the exhibition look good, Takahashi must always satisfy the following condition:
Appearance Condition: Immediately after each step, when looking at the row of block towers from the left end, the "number of visible towers" must be at least K.
When looking at the row of block towers from the left end, the i-th tower is "visible" if its current height is at least 1, and it is strictly greater than the current heights of all towers to its left. For i = 1, it is visible if its height is at least 1.
More precisely, letting h_1, h_2, \ldots, h_N be the current heights of the towers, tower i is visible if h_i \geq 1 and h_i > \max(h_1, h_2, \ldots, h_{i-1}) (for i = 1, it is visible if h_1 \geq 1).
Takahashi wants to determine whether it is possible to complete the construction while satisfying this appearance condition. Furthermore, if it is possible, he wants to find the number of valid construction procedures (sequences of steps).
If it is possible to stack all block towers to their target heights while satisfying the condition, output the total number of such construction procedures modulo 10^9 + 7. If it is impossible, output 0.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- 1 \leq H_i \leq 10^6
- \sum_{i=1}^{N} H_i \leq 10^6
- All input values are integers.
Input
N K H_1 H_2 \ldots H_N
- The first line contains N, the number of block towers, and K, the minimum number of visible towers required by the appearance condition, separated by a space.
- The second line contains H_1, H_2, \ldots, H_N, the target heights of each block tower, separated by a space.
Output
Print the total number of construction procedures to build all block towers to their target heights while satisfying the appearance condition, modulo 10^9 + 7, in a single line. If no such procedure exists, print 0.
Sample Input 1
3 1 1 2 1
Sample Output 1
12
Sample Input 2
4 2 1 1 1 1
Sample Output 2
0
Sample Input 3
8 1 3 1 4 2 5 2 1 3
Sample Output 3
821889649
Sample Input 4
20 1 12 7 25 18 3 30 11 6 22 14 9 27 5 16 20 8 24 13 10 19
Sample Output 4
85505878
Sample Input 5
1 1 1
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は配送ドライバーとして働いています。今日は複数の届け先を回って荷物を届ける仕事があります。
高橋君が担当するエリアには N 個の地点があり、地点は 1 から N までの番号で管理されています。地点と地点の間には M 本の双方向の道路があり、 i 本目の道路は地点 U_i と地点 V_i を結んでおり、移動にかかる時間は W_i です。
高橋君は地点 1 にある営業所を出発し、最終的に地点 N にある倉庫に到着しなければなりません。その途中で、指定された K 個の届け先にすべて立ち寄って荷物を届ける必要があります。これらの届け先は C_1, C_2, \dots, C_K で与えられます。ただし、届け先を訪れる順番は自由です。
高橋君はできるだけ早く配送を終えたいと考えています。地点 1 を出発し、 K 個の届け先をすべて訪れ、最終的に地点 N に到着するまでの移動時間の合計の最小値を求めてください。
なお、届け先以外の地点を途中で通過することは自由にでき、同じ地点や同じ道路を複数回通ることも許されます。すべての届け先を訪れて地点 N に到着することが可能であることは保証されます。
制約
- 2 \leq N \leq 10^5
- 1 \leq M \leq 5 \times 10^4
- 1 \leq K \leq 15
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i
- 1 \leq W_i \leq 10^9
- 1 \leq C_j \leq N
- C_j はすべて異なる
- C_j \neq 1 かつ C_j \neq N
- すべての届け先を訪れて地点 N に到着することが可能である
- 入力はすべて整数
入力
N M K U_1 V_1 W_1 U_2 V_2 W_2 : U_M V_M W_M C_1 C_2 \dots C_K
- 1 行目には、地点の数を表す N 、道路の数を表す M 、届け先の数を表す K が、スペース区切りで与えられる。
- 2 行目から M + 1 行目では、道路の情報が M 行で与えられる。
- 1 + i 行目では、 i 本目の道路が結ぶ地点 U_i 、 V_i 、および移動時間 W_i が、スペース区切りで与えられる。
- M + 2 行目には、届け先である K 個の地点の番号 C_1, C_2, \dots, C_K が、スペース区切りで与えられる。
出力
地点 1 を出発し、指定された K 個の届け先をすべて訪れ、地点 N に到着するまでの移動時間の合計の最小値を 1 行で出力せよ。
入力例 1
5 6 2 1 2 2 2 5 5 1 3 4 3 4 1 4 5 3 2 4 2 2 4
出力例 1
7
入力例 2
4 4 1 1 2 10 2 4 10 1 3 1 3 2 1 2
出力例 2
12
入力例 3
10 15 4 1 2 3 1 3 8 2 4 2 3 4 1 4 5 4 5 10 7 4 6 6 6 10 3 2 7 5 7 8 2 8 5 2 3 9 4 9 6 5 7 9 3 8 10 10 5 7 9 6
出力例 3
24
入力例 4
20 35 8 1 2 4 2 3 6 3 4 5 4 5 3 5 6 7 6 7 2 7 8 4 8 9 6 9 10 5 10 11 3 11 12 8 12 13 4 13 14 6 14 15 2 15 16 5 16 17 7 17 18 3 18 19 4 19 20 6 1 5 15 2 8 9 3 10 12 4 11 7 5 12 10 6 14 8 7 15 11 8 16 6 9 17 9 10 18 13 11 19 7 12 20 14 3 7 4 13 18 5 2 14 20 6 19 16 4 7 9 12 14 16 18 19
出力例 4
82
入力例 5
3 2 1 1 2 1000000000 2 3 1000000000 2
出力例 5
2000000000
Score : 433 pts
Problem Statement
Takahashi works as a delivery driver. Today, he has a job to deliver packages to multiple destinations.
In Takahashi's service area, there are N locations, numbered 1 to N. There are M bidirectional roads connecting these locations. The i-th road connects location U_i and location V_i, and the travel time is W_i.
Takahashi must start from the office at location 1 and eventually arrive at the warehouse at location N. Along the way, he must visit all of the K designated delivery destinations to deliver the packages. These destinations are given as C_1, C_2, \dots, C_K. The order in which he visits these destinations is arbitrary.
Takahashi wants to finish the deliveries as quickly as possible. Find the minimum total travel time to start from location 1, visit all K destinations, and finally arrive at location N.
Note that he can pass through locations other than the destinations, and he is allowed to visit the same location or road multiple times. It is guaranteed that it is possible to visit all destinations and arrive at location N.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq M \leq 5 \times 10^4
- 1 \leq K \leq 15
- 1 \leq U_i, V_i \leq N
- U_i \neq V_i
- 1 \leq W_i \leq 10^9
- 1 \leq C_j \leq N
- C_j are all distinct
- C_j \neq 1 and C_j \neq N
- It is guaranteed that it is possible to visit all destinations and arrive at location N.
- All input values are integers
Input
N M K U_1 V_1 W_1 U_2 V_2 W_2 : U_M V_M W_M C_1 C_2 \dots C_K
- On the first line, the number of locations N, the number of roads M, and the number of destinations K are given, separated by spaces.
- From the 2nd line to the (M + 1)-th line, the road information is given in M lines.
- On the (1 + i)-th line, the locations U_i and V_i connected by the i-th road, and the travel time W_i, are given, separated by spaces.
- On the (M + 2)-th line, the location numbers of the K destinations C_1, C_2, \dots, C_K are given, separated by spaces.
Output
Print the minimum total travel time to start from location 1, visit all K designated destinations, and arrive at location N in a single line.
Sample Input 1
5 6 2 1 2 2 2 5 5 1 3 4 3 4 1 4 5 3 2 4 2 2 4
Sample Output 1
7
Sample Input 2
4 4 1 1 2 10 2 4 10 1 3 1 3 2 1 2
Sample Output 2
12
Sample Input 3
10 15 4 1 2 3 1 3 8 2 4 2 3 4 1 4 5 4 5 10 7 4 6 6 6 10 3 2 7 5 7 8 2 8 5 2 3 9 4 9 6 5 7 9 3 8 10 10 5 7 9 6
Sample Output 3
24
Sample Input 4
20 35 8 1 2 4 2 3 6 3 4 5 4 5 3 5 6 7 6 7 2 7 8 4 8 9 6 9 10 5 10 11 3 11 12 8 12 13 4 13 14 6 14 15 2 15 16 5 16 17 7 17 18 3 18 19 4 19 20 6 1 5 15 2 8 9 3 10 12 4 11 7 5 12 10 6 14 8 7 15 11 8 16 6 9 17 9 10 18 13 11 19 7 12 20 14 3 7 4 13 18 5 2 14 20 6 19 16 4 7 9 12 14 16 18 19
Sample Output 4
82
Sample Input 5
3 2 1 1 2 1000000000 2 3 1000000000 2
Sample Output 5
2000000000