单源最短路 Dijkstra O(mlogn) (类实现)
#include <iostream>
#include <vector>
#include <map>

#define maxn 1010
using namespace std;
typedef double weight;

class graph_c


{
public:
void init(int _n);
void dijkstra(int S);
void add_edge(int u, int v, weight w);
private:
int n;
vector <int> r[maxn];
vector <weight> e[maxn];
weight dist[maxn];
int pa[maxn];
multimap <weight, int> h;
};

void graph_c::init(int _n)


{
n = _n;
for (int i = 0; i < n; i++)

{
r[i].clear();
e[i].clear();
}
}

void graph_c::add_edge(int u, int v, weight w)


{
r[u].push_back(v);
e[u].push_back(w);
}

void graph_c::dijkstra(int S)


{
weight d, tmp;
int v;
multimap<weight, int>::iterator it;
h.clear();
for (int i = 0; i < n; i++) dist[i] = -1;
dist[S] = 0;
pa[S] = -1;
h.insert(multimap<weight, int>::value_type(0, S));
while (!h.empty())

{
it = h.begin();
v = it->second;
d = it->first;
h.erase(it);
for (int i = 0; i < r[v].size(); i++)

{
tmp = d + e[v][i];
int j = r[v][i];
if (dist[j] < 0 || tmp < dist[j])

{
dist[j] = tmp;
pa[j] = v;
h.insert(multimap<weight, int>::value_type(tmp, j));
}
}
}
}