PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000089. Thiết kế đồ thị và truy vấn đường ngắn (Design Graph With Shortest Path Calculator)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Thiết kế đồ thị và truy vấn đường ngắn (Design Graph With Shortest Path Calculator)
Design Graph With Shortest Path Calculator
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
A directed positive-weighted graph with 0-based vertices supports sequential operations 1 u v w (add a directed edge) and 2 s t (query current shortest distance, or -1 when unreachable). Parallel edges may be added.
Input
First , then directed edges q$ operations of type 1 or 2; vertices are 0-based.
Output
Print a distance or -1 for every type-2 operation.
Subtasks
-
Subtask 1 (40 points): . , , ; các cung có hướng.
-
Subtask 2 (60 points): . , , ; các cung có hướng.
Examples
Input
3 2
0 1 2
1 2 3
4
2 0 2
1 0 2 1
2 0 2
2 2 0
Output
5
1
-1
Explanation
The first 0→2 query costs 5 via vertex 1. Adding a direct edge of cost 1 improves the answer to 1; vertex 2 still cannot reach vertex 0.