Yet Another Shortest Path Query
시간 제한12초메모리 제한2048 MB
무방향 가중 평면 그래프와 여러 질의가 주어질 때 두 정점 사이의 간선 3개 이하 최단 경로 길이를 구하고, 없으면 -1을 출력한다.
문제
In graph theory, a planar graph is a graph that can be drawn on the plane in such a way that its edges intersect only at their endpoints. In other words, it can be drawn so that no edges cross each other. Such a drawing is called a plane graph or a planar embedding of the graph. A plane graph can be defined as a planar graph with a mapping from every node to a point on the plane, and from every edge to a plane curve on that plane, such that the extreme points of each curve are the points mapped from its end nodes, and all curves are disjoint except for their extreme points.
You will be given an undirected planar graph with vertices and edges. The vertices are labeled by . The -th edge connects vertices and , and its length is .
You will then be given queries. In the -th query, you will be given two integers and . Please write a program to figure out the length of the shortest path from vertex to vertex such that the path contains no more than three edges, or determine that there is no such path.
입력
The first line of the input contains two integers and (, ) denoting the number of vertices and the number of edges.
In the next lines, the -th line contains three integers , and (, , ) describing the -th edge. It is guaranteed that there is at most one edge between each pair of vertices. It is also guaranteed that the graph is planar.
The next line contains a single integer () denoting the number of queries.
In the next lines, the -th line contains two integers and (, ) describing the -th query.
출력
For each query, print a single line containing an integer denoting the length of the shortest path containing at most three edges. If there is no such path, please print "-1" instead.