QOJ.ac

QOJ

시간 제한: 2 s 메모리 제한: 64 MB 총점: 100

#23772. 船旅

통계
問題
6
   船旅

問題

JOI国には,n 島の島があり, 各島には 1 から n までの番号が付けられている. 現在,JOI国では各島の間を結ぶ航路網の整備が進んでいる.

あなたは,船舶の切符を扱っているチケットセンターに勤務している.JOI国には船舶を使って,できる限り安価に, 島と島の間を旅行したいと考えている人が沢山おり, 彼らは,出発地と目的地を注文票に記入して,あなたのところに送ってくる.
あなたの仕事は,客から注文票を受け取ったらすぐに, いくつかの船舶を乗り継いで, 出発地と目的地を結ぶ航路の中で, もっとも安価な運賃を計算し,客に伝えることである.
ただし,旅程によっては,船舶を使って旅行することが出来ない場合もある. そのときは『船舶を使って旅行することが出来ない』と客に伝える必要がある. また,JOI国では,島と島の間を結ぶ新しい船舶が,次々と運航を開始しており, あなたには,その情報が随時伝えられる. 客に返事をする際には,最新の情報に留意しなければならない.

入力として,客の注文票や新たに運航を開始した船舶の運航情報が与えられたときに, 客への返事を求めるプログラムを作成せよ.

なお,入力例1と出力例1に対する実行状況を,図1として図示している.

入力

入力の 1 行目には2つの整数 n, k (1 ≦ n ≦ 100, 1 ≦ k ≦ 5000) が書かれている. これは,島の数が n 島で,入力が k + 1 行からなることを表す.
i + 1 行目 (1 ≦ i ≦ k) には, 3 個または 4 個の整数が空白を区切りとして書かれている.

  • 最初の数字が 0 のとき,この行は客の注文票を表す.
    この行には3個の整数 0, a, b (1 ≦ a ≦ n, 1 ≦ b ≦ n, a ≠ b) が空白を区切りとして書かれている.
    これは,客が,島 a を出発地とし島 b を目的地とするような注文票を送ってきたことを表す.
  • 最初の数字が 1 のとき,この行は新たに運航を開始した船舶の運航情報を表す.
    この行には 4 個の整数 1, c, d, e (1 ≦ c ≦ n, 1 ≦ d ≦ n, c ≠ d, 1 ≦ e ≦ 1000000) が書かれている.
    これは島 c と島 d を往復する船舶が新たに運航を開始し, この船舶の島 c から島 d への運賃と,島 d から島 c への運賃が,共に e であることを表す.
    この行以降の注文票に対しては,この船舶も考慮して返事をしなければならない.

最初の段階では,船舶は一隻も運航していないものとする. 入力のうち,船舶の運航情報を表す行は 1000 行以下である. また,島と島の間に,複数の船舶が運航することがあることに注意せよ.

出力

入力のうち,注文票を表す行の数を m とおく.
提出する出力ファイルは m 行からなり, i 行目(1 ≦ i ≦ m)には, i 番目の注文票に対する返事を表す整数を書く.
すなわち, i 番目の注文票の出発地と目的地の間を,いくつかの船舶を乗り継いで旅行することが可能ならば, その運賃の合計の最小値を書く. 旅行することが不可能ならば,-1 を書く.

入出力例

入力例1 入力例2
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
  
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
   
 
出力例1 出力例2
-1
15
12
   
5955
21431
9298
16392
24774
8840
   

なお,入力例1と出力例1の船舶が運航を開始していく模様と客からの注文票に対する返答を,図1として以下に図示している.

![](/images/problems/problem_23772_408680c615f1b947776f4316a63fb926.gif) | | | | | | | |-------------------------------|------------------------------|-------------------------------|------------------------------|-------------------------------|------------------------------| | | ![](/images/problems/problem_23772_b18e597e0adf40597cdaabb907e418ed.png) | ![](/images/problems/problem_23772_8822e9bb85360726256fdc7bf1675797.png) | ![](/images/problems/problem_23772_fcbfcc2ae427be649e14200925e6b008.png) | ![](/images/problems/problem_23772_8822e9bb85360726256fdc7bf1675797.png) | ![](/images/problems/problem_23772_168491acd03639e798977e3bc730d36f.png) | | ![](/images/problems/problem_23772_8822e9bb85360726256fdc7bf1675797.png) | ![](/images/problems/problem_23772_12a53a0c13cac15f9c23d47c1fcb6b75.png) | ![](/images/problems/problem_23772_8822e9bb85360726256fdc7bf1675797.png) | ![](/images/problems/problem_23772_26d5735364f7bc7bd859a7b732dcca55.png) | ![](/images/problems/problem_23772_8822e9bb85360726256fdc7bf1675797.png) | ![](/images/problems/problem_23772_7d2ef07de851cc7738f2c396ff2385cd.png) | | ![](/images/problems/problem_23772_8822e9bb85360726256fdc7bf1675797.png) | ![](/images/problems/problem_23772_8ed0238b582fcb114a82726f76746a94.png) | ![](/images/problems/problem_23772_8822e9bb85360726256fdc7bf1675797.png) | ![](/images/problems/problem_23772_7e49feca03baefb29160372da64b0324.png) | ![](/images/problems/problem_23772_8822e9bb85360726256fdc7bf1675797.png) | ![](/images/problems/problem_23772_4193a3e993a7557887a7eaa7d6860019.png) | 図1. 入力例1と出力例1に対するアニメーションと分解写真

※各入出力例のデータは, 右クリック等によりファイルに保存して利用可能です.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.