CF1487E.Cheap Dinner

普及+/提高

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.

There are n1n_1 different types of first courses Ivan can buy (the ii-th of them costs aia_i coins), n2n_2 different types of second courses (the ii-th of them costs bib_i coins), n3n_3 different types of drinks (the ii-th of them costs cic_i coins) and n4n_4 different types of desserts (the ii-th of them costs did_i coins).

Some dishes don't go well with each other. There are m1m_1 pairs of first courses and second courses that don't go well with each other, m2m_2 pairs of second courses and drinks, and m3m_3 pairs of drinks and desserts that don't go well with each other.

Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!

伊万想吃一顿丰盛的晚餐。一顿丰盛的晚餐应包含一道头盘、一道主菜、一杯饮品和一份甜点。

可供选择的头盘有 n1n_1 种(第 ii 种价格为 aia_i 枚金币),主菜有 n2n_2 种(第 ii 种价格为 bib_i 枚金币),饮品有 n3n_3 种(第 ii 种价格为 cic_i 枚金币),甜点有 n4n_4 种(第 ii 种价格为 did_i 枚金币)。

某些菜品之间并不搭配。存在 m1m_1 对不搭配的头盘与主菜组合,m2m_2 对不搭配的主菜与饮品组合,以及 m3m_3 对不搭配的饮品与甜点组合。

伊万希望恰好选购一道头盘、一道主菜、一杯饮品和一份甜点,且所选四者彼此搭配,同时使整顿晚餐的总花费最低。请帮他找出最便宜的晚餐方案!

输入格式

The first line contains four integers n1n_1, n2n_2, n3n_3 and n4n_4 (1≤ni≤1500001 \le n_i \le 150000) — the number of types of first courses, second courses, drinks and desserts, respectively.

Then four lines follow. The first line contains n1n_1 integers a1,a2,…,an1a_1, a_2, \dots, a_{n_1} (1≤ai≤1081 \le a_i \le 10^8), where aia_i is the cost of the ii-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way (1≤bi,ci,di≤1081 \le b_i, c_i, d_i \le 10^8).

The next line contains one integer m1m_1 (0≤m1≤2000000 \le m_1 \le 200000) — the number of pairs of first and second courses that don't go well with each other. Each of the next m1m_1 lines contains two integers xix_i and yiy_i (1≤xi≤n11 \le x_i \le n_1; 1≤yi≤n21 \le y_i \le n_2) denoting that the first course number xix_i doesn't go well with the second course number yiy_i. All these pairs are different.

The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other (0≤m2,m3≤2000000 \le m_2, m_3 \le 200000).

第一行包含四个整数 n1n_1、n2n_2、n3n_3 和 n4n_4(1≤ni≤1500001 \le n_i \le 150000),分别表示头盘、主菜、饮品和甜点的种类数量。

接下来是四行。第一行包含 n1n_1 个整数 a1,a2,…,an1a_1, a_2, \dots, a_{n_1}(1≤ai≤1081 \le a_i \le 10^8),其中 aia_i 表示第 ii 种头盘的价格。随后三行以相同方式分别给出主菜、饮品和甜点的价格(1≤bi,ci,di≤1081 \le b_i, c_i, d_i \le 10^8)。

接下来一行包含一个整数 m1m_1(0≤m1≤2000000 \le m_1 \le 200000),表示彼此不搭的头盘与主菜组合对数。随后 m1m_1 行中,每行包含两个整数 xix_i 和 yiy_i(1≤xi≤n11 \le x_i \le n_1;1≤yi≤n21 \le y_i \le n_2),表示编号为 xix_i 的头盘与编号为 yiy_i 的主菜不搭。所有这些组合对互不相同。

彼此不搭的主菜与饮品组合对以相同格式给出。同样地,彼此不搭的饮品与甜点组合对也以相同格式给出(0≤m2,m3≤2000000 \le m_2, m_3 \le 200000)。

输出格式

If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print −1-1. Otherwise, print one integer — the minimum total cost of the dinner.

如果无法选择一道主菜、一道副菜、一种饮品和一份甜点,使得它们彼此搭配得当,则输出 −1-1;否则,输出一个整数——晚餐的最低总费用。

输入输出样例

  • 输入#1

    4 3 2 1
    1 2 3 4
    5 6 7
    8 9
    10
    2
    1 2
    1 1
    2
    3 1
    3 2
    1
    1 1

    输出#1

    26
  • 输入#2

    1 1 1 1
    1
    1
    1
    1
    1
    1 1
    0
    0

    输出#2

    -1

说明/提示

The best option in the first example is to take the first course 22, the second course 11, the drink 22 and the dessert 11.

In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.

第一个样例中的最优选择是:第一道菜选 22,第二道菜选 11,饮料选 22,甜点选 11。

第二个样例中,唯一的一对第一道菜与第二道菜的组合是不合法的,因此无法完成晚餐。

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

首页