[P2888] Preferably minimization of the longest side of the path

Meaning of the questions: There are many paths, each path has the longest edge x, x choose the smallest case as an answer

Code:

1.Floyd writing:

#include<bits/stdc++.h>
#define LL long long
#define maxn 302
#define inf 0x3f3f3f3f
using namespace std;
int mp[maxn][maxn];
int main(){
    int n,m,t;
    scanf("%d%d%d",&n,&m,&t);    
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        mp[i][j]=inf;
    mp[i][i]=0;
    }    
    int a,b,c;
    for(int i=1;i<=m;i++){
        cin>>a>>b>>c;
        mp[a][b]=c;
    }
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++){
        if(i!=j&&i!=k&&j!=k&&mp[i][k]!=inf&&mp[k][j]!=inf)
        mp[i][j]=min(mp[i][j],max(mp[i][k],mp[k][j]));
    }
    for(int i=1;i<=t;i++){
        cin>>a>>b;
        if(mp[a][b]==inf)
        cout<<"-1"<<endl;
        else
        cout<<mp[a][b]<<endl;
    }
}
View Code

2.SPFA writing (recording asked to avoid TLE)

 

Guess you like

Origin www.cnblogs.com/Aiahtwo/p/11519920.html