Floyd算法适用于多源的情况,用dis[st][en]就可以轻松的查询st到en的最短长度。
具体原理我也不太懂,记住模板就行
参考链接:https://blog.csdn.net/qq_35644234/article/details/60875818
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int N=1005;
struct date
{
int from, to, dic;
};
date ee[N*100];
int n, m;
int st, en;
int map[N][N];
int dis[N][N], path[N][N];//dis[i][j]表示i和j之间的最短距离,path[i][j]表示i是经过了哪个点找到j的
void flayd()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
dis[i][j]=map[i][j];
path[i][j]=j;
}
}
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(dis[i][j]>dis[i][k]+dis[k][j])//换成<就是最大距离
{
dis[i][j]=dis[i][k]+dis[k][j];
path[i][j]=path[i][k];
}
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
st=1, en=n;//要查询的两个点
int from, to, dic;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
map[i][j] = i==j ? 0:INF;
for(int i=0; i<m; i++)
{
scanf("%d%d%d", &from, &to, &dic);
if(dic<map[from][to])
map[from][to]=dic;//改为map[from][to]=map[to][from]=dic;就是无向图
}
flayd();
printf("%d\n", dis[st][en]);//输出两点的距离
}
return 0;
}