今天写了Dijkstra单源最短路径的代码,贴出来... #include "stdafx.h" #include <limits> #include <iostream> #include <fstream> using namespace std; const int MAXINT = numeric_limits<int>::max(); template <class Type> void Dijkstra(int n, int v, Type dist[], int prev[], Type** c) { bool *s = new bool[n+1]; int i, j; for(i = 1; i <=n; i++) { dist[i] = c[v][i]; if(c[v][i]!=MAXINT) prev[i] = v; else prev[i] =0; s[i] = false; } s[v] = true; dist[v] = 0; prev[v] = 0; for(i = 1; i< n; i++) { int u = v; int temp = MAXINT; for( j = 1; j<=n; j++) if(!s[j]&&dist[j]<temp) { u = j; temp = dist[j]; } s[u] = true; for(j=1; j<=n; j++) { if((!s[j])&&