voideachT(){ int n = read(); for (int i = 0; i <= n; ++i) { vis[i] = 0; dis[i] = 0x3f3f3f3f; for (int j = 0; j <= n; ++j) E[i][j] = 0x3f3f3f3f; } // 点数n 并初始化
for (int m = read(); m--;) { int u = read(), v = read(), w = read(); E[u][v] = w; } // 边数m 邻接矩阵存边
int st = read(); dis[st] = 0; // 起始点初始化
for (int u = -1;; u = -1) { // 从未确定最短路的点中选取最短路长度最小的点 for (int i = 1; i <= n; ++i) if (!vis[i] && (u == -1 || dis[i] < dis[u])) u = i; if (u == -1) break; // 找不到 说明已经遍历完毕
vis[u] = 1; // 这个点的最短路已经确定 // 松弛出边 for (int i = 1; i <= n; ++i) if (!vis[i]) dis[i] = min(dis[i], dis[u] + E[u][i]); } for (int i = 1; i <= n; ++i) printf("%d ", dis[i] == 0x3f3f3f3f ? -1 : dis[i]); }
structEdge { int nxt, to, w; }; vector<Edge> E; int head[N]; inlinevoidadd(int u, int v, int w){ E.push_back({ head[u], v, w }); head[u] = E.size() - 1; } // 链式前向星
int dis[N], vis[N], path[N]; unsignedlonglong pcnt[N];
voidDijkstra(int n, int st){ for (int i = 0; i <= n; ++i) dis[i] = 0x3f3f3f3f, vis[i] = path[i] = pcnt[i] = 0; dis[st] = 0, pcnt[st] = 1; // 初始化到自己的距离和最短路
priority_queue<pii, vector<pii>, greater<pii> > Q; Q.push({ 0, st }); while (!Q.empty()) { auto [dis_u, u] = Q.top(); Q.pop(); if (vis[u]) continue; // 取出未访问的点 vis[u] = 1; for (int i = head[u]; i >= 1; i = E[i].nxt) { int to = E[i].to; if (dis[to] == E[i].w + dis_u) { pcnt[to] += pcnt[u]; // 继承最短路数量 } elseif (dis[to] > E[i].w + dis_u) { dis[to] = E[i].w + dis_u; // 松弛 path[to] = u; // 前驱记录路径 pcnt[to] = pcnt[u]; // 更新最短路数量 Q.push({ dis[to], to }); // 已更新的点加入Q } } } }
voideachT(){ int n = read(); E.push_back({ -1,0,0 }); for (int m = read(); m--;) { int u = read(), v = read(), w = read(); add(u, v, w); } // 链式前向星存边
int st = read(); Dijkstra(n, st);
for (int i = 1; i <= n; ++i) printf("%d ", dis[i] == 0x3f3f3f3f ? -1 : dis[i]); for (int i = 1; i <= n; ++i) printf("%lld ", pcnt[i]);
int end = read(); while (end != st) { printf("%d<-", end); end = path[end]; } printf("%d\n", st); }
structEdge { int nxt, to, w; }; vector<Edge> E; int head[N]; inlinevoidadd(int u, int v, int w){ E.push_back({ head[u], v, w }); head[u] = E.size() - 1; }
int bis[N], dis[N], cnt[N], vis[N];
boolSPFA(int n, int st){ for (int j = 1; j <= n; ++j) bis[j] = 0x3f3f3f3f, cnt[j] = vis[j] = 0; bis[st] = 0, vis[st] = 1, cnt[st] = 1;
queue<int> Q; Q.push(st); while (!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for (int i = head[u]; i >= 1; i = E[i].nxt) { int v = E[i].to, w = E[i].w; if (bis[u] != 0x3f3f3f3f && bis[v] > bis[u] + w) { bis[v] = bis[u] + w; if (!vis[v]) { Q.push(v), vis[v] = 1; cnt[v] += 1; if (cnt[v] > n) return1; } } } } return0; }
voidDijkstra(int n, int st){ for (int i = 0; i <= n; ++i) dis[i] = 0x3f3f3f3f, vis[i] = 0; dis[st] = 0;
priority_queue<pii, vector<pii>, greater<pii> > Q; Q.push({ 0, st }); while (!Q.empty()) { auto [dis_u, u] = Q.top(); Q.pop(); if (vis[u]) continue; // 取出未访问的点 vis[u] = 1; for (int i = head[u]; i >= 1; i = E[i].nxt) { int to = E[i].to; if (dis[to] > E[i].w + dis_u) { dis[to] = E[i].w + dis_u; // 松弛 Q.push({ dis[to], to }); // 已更新的点加入Q } } }
voideachT(){ int n = read(); for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) dis[i][j] = 0x3f3f3f3f; } // 点数n 并初始化
for (int m = read(); m--;) { int u = read(), v = read(), w = read(); dis[u][v] = w; } // 边数m 邻接矩阵存边
for (int i = 1; i <= n; ++i) dis[i][i] = 0; // 初始化 for (int k = 1; k <= n; ++k) { for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); } }