#abc328e. E - Modulo MST
E - Modulo MST
Score : points
问题描述
给定一个带权无向简单连通图,包含 个顶点和 条边,其中顶点编号从 到 ,边编号从 到 。另外还给出一个正整数 。
边 连接顶点 和 ,具有权重 。
对于该图的一棵生成树 ,定义其成本为树中所有边的权重之和,对 取模后的结果。请找出该图中生成树的最小成本。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a weighted simple connected undirected graph with vertices and edges, where vertices are numbered to , and edges are numbered to . Additionally, a positive integer is given.
Edge connects vertices and and has a weight of .
For a spanning tree of this graph, the cost of is defined as the sum, modulo , of the weights of the edges in . Find the minimum cost of a spanning tree of this graph.
Constraints
- The given graph is simple and connected.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
5 6 328
1 2 99
1 3 102
2 3 86
2 4 94
2 5 95
3 4 81
Sample Output 1
33
The given graph is shown below:
The cost of the spanning tree containing edges is . The cost of every spanning tree of this graph is at least , so print .
Sample Input 2
6 5 998244353
1 2 337361568
1 6 450343304
2 3 61477244
2 5 745383438
4 5 727360840
Sample Output 2
325437688
Print the cost of the only spanning tree of this graph, which is .
Sample Input 3
8 28 936294041850197
1 2 473294720906780
1 3 743030800139244
1 4 709363019414774
1 5 383643612490312
1 6 557102781022861
1 7 623179288538138
1 8 739618599410809
2 3 857687812294404
2 4 893923168139714
2 5 581822471860662
2 6 740549363586558
2 7 307226438833222
2 8 447399029952998
3 4 636318083622768
3 5 44548707643622
3 6 307262781240755
3 7 12070267388230
3 8 700247263184082
4 5 560567890325333
4 6 704726113717147
4 7 588263818615687
4 8 549007536393172
5 6 779230871080408
5 7 825982583786498
5 8 713928998174272
6 7 751331074538826
6 8 449873635430228
7 8 11298381761479
Sample Output 3
11360716373
Note that the input and the answer may not fit into a integer.
update @ 2024/3/10 01:53:01