Shortest Path Query
Time limit2sMemory limit1024 MB
Given a DAG whose edges are black or white, answer queries for the shortest distance from vertex 1 to x when black edges cost a and white edges cost b.
- Level
Hard8 of 10
- Topics
- Shortest path, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
You are given a directed acyclic graph with vertices, labeled . The graph has edges, and each edge is either black or white. Every vertex is reachable from vertex .
You are given queries. In the -th query, you get three integers , , and . Give each black edge length and each white edge length . Find the length of the shortest path from vertex to vertex .
Input
The first line contains two integers and (, ), the number of vertices and edges.
Each of the next lines contains , , and , describing a directed edge from vertex to vertex (, , ). The edge is black if and white if .
The next line contains (), the number of queries. Each of the next lines contains , , and (, ).
Output
For each query, print one line containing the length of the shortest path from vertex to vertex .