Floyd(弗洛伊德)
时间复杂度O(n^3)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define pll pair<ll,ll>
const ll mod=1e9+7;
const ll N=1e4+9;
ll a[N][N];
ll n,m;
void floyd()
{
for(ll k=1;k<=n;k++)
for(ll i=1;i<=n;i++)
for(ll j=1;j<=n;j++)
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
//从i到j的距离 与 从i到k再从k到j的距离 比较更新
}
void f0(ll T)
{
cin>>n>>m;
for(ll i=1;i<=n;i++)
for(ll j=1;j<=n;j++)
if(i==j)
a[i][j]=0;
else
a[i][j]=1e18;
for(ll i=1;i<=m;i++)
{
ll x,y,z;
cin>>x>>y>>z;
a[x][y]=a[y][x]=min(a[x][y],z);
}
floyd();
cout<<a[1][n];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll T=1;
// cin>>T;
for(ll i=1;i<=T;i++)
f0(i);
return 0;
}
标签:弗洛伊德,const,ll,long,Floyd,define
From: https://www.cnblogs.com/2021sgy/p/16563145.html