CF1578L.Labyrinth

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

In a dream, Lucy found herself in a labyrinth. This labyrinth consists of nn rooms, connected by mm passages (ii-th passage is wiw_i cm wide). Each passage can be traversed in both directions. It is guaranteed that it is possible to get from any room to any other room. But this is not an ordinary labyrinth — each room in this labyrinth contains a magic candy. When Lucy eats this magic candy, she is getting wider. Specifically, if she eats candy from room ii she becomes wider by cic_i cm. Note that she is not obliged to eat candy the first time she visits a particular room, but she can eat each candy only once.

Unfortunately, passages in this labyrinth are pretty narrow, so after eating some candy, Lucy can get too wide and will not be able to traverse them — her width should not be greater than the width of the corresponding passage.

Lucy starts her journey in a room number 11. She wants to eat all the candies. After that, she will just wake up, so she does not have to be able to return to the room 11. She realizes that with her current width, she may not be able to do so, so she plans a workout before embarking on her journey. Lucy wants to know if it is possible to start with some positive width and still eat all the candies. If yes, then what is the maximal starting width with which it is possible.

在梦中,露西发现自己置身于一座迷宫之中。这座迷宫由 nn 个房间组成,房间之间通过 mm 条通道相连(第 ii 条通道宽 wiw_i 厘米)。每条通道均可双向通行。保证任意两个房间之间均可达。但这并非一座普通迷宫——每个房间中都有一颗魔法糖果。当露西吃下某颗魔法糖果时,她的身体会变宽。具体而言,若她吃下第 ii 个房间中的糖果,则她的宽度将增加 cic_i 厘米。注意:她首次访问某个房间时并不一定要立即吃掉其中的糖果,但每颗糖果只能被吃一次。

不幸的是,这座迷宫中的通道十分狭窄;因此,在吃下若干糖果后,露西可能变得过宽,从而无法再通过某些通道——她的当前宽度不得超过所通行通道的宽度。

露西从编号为 11 的房间出发。她希望吃掉所有糖果。之后她便会醒来,因此无需返回房间 11。她意识到,以自己当前的初始宽度,可能无法完成这一目标,因此她计划在启程前进行一番训练。露西想知道:是否存在某个正数初始宽度,使得她能够吃掉所有糖果?如果存在,那么满足条件的最大初始宽度是多少?

输入格式

The first line contains two integers, nn and mm (2≤n≤105;n−1≤m≤1052 \le n \le 10^5; n - 1 \le m \le 10^5) — the number of rooms and the number of passages.

The second line contains nn integers — cic_i (1≤ci≤1091 \le c_i \le 10^9).

Next mm lines contain three integers each — aia_i, bib_i and wiw_i (1≤ai,bi≤n;ai≠bi;1≤wi≤1091 \le a_i, b_i \le n; a_i \ne b_i; 1 \le w_i \le 10^9) describing passage that connects rooms aia_i and bib_i and is wiw_i cm wide. It is guaranteed that the resulting labyrinth is connected and there is at most one passage between any pair of rooms.

第一行包含两个整数 nn 和 mm(2≤n≤1052 \le n \le 10^5;n−1≤m≤105n - 1 \le m \le 10^5)—— 分别表示房间数量和通道数量。

第二行包含 nn 个整数 —— cic_i(1≤ci≤1091 \le c_i \le 10^9)。

接下来的 mm 行每行包含三个整数 —— aia_i、bib_i 和 wiw_i(1≤ai,bi≤n1 \le a_i, b_i \le n;ai≠bia_i \ne b_i;1≤wi≤1091 \le w_i \le 10^9),描述一条连接房间 aia_i 和 bib_i、宽度为 wiw_i 厘米的通道。保证所构成的迷宫是连通的,且任意两个房间之间至多只有一条通道。

输出格式

If it is possible to eat all the candies, output the maximal possible starting width, otherwise output −1-1.

如果可以吃完所有糖果,则输出最大的可能起始宽度;否则输出 −1-1。

输入输出样例

  • 输入#1

    3 3
    1 2 3
    1 2 4
    1 3 4
    2 3 6

    输出#1

    3
  • 输入#2

    2 1
    1 1
    1 2 1

    输出#2

    -1

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

首页