迪杰斯特拉简单版
#include <bits/stdc++.h>
using namespace std;
int m,n;
const int inf = 0x3f3f3f3f;
int dis[1005];
int gra[405][405];
int vis[1005];
void dj(int s, int t)
{
memset(vis,0,sizeof(vis));
int minn, v;
for(int i = 0; i < n; i ++)
dis[i] = gra[s][i];
for(int i = 1; i <= n; i ++)
{
minn = inf;
for(int j = 0; j < n; j ++)
{
if(!vis[j] && dis[j] < minn)
{
v = j;
minn = dis[j];
}
}
vis[v] = 1;
for(int j = 0; j < n; j ++)
{
if(gra[v][j] + dis[v] < dis[j] && !vis[j])
{
dis[j] = gra[v][j] + dis[v];
}
}
}
if(dis[t] == inf)printf("-1
");
else printf("%d
",dis[t]);
}
int main()
{
int i, j, a, b, c, s, t;
while(~scanf("%d%d", &n, &m))
{
for(i = 0; i < n; i ++)
{
for(j = 0; j < n; j ++)
{
if(i == j)
gra[i][j] = 0;
else
gra[i][j] = gra[j][i] = inf;
}
}
for(i = 1; i <= m; i ++)
{
scanf("%d%d%d", &a, &b, &c);
if(gra[a][b] > c)
gra[a][b] = gra[b][a] = c;
}
scanf("%d %d",&s,&t);
dj(s,t);
}
return 0;
}