最短路
Dijkstra
题目:743. 网络延迟时间
邻接矩阵:
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a < b ? b : a;
}
int networkDelayTime(int** times, int timesSize, int* timesColSize, int n, int k){
int g[n+1][n+1];
memset(g, 0x3f, sizeof(g));
for (int i = 0; i < timesSize; i++) {
int a = times[i][0], b = times[i][1], v = times[i][2];
g[a][b] = v;
}
int dis[n+1];
int vis[n+1];
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
dis[k] = 0;
int res = 0;
for (int i = 1; i <= n; i++) {
int pos = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (pos == 0 || dis[j] < dis[pos])) {
pos = j;
}
}
vis[pos] = 1;
res = max(res, dis[pos])