算法提高之信使
-
核心思想:单源最短路
- 因为数据范围很小 可以考虑floyd算法(三重循环)
-
#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 110,INF = 0x3f3f3f3f;int d[N][N];int n,m;int main(){cin>>n>>m;memset(d,0x3f,sizeof d);for (int i = 1; i <= n; i ++ ) d[i][i] = 0;for(int i=0;i<m;i++){int a,b,c;cin>>a>>b>>c;d[a][b] = d[b][a] = min(d[a][b],c); //重边留最小的}for (int k = 1; k <= n; k ++ )for (int i = 1; i <= n; i ++ )for (int j = 1; j <= n; j ++ )d[i][j] = min(d[i][j],d[i][k] + d[k][j]);int res=0;for(int i=2;i<=n;i++)if (d[1][i] == INF) //无解{res = -1;break;}else res = max(res,d[1][i]);cout<<res<<endl;}