编程介的小学生 2017-08-24 14:45 采纳率: 20.3%
浏览 663
已采纳

Delivery

Karl is an impatient guy. He always wants to find the fastest way to solve any problem. As a delivery man, it's a very good personality because he can make the work more efficient.

Now, Karl gets T tasks from his boss, which requires him to send the goods from cities to cities. There are N cities , numbered from 1 to N. And N-1 directional roads connect these cities. The ith road connects City i to City i+1 with a length Di. Moreover, there are M small Paths connect cities. They are also directional, and each of them has a length Qi. People can go through both roads and paths.

Karl wants to find the shortest way to complete every task. But he is afraid to walk on the small path. So, in every task, he can only pass through at most 1 small path. He gets a little faint about this question, so he asked for your help.

Input

There are few test cases. NOTICE that's no empty line between each test case. For each test case:
The first line contains two integers N , M(1 <= N <= 100000 , 1 <= M <= 200000), indicating there are N cities and M small paths.
The second line contains N-1 integers, indicating the length Di(1 <= Di <= 100000) of the ith directional road, which connects City i to City i+1.
The following M lines indicates M small paths. Each line contains 3 integers Ai , Bi , Qi, (1 <= Ai <= N , 1 <= Bi <= N , 1 <= Qi <= 100000)indicating this directional small path connects City Ai to City Bi with length Qi.
The next line contains a integer T(1 <= 200000), which means there are T tasks.
The following T lines indicates the T tasks. Each line contains 2 integers Ui(1 <= Ui <= N) , Vi(1 <= Vi <= N), indicating you are asked to find the shortest way from City Ui to City Vi with the requirements.
We promise that there is an answer to each Task.(There should be at least one way from City Ui to City Vi with requirements)
Output

For each test case, for every task, output a integer in a line which shows the shortest way for the query with requirements.
We promise that each answer will not exceed 2^31 - 1.

Sample Input

5 3
1 2 3 4
2 4 2
1 3 2
5 1 3
5
1 4
4 2
3 1
1 3
1 5
Sample Output

3
8
10
2
7

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)