1)朴素Dijkstra算法
适用稠密图(m~n^2), 用邻接矩阵实现;
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 1000;
int g[N][N];
int dis[N];
bool st[N];
int Dijkstra(int n )
{
memset(dis, inf, sizeof(dis));
dis[1] = 0 ;
for(int i =1; i<=n; i++)
{
int t = - 1;
for(int j = 1; j<= n; j++)
if(!st[j] && (t==-1 || dis[j] < dis[t]))
t = j;
st[t] = true;
for(int j = 1; j<= n; j++)
dis[j] = min(dis[j], dis[t] + g[t][j]);
}
if(dis[n] == inf) return -1;
return dis[n];
}
int main()
{
memset(g, inf, sizeof(g));
i