CF1863E.Speedrun

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are playing a video game. The game has nn quests that need to be completed. However, the jj-th quest can only be completed at the beginning of hour hjh_j of a game day. The game day is kk hours long. The hours of each game day are numbered 0,1,…,k−10, 1, \ldots, k - 1. After the first day ends, a new one starts, and so on.

Also, there are dependencies between the quests, that is, for some pairs (ai,bi)(a_i, b_i) the bib_i-th quest can only be completed after the aia_i-th quest. It is guaranteed that there are no circular dependencies, as otherwise the game would be unbeatable and nobody would play it.

You are skilled enough to complete any number of quests in a negligible amount of time (i. e. you can complete any number of quests at the beginning of the same hour, even if there are dependencies between them). You want to complete all quests as fast as possible. To do this, you can complete the quests in any valid order. The completion time is equal to the difference between the time of completing the last quest and the time of completing the first quest in this order.

Find the least amount of time you need to complete the game.

你正在玩一款电子游戏。游戏中共有 nn 个任务需要完成。然而,第 jj 个任务只能在游戏日的第 hjh_j 小时初完成。每个游戏日共 kk 小时,每小时按 0,1,…,k−10, 1, \ldots, k - 1 编号。第一个游戏日结束后,下一个游戏日立即开始,依此类推。

此外,任务之间存在依赖关系:对某些数对 (ai,bi)(a_i, b_i),第 bib_i 个任务只能在第 aia_i 个任务完成之后才能完成。题目保证不存在环形依赖(否则游戏将无法通关,也就没人会玩了)。

你的操作足够熟练,可以在可忽略不计的时间内完成任意数量的任务(即:你可以在同一小时初完成任意多个任务,即使它们之间存在依赖关系)。你希望以最短时间完成所有任务。为此,你可以按任意满足依赖约束的顺序完成任务。完成时间定义为:最后一个任务的完成时刻与第一个任务的完成时刻之差。

求完成全部任务所需的最短时间。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤100 0001\le t\le 100\,000). The description of the test cases follows.

The first line of each test case contains three integers nn, mm, and kk (1≤n≤200 0001\le n\le 200\,000, 0≤m≤200 0000\le m\le 200\,000, 1≤k≤1091\le k\le 10^9) — the number of quests, the number of dependencies between them, and the number of hours in a game day, respectively.

The next line contains nn integers h1,h2,…,hnh_1, h_2, \ldots, h_n (0≤hi<k0\le h_i \lt k).

The next mm lines describe the dependencies. The ii-th of these lines contains two integers aia_i and bib_i (1≤ai<bi≤n1\le a_i \lt b_i\le n) meaning that quest bib_i can only be completed after quest aia_i. It is guaranteed that all dependencies are pairwise distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 200 000200\,000.

It is guaranteed that the sum of mm over all test cases does not exceed 200 000200\,000.

每个测试包含多个测试用例。第一行包含测试用例数量 tt(1≤t≤100 0001\le t\le 100\,000)。随后是各测试用例的描述。

每个测试用例的第一行包含三个整数 nn、mm 和 kk(1≤n≤200 0001\le n\le 200\,000,0≤m≤200 0000\le m\le 200\,000,1≤k≤1091\le k\le 10^9),分别表示任务数量、任务之间的依赖关系数量以及游戏一天中的小时数。

下一行包含 nn 个整数 h1,h2,…,hnh_1, h_2, \ldots, h_n(0≤hi<k0\le h_i \lt k)。

接下来的 mm 行描述依赖关系。其中第 ii 行包含两个整数 aia_i 和 bib_i(1≤ai<bi≤n1\le a_i \lt b_i\le n),表示任务 bib_i 只能在任务 aia_i 完成后才能完成。保证所有依赖关系两两不同。

保证所有测试用例中 nn 的总和不超过 200 000200\,000。

保证所有测试用例中 mm 的总和不超过 200 000200\,000。

输出格式

For each test case, output a single integer — the minimum completion time.

对于每个测试用例,输出一个整数——最小完成时间。

输入输出样例

  • 输入#1

    6
    4 4 24
    12 16 18 12
    1 2
    1 3
    2 4
    3 4
    4 3 10
    2 6 5 9
    1 4
    2 4
    3 4
    2 1 10
    5 5
    1 2
    5 0 1000
    8 800 555 35 35
    5 0 10
    3 2 5 4 7
    3 2 5
    4 3 2
    1 2
    2 3

    输出#1

    24
    7
    0
    480
    5
    8

说明/提示

In the first test case, quests 11 and 44 must be completed at the beginning of the 1212-th hour of the day, but they cannot be completed during the same hour, because you also need to complete quests 22 and 33 between them. You can do all this in 2424 hours, though. To do so, you start at 1212 hours of the first game day by completing the first quest. At 1616 hours you complete quest 22. At 1818 hours you complete quest 33. Finally at 1212 hours of the second day you can complete quest 44. The total time elapsed (from the moment you completed the first quest and the moment you completed the last) is 2424 hours.

In the third test case, you can complete the first quest and then complete the remaining quest right after. You start at 55 hours of the first day by completing the first quest. After this the second quest becomes available, you complete it as well. The total time elapsed is 00.

In the fourth test case, you can start with the third quest. You start at 555555 hours of the first day and you can finish at 3535 hours of the second day. The total time elapsed is 1035−555=4801035-555=480.

在第一个测试用例中,任务 11 和任务 44 必须在一天的第 1212 小时初完成,但它们不能在同一小时内完成,因为你还需要在二者之间完成任务 22 和任务 33。不过,你仍然可以在 2424 小时内完成所有这些任务。具体做法是:你在游戏第一天的 1212 小时开始,完成第一个任务;在 1616 小时完成任务 22;在 1818 小时完成任务 33;最后在第二天的 1212 小时完成任务 44。从完成第一个任务到完成最后一个任务所经过的总时间为 2424 小时。

在第三个测试用例中,你可以先完成第一个任务,紧接着立即完成剩余的任务。你于第一天的 55 小时开始并完成第一个任务;此后第二个任务立即解锁,你也随即完成它。总耗时为 00。

在第四个测试用例中,你可以从第三个任务开始。你于第一天的 555555 小时开始,并于第二天的 3535 小时结束。总耗时为 1035−555=4801035-555=480。

输入解题思路,AI测评打分。不知道怎么写?

首页