思路:
模板题,一遍Dijskra,选择最短路中的最长路即为答案。
class Solution {
public:
#define inf 0x3f3f3f3f
int vis[100005];
struct node
{
int to;
int len;
node(int x,int y)
{
to=x;len=y;
}
bool operator < (const node& x)const
{
return x.len<len;
}
};
vector<node> ve[100005];
priority_queue<node>que;
int dis[100005];
int st;
int dij(int n)
{
for(int i=1;i<=n;i++)dis[i]=inf;
dis[st]=0;
vis[st]=1;
que.push(node(st,0));
while(!que.empty())
{
node p=que.top();
vis[p.to]=1;
que.pop();
for(int i=0;i<ve[p.to].size();i++)
{
if(!vis[ve[p.to][i].to]&&dis[p.to]+ve[p.to][i].len<dis[ve[p.to][i].to])
{
dis[ve[p.to][i].to]=dis[p.to]+ve[p.to][i].len;
que.push(node(ve[p.to][i].to,dis[ve[p.to][i].to]));
}
}
}
int maxn=0;
for(int i=1;i<=n;i++)
{
if(dis[i]==inf)return -1;
maxn=max(dis[i],maxn);
}
return maxn;
}
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
for(int i=0;i<times.size();i++)
{
ve[times[i][0]].push_back(node(times[i][1],times[i][2]));
}
st=k;
return dij(n);
}
};