#abc338f. F - Negative Traveling Salesman
F - Negative Traveling Salesman
Score: points
问题描述
存在一个带有权重的简单有向图,包含 个顶点和 条边。顶点编号为 到 ,其中第 条边的权重为 ,从顶点 指向顶点 。权重可以为负数,但图中不包含负权环。
判断是否存在一条至少访问每个顶点一次的路径。如果存在这样的路径,请找出所经过边的最小总权重。若同一条边被多次经过,则该边的权重每次经过都会被累加。
这里,“至少访问每个顶点一次的路径”是指满足以下两个条件的一系列顶点 :
- 对于所有 ,存在一条从顶点 指向顶点 的边。
- 对于所有 ,存在某个 ,使得 。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a weighted simple directed graph with vertices and edges. The vertices are numbered to , and the -th edge has a weight of and extends from vertex to vertex . The weights can be negative, but the graph does not contain negative cycles.
Determine whether there is a walk that visits each vertex at least once. If such a walk exists, find the minimum total weight of the edges traversed. If the same edge is traversed multiple times, the weight of that edge is added for each traversal.
Here, "a walk that visits each vertex at least once" is a sequence of vertices that satisfies both of the following conditions:
- For every , there is an edge extending from vertex to vertex .
- For every , there is such that .
Constraints
- for
- The given graph does not contain negative cycles.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If there is a walk that visits each vertex at least once, print the minimum total weight of the edges traversed. Otherwise, print No
.
Sample Input 1
3 4
1 2 5
2 1 -3
2 3 -4
3 1 100
Sample Output 1
-2
By following the vertices in the order , you can visit all vertices at least once, and the total weight of the edges traversed is . This is the minimum.
Sample Input 2
3 2
1 2 0
2 1 0
Sample Output 2
No
There is no walk that visits all vertices at least once.
Sample Input 3
5 9
1 2 -246288
4 5 -222742
3 1 246288
3 4 947824
5 2 -178721
4 3 -947824
5 4 756570
2 5 707902
5 1 36781
Sample Output 3
-449429
update @ 2024/3/10 01:30:07
相关
在以下作业中: