luogu#P10768. 「CROI · R2」落月摇情
「CROI · R2」落月摇情
Background
He dreamed of flowers falling o'er the pool last night;
Alas! Spring has gone, he can’t homeward go.
The water bearing spring will run away in flight;
The moon over the pool will in the west sink low.
In the mist on the sea the slanting moon will hide.
It's a long way from northern hills to southern streams.
How many can go home by moonlight on the tide?
The setting moon sheds o'er riverside trees but dreams riverside trees.
Problem Description
Xiao Yan, a fairy living on the moon, maintains a magical tree by the river to communicate with the human world. Moonlight forms specific patterns through its branches on the river surface. To restore the tree after damage, Xiao Yan needs to reconnect its nodes with minimal cost. The tree is an undirected connected graph with nodes and edges, without multiple edges or self-loops.
The cost to generate an edge is calculated as , where represents the influence factor of node . Your task is to find a connected graph with edges that minimizes the total edge cost.
Formally: Given nodes with weights , construct a connected undirected graph with edges (no duplicates/self-loops) such that the sum of all edge weights () is minimized.
Input Format
First line: Two non-negative integers , .
Second line: space-separated integers .
Output Format
Output lines:
- First line: Total cost .
- Next lines: Pairs representing edges (unordered, no duplicates).
Any valid solution is accepted if conditions are met.
4 5
1 2 -2 -3
-13
1 2
1 3
1 4
2 3
2 4
6 5
1 2 4 5 0 -3
-36
1 6
2 6
3 6
4 6
5 6
Hint
【Special Judge】
Your output will be accepted if:
- The graph is connected with no duplicate/self-loop edges.
- The total cost matches the claimed (which must equal the standard answer).
【Data Range】
Constraints:
Subtasks (with dependencies):
| Subtask | | | Special Conditions | Points | Dependencies |
|---------|----------|----------|--------------------|--------|--------------|
| 1 | 7 | 21 | None | 10 | None |
| 2 | 16 | 120 | None | 15 | 1 |
| 3 | 1000 | 3e5 | None | 15 | 1,2 |
| 4 | 2e5 | 3e5 | | 15 | None |
| 5 | 2e5 | 3e5 | | 10 | None |
| 6 | 2e5 | 3e5 | None | 15 | 1,2,3 |
| 7 | 1e6 | 1e6 | None | 20 | 1,2,3,6 |
【Sample Explanations】
- Sample 1: Unique solution with total cost .

- Sample 2: Multiple valid solutions (e.g., edge can be replaced with ).
