单源最短路径问题求解
带权有向图(邻接表表示法)
完整源码
#include <iostream>
#include <vector>
#include <tuple>
#include <map>
using namespace std;
map<int , vector<tuple<int, int, double>>> EWD;
int main()
{
int V, E;
cin >> V >> E;
for(int i = 0 ; i < E ;i++)
{
int v, w;
double weight;
cin >> v >> w >> weight;
EWD[v].push_back(make_tuple(v, w, weight));
}
cout << "EdgeWeightedDigraph : " << endl;
for(int v = 0; v < V; v++)
{
cout << v << " : ";
for(vector<tuple<int, int, double>>::iterator ii = EWD[v].begin(); ii != EWD[v].end(); ii++)
cout << get<0>(*ii) << "->" << get<1>(*ii) << " " << get<2>(*ii) << " ";
cout << endl;
}
system("pause");
}
测试用例
测试数据来源见引用[1]
文件结构:
V
E
v w weight
数据内容:
8
15
4 5 0.35
5 4 0.35
4 7 0.37
5 7 0.28
7 5 0.28
5 1 0.32
0 4 0.38
0 2 0.26
7 3 0.39
1 3 0.29
2 7 0.34
6 2 0.40
3 6 0.52
6 0 0.58
6 4 0.93
运行结果
![带权有向图](https://img-blog.csdn.net/20161228200841063?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29vazJlYXQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
EdgeWeightedDigraph :
0 : 0->4 0.38 0->2 0.26
1 : 1->3 0.29
2 : 2->7 0.34
3 : 3->6 0.52