用c++解决下述问题:描述 After finishing the "Village Construction Project", the provincial government built more roads to make the trips between two villages more convenient. Then there is a new problem, which path is the shortest path between village V1 and V2? 输入 The first line of each test case contains four integers N, M, V1, V2 (1<=N, M, V1, V2<=1000; V1 != V2). Still, N and M represent the number of villages and the number of roads respectively. Then N lines follow, and every line contains three integers Ai, Bi, and Vi (1<=Ai, Bi <=N, Ai != Bi, 1<=Vi<=1000 ), meaning there is a road between village Ai and village Bi with its cost Vi. The inputs terminate at the end of the file (EOF). 输出 For each test case, output the cost of the shortest path between V1 and V2. If there is no path between V1 and V2, print -1. 输入样例 1
时间: 2024-04-27 21:24:10 浏览: 161
C++问题解决办法.pdf
由于没有给出具体的输入样例,我就以一个例子为参考,假设输入样例为:
5 6 1 5 // N=5, M=6, V1=1, V2=5
1 2 2
1 3 3
2 3 1
2 4 5
3 5 4
4 5 1
则表示有5个村庄,6条道路,需要求解从村庄1到村庄5的最短路径。
下面是用 C++ 解决该问题的代码:
阅读全文