原题链接:Leetcode 743. 网络延迟时间
DFS
class Solution {
public:
vector<vector<pair<int,int>>> adj;
vector<int> visit;
void dfs(int now,int w)
{
if(visit[now]>w)
{
visit[now]=w;
for(auto x:adj[now])
{
int a=x.first,b=x.second;
dfs(a,w+b);
}
}
}
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
adj.resize(n+1);
visit.resize(n+1,INT_MAX);
for(auto x:times)
{
int a=x[0],b=x[1],w=x[2];
adj[a].push_back({b,w});
}
dfs(k,0);
int res=0;
for(int i=1;i<=n;i++)
{
if(visit[i]==INT_MAX) return -1;
else res=max(res,visit[i]);
}
return res;
}
};
BFS
class Solution {
public:
vector<vector<pair<int,int>>> adj;
vector<int> time;
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
adj.resize(n+1);
time.resize(n+1,INT_MAX);
for(auto x:times)
{
int a=x[0],b=x[1],w=x[2];
adj[a].push_back({b,w});
}
queue<int> q;
q.push(k);
time[k]=0;
while(!q.empty())
{
auto tmp=q.front();
q.pop();
for(auto x:adj[tmp])
{
int t=time[tmp]+x.second;
if(t<time[x.first])
{
time[x.first]=t;
q.push(x.first);
}
}
}
int res=0;
for(int i=1;i<=n;i++)
{
if(time[i]==INT_MAX) return -1;
else res=max(res,time[i]);
}
return res;
}
};
Dijkstra
class Solution {
public:
vector<vector<pair<int,int>>> adj;
vector<int> d;
vector<int> visit;
void dijkstra(int t,int n)
{
d[t]=0;
for(int i=1;i<=n;i++)
{
int MIN=INT_MAX,u=-1;
for(int j=1;j<=n;j++)
{
if(!visit[j] && d[j]<MIN)
{
MIN=d[j]; u=j;
}
}
if(u==-1) return ;
visit[u]=1;
for(auto x:adj[u])
{
int a=x.first,b=x.second;
if(!visit[a] && d[u]+b<d[a]) d[a]=d[u]+b;
}
}
}
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
adj.resize(n+1);
visit.resize(n+1);
d.resize(n+1,INT_MAX);
for(auto x:times)
{
int a=x[0],b=x[1],w=x[2];
adj[a].push_back({b,w});
}
int res=0;
dijkstra(k,n);
for(int i=1;i<=n;i++)
{
if(d[i]==INT_MAX) return -1;
else if(i!=k) res=max(res,d[i]);
}
return res;
}
};
Dijkstra+优先队列 优先队列piar按second元素排序
class Solution {
public:
struct cmp{
bool operator() (const pair<int,int> p1,pair<int,int> p2)
{
return p1.second>p2.second;
}
};
vector<vector<pair<int,int>>> adj;
vector<int> d;
vector<int> visit;
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> pq;
void dijkstra(int t)
{
d[t]=0;
pq.push({t,0});
while(!pq.empty())
{
auto tmp=pq.top();
pq.pop();
int u=tmp.first,w=tmp.second;
if(visit[u]) continue;
visit[u]=1;
for(auto x:adj[u])
{
int a=x.first,b=x.second;
if(!visit[a] && w+b<d[a])
{
d[a]=w+b;
pq.push({a,d[a]});
}
}
}
}
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
adj.resize(n+1);
visit.resize(n+1);
d.resize(n+1,INT_MAX);
for(auto x:times)
{
int a=x[0],b=x[1],w=x[2];
adj[a].push_back({b,w});
}
int res=0;
dijkstra(k);
for(int i=1;i<=n;i++)
{
if(d[i]==INT_MAX) return -1;
else if(i!=k) res=max(res,d[i]);
}
return res;
}
};
Floyd
class Solution {
public:
vector<vector<int>> d;
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
d.resize(n+1,vector<int>(n+1,INT_MAX));
for(auto x:times)
{
int a=x[0],b=x[1],w=x[2];
d[a][b]=w;
}
for(int i=1;i<=n;i++) d[i][i]=0;
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(d[i][k]!=INT_MAX && d[k][j]!=INT_MAX && d[i][k]+d[k][j]<d[i][j])
{ d[i][j]=d[i][k]+d[k][j]; }
}
}
}
int res=0;
for(int i=1;i<=n;i++)
{
if(d[k][i]==INT_MAX) return -1;
else if(i!=k) res=max(res,d[k][i]);
}
return res;
}
};
Bellman-Ford
class Solution {
public:
vector<int> d;
void BF(vector<vector<int>>& times,int t,int n)
{
d[t]=0;
for(int i=0;i<n-1;i++)
{
for(auto x:times)
{
int a=x[0],b=x[1],w=x[2];
if(w<d[b]-d[a]) d[b]=d[a]+w;
}
}
}
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
d.resize(n+1,INT_MAX);
int res=0;
BF(times,k,n);
for(int i=1;i<=n;i++)
{
if(d[i]==INT_MAX) return -1;
else if(i!=k) res=max(res,d[i]);
}
return res;
}
};
SPFA
class Solution {
public:
vector<int> d;
vector<vector<pair<int,int>>> adj;
void SPFA(int t,int n)
{
d[t]=0;
queue<int> q;
q.push(t);
while(!q.empty())
{
int i=q.front(); q.pop();
for(auto x:adj[i])
{
int a=x.first,b=x.second;
if(d[i]+b<d[a])
{
d[a]=d[i]+b;
q.push(a);
}
}
}
}
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
d.resize(n+1,INT_MAX);
adj.resize(n+1);
for(auto x:times)
{
int a=x[0],b=x[1],w=x[2];
adj[a].push_back({b,w});
}
int res=0;
SPFA(k,n);
for(int i=1;i<=n;i++)
{
if(d[i]==INT_MAX) return -1;
else if(i!=k) res=max(res,d[i]);
}
return res;
}
};