1 && 3 Graph
Time limit4sMemory limit1024 MB
Answer many shortest-path queries on a special connected graph where fewer than 3 vertices have degree at least 3, exploiting its path/cycle-like structure for efficiency.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Implementation
- Solved
- No attempts yet
Problem
Sehun and Chanwoo have solved many graph problems and developed their own ideas about what makes a good one.
- Sehun thinks the input graph should be ordinary enough that it needs no special-case handling. In this problem, an ordinary graph is an undirected simple connected graph: there are no duplicate edges, every vertex is connected, and every edge joins two distinct vertices.
- Chanwoo thinks a graph becomes messy when it has too many high-degree vertices. More precisely, the number of vertices whose degree is at least must be less than .
A graph satisfying both conditions is called a 1 && 3 graph. You are given a 1 && 3 graph with vertices and edges. Write a program that processes queries asking for the shortest distance between two vertices.
Input
The first line contains three integers , , and : the number of vertices, the number of edges, and the number of queries.
Each of the next lines contains three integers , , and , meaning that there is an edge of weight between vertices and .
Each of the next lines contains two integers and . This query asks for the shortest distance between vertices and .
All input values are integers, and the given graph is a 1 && 3 graph.
Output
Print lines. For each query, print its answer on its own line, in the same order as the input.