#P10166. [DTCPC 2024] 环

    ID: 11529 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>贪心2024拓扑排序洛谷月赛

[DTCPC 2024] 环

Background

Cycle.

Problem Description

Given a directed graph GG with no multiple edges and no self-loops, and a sequence {an}\{a_n\}, each time you may add a directed edge i→ji\to j at a cost of ai+aja_i+a_j. Find the minimum total cost to make it possible to find k≥2k\geq 2 distinct vertices p1,p2,…,pkp_1,p_2,\dots,p_k such that for all i∈[1,k]i\in [1,k], there is an edge pi→pi mod k+1p_i\to p_{i\bmod k+1}.

Input Format

The first line contains two integers n,mn,m (2≤n≤5×1052\le n\le 5 \times 10^5, n−1≤m≤106n-1 \le m \le 10^6), denoting the number of vertices and edges in the graph.

The second line contains nn integers, denoting the sequence {an}\{a_n\} (1≤ai≤1091\le a_i\le 10^9).

The next mm lines each contain two integers u,vu,v (1≤u,v≤n1\le u,v\le n), meaning there is a directed edge u→vu\to v.

Output Format

Output one integer in a single line, denoting the minimum cost.

5 5
1 2 3 4 5 
1 2
2 3
3 4
1 5
5 4 
3

Hint

Translated by ChatGPT 5