非常经典的最短路问题,用djks轻松解决
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
int djks[110],a[110][110]={0};
bool b[110]={0};
memset(djks,0x3f,sizeof(djks));
memset(a,0x3f,sizeof(a));
for(auto x:times)
{
a[x[0]][x[1]]=min(a[x[0]][x[1]],x[2]);
}
djks[k]=0;
for(int i=1;i<=n;i++)
{
int minn=0x3f3f3f3f,k=-1;
for(int j=1;j<=n;j++)
{
if(!b[j]&&djks[j]<minn)
{
minn=djks[j];
k=j;
}
}
if(k==-1) break;
b[k]=1;
for(int j=1;j<=n;j++)
{
if(!b[j])
djks[j]=min(djks[j],djks[k]+a[k][j]);
}
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,djks[i]);
}
return ans==0x3f3f3f3f?-1:ans;
}
};