logo AlgoBeat OnlineJudge
登录 注册

#208322. [USACO23JAN] Mana Collection P

内存限制:512 MiB 时间限制:5000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Note: The time limit for this problem is 5s, 2.5 times the default. The memory limit for this problem is 512MB, twice the default.

Bessie has recently taken an interest in magic and needs to collect mana for a very important spell. Bessie has mana pools, the ith of which accumulates mana per second . The pools are linked by a collection of directed edges , meaning that she can travel from to in seconds . Whenever Bessie is present at a pool, she can collect all the mana stored at that location, emptying it. At time , all mana pools are empty, and Bessie can select any pool to start at.

Answer queries, each specified by two integers and . For each query, determine the maximum amount of mana Bessie can collect in s seconds if she must be at mana pool at the end of the -th second.

输入格式

First line contains and .

Next line contains .

Next lines contain . No ordered pair appears more than once in the input.

Next line contains .

Next lines contain two integers and .

输出格式

lines, one for each query.

样例

样例输入 1

2 1
1 10
1 2 10
4
5 1
5 2
100 1
100 2

样例输出 1

5
50
100
1090

样例输入 2

4 8
50000000 100000000 20000000 70000000
1 2 20
2 1 50
2 3 90
1 3 40
3 1 10
4 1 25
1 4 5
4 3 70
3
8 3
1000000000 1
500000 4

样例输出 2

160000000
239999988050000000
119992550000000

数据范围与提示

Explanation for Sample 1

First query: Bessie takes mana from pool after seconds.

Second query: Bessie takes mana from pool after seconds.

Third query: Bessie takes mana from pool after seconds.

Fourth query: Bessie takes mana from pool after seconds and mana from pool after seconds.

Explanation for Sample 2

An example where Bessie is able to collect much larger amounts of mana.

Scoring

  • Inputs : ,
  • Inputs :
  • Inputs :
  • Inputs :
  • Inputs :
  • Inputs : No additional constraints.