CF1253F.Cheap Robot

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You're given a simple, undirected, connected, weighted graph with nn nodes and mm edges.

Nodes are numbered from 11 to nn. There are exactly kk centrals (recharge points), which are nodes 1,2,…,k1, 2, \ldots, k.

We consider a robot moving into this graph, with a battery of capacity cc, not fixed by the constructor yet. At any time, the battery contains an integer amount xx of energy between 00 and cc inclusive.

Traversing an edge of weight wiw_i is possible only if x≥wix \ge w_i, and costs wiw_i energy points (x:=x−wix := x - w_i).

Moreover, when the robot reaches a central, its battery is entirely recharged (x:=cx := c).

You're given qq independent missions, the ii-th mission requires to move the robot from central aia_i to central bib_i.

For each mission, you should tell the minimum capacity required to acheive it.

给你一个简单、无向、连通、带权图,包含 nn 个节点和 mm 条边。

节点编号为 11 到 nn。图中恰好有 kk 个中心节点(即充电点),它们是节点 1,2,…,k1, 2, \ldots, k。

我们考虑一个在该图中移动的机器人,其电池容量为 cc(尚未由构造器确定)。在任意时刻,电池中所含能量为整数 xx,满足 0≤x≤c0 \le x \le c。

只有当当前能量 x≥wix \ge w_i 时,机器人才能遍历一条权重为 wiw_i 的边,且该操作消耗 wiw_i 单位能量(即 x:=x−wix := x - w_i)。

此外,当机器人到达某个中心节点时,其电池将被完全充满(即 x:=cx := c)。

你将收到 qq 个相互独立的任务;其中第 ii 个任务要求将机器人从中心节点 aia_i 移动到中心节点 bib_i。

对每个任务,请输出完成该任务所需的最小电池容量。

输入格式

The first line contains four integers nn, mm, kk and qq (2≤k≤n≤1052 \le k \le n \le 10^5 and 1≤m,q≤3⋅1051 \le m, q \le 3 \cdot 10^5).

The ii-th of the next mm lines contains three integers uiu_i, viv_i and wiw_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \neq v_i, 1≤wi≤1091 \le w_i \le 10^9), that mean that there's an edge between nodes uu and vv, with a weight wiw_i.

It is guaranteed that the given graph is simple (there is no self-loop, and there is at most one edge between every pair of nodes) and connected.

The ii-th of the next qq lines contains two integers aia_i and bib_i (1≤ai,bi≤k1 \le a_i, b_i \le k, ai≠bia_i \neq b_i).

第一行包含四个整数 nn、mm、kk 和 qq(满足 2≤k≤n≤1052 \le k \le n \le 10^5,且 1≤m,q≤3⋅1051 \le m, q \le 3 \cdot 10^5)。

接下来的 mm 行中,第 ii 行包含三个整数 uiu_i、viv_i 和 wiw_i(满足 1≤ui,vi≤n1 \le u_i, v_i \le n,ui≠viu_i \neq v_i,1≤wi≤1091 \le w_i \le 10^9),表示节点 uiu_i 与 viv_i 之间存在一条权重为 wiw_i 的边。

保证所给图是简单图(无自环,且任意两个节点之间至多只有一条边)且连通。

接下来的 qq 行中,第 ii 行包含两个整数 aia_i 和 bib_i(满足 1≤ai,bi≤k1 \le a_i, b_i \le k,且 ai≠bia_i \neq b_i)。

输出格式

You have to output qq lines, where the ii-th line contains a single integer : the minimum capacity required to acheive the ii-th mission.

你需要输出 qq 行,其中第 ii 行包含一个整数:完成第 ii 项任务所需的最小容量。

输入输出样例

  • 输入#1

    10 9 3 1
    10 9 11
    9 2 37
    2 4 4
    4 1 8
    1 5 2
    5 7 3
    7 3 2
    3 8 4
    8 6 13
    2 3

    输出#1

    12
  • 输入#2

    9 11 3 2
    1 3 99
    1 4 5
    4 5 3
    5 6 3
    6 4 11
    6 7 21
    7 2 6
    7 8 4
    8 9 3
    9 2 57
    9 3 2
    3 1
    2 3

    输出#2

    38
    15

说明/提示

In the first example, the graph is the chain 10−9−2C−4−1C−5−7−3C−8−610 - 9 - 2^C - 4 - 1^C - 5 - 7 - 3^C - 8 - 6, where centrals are nodes 11, 22 and 33.

For the mission (2,3)(2, 3), there is only one simple path possible. Here is a simulation of this mission when the capacity is 1212.

  • The robot begins on the node 22, with c=12c = 12 energy points.
  • The robot uses an edge of weight 44.
  • The robot reaches the node 44, with 12−4=812 - 4 = 8 energy points.
  • The robot uses an edge of weight 88.
  • The robot reaches the node 11 with 8−8=08 - 8 = 0 energy points.
  • The robot is on a central, so its battery is recharged. He has now c=12c = 12 energy points.
  • The robot uses an edge of weight 22.
  • The robot is on the node 55, with 12−2=1012 - 2 = 10 energy points.
  • The robot uses an edge of weight 33.
  • The robot is on the node 77, with 10−3=710 - 3 = 7 energy points.
  • The robot uses an edge of weight 22.
  • The robot is on the node 33, with 7−2=57 - 2 = 5 energy points.
  • The robot is on a central, so its battery is recharged. He has now c=12c = 12 energy points.
  • End of the simulation.

Note that if value of cc was lower than 1212, we would have less than 88 energy points on node 44, and we would be unable to use the edge 4↔14 \leftrightarrow 1 of weight 88. Hence 1212 is the minimum capacity required to acheive the mission.

—

The graph of the second example is described here (centrals are red nodes):

The robot can acheive the mission (3,1)(3, 1) with a battery of capacity c=38c = 38, using the path 3→9→8→7→2→7→6→5→4→13 \rightarrow 9 \rightarrow 8 \rightarrow 7 \rightarrow 2 \rightarrow 7 \rightarrow 6 \rightarrow 5 \rightarrow 4 \rightarrow 1

The robot can acheive the mission (2,3)(2, 3) with a battery of capacity c=15c = 15, using the path 2→7→8→9→32 \rightarrow 7 \rightarrow 8 \rightarrow 9 \rightarrow 3

在第一个例子中,图是一条链:10−9−2C−4−1C−5−7−3C−8−610 - 9 - 2^C - 4 - 1^C - 5 - 7 - 3^C - 8 - 6,其中中心节点(centrals)为节点 11、22 和 33。

对于任务 (2,3)(2, 3),仅存在一条简单路径。以下是当电池容量为 1212 时该任务的模拟过程:

  • 机器人起始于节点 22,初始能量 c=12c = 12;
  • 机器人经过一条权值为 44 的边;
  • 机器人到达节点 44,剩余能量为 12−4=812 - 4 = 8;
  • 机器人经过一条权值为 88 的边;
  • 机器人到达节点 11,剩余能量为 8−8=08 - 8 = 0;
  • 机器人位于一个中心节点,因此电池被充满,当前能量变为 c=12c = 12;
  • 机器人经过一条权值为 22 的边;
  • 机器人到达节点 55,剩余能量为 12−2=1012 - 2 = 10;
  • 机器人经过一条权值为 33 的边;
  • 机器人到达节点 77,剩余能量为 10−3=710 - 3 = 7;
  • 机器人经过一条权值为 22 的边;
  • 机器人到达节点 33,剩余能量为 7−2=57 - 2 = 5;
  • 机器人位于一个中心节点,因此电池被充满,当前能量变为 c=12c = 12;
  • 模拟结束。

注意:若 cc 的值小于 1212,则机器人在节点 44 处的能量将少于 88,从而无法使用权值为 88 的边 4↔14 \leftrightarrow 1。因此,1212 是完成该任务所需的最小容量。

—

第二个例子的图结构如下所示(中心节点为红色节点):

机器人可使用容量为 c=38c = 38 的电池,沿路径 3→9→8→7→2→7→6→5→4→13 \rightarrow 9 \rightarrow 8 \rightarrow 7 \rightarrow 2 \rightarrow 7 \rightarrow 6 \rightarrow 5 \rightarrow 4 \rightarrow 1 完成任务 (3,1)(3, 1);
机器人可使用容量为 c=15c = 15 的电池,沿路径 2→7→8→9→32 \rightarrow 7 \rightarrow 8 \rightarrow 9 \rightarrow 3 完成任务 (2,3)(2, 3)。

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

首页