diff --git a/Luogu/problem/P3905/P3905.cpp b/Luogu/problem/P3905/P3905.cpp index 64ec8a3c..e6951ec3 100644 --- a/Luogu/problem/P3905/P3905.cpp +++ b/Luogu/problem/P3905/P3905.cpp @@ -2,7 +2,25 @@ using namespace std; -int n, m, u, v, w, d, a, b, f[105][105], g[105][105]; +int n, m, u, v, w, d, a, b, f[105][105], g[105][105], dist[105]; +bool st[105]; + +void dijkstra() { + memset(dist, 0x3f, sizeof(dist)); + dist[a] = 0; + for (int i = 1; i < n; i++) { + int t = -1; + for (int j = 1; j <= n; j++) { + if (!st[j] && (t == -1 || dist[t] > dist[j])) { + t = j; + } + } + for (int j = 1; j <= n; j++) { + dist[j] = min(dist[j], dist[t] + g[t][j]); + } + st[t] = true; + } +} int main() { cin >> n >> m; @@ -19,16 +37,11 @@ int main() { cin >> d; for (int i = 1; i <= d; i++) { cin >> u >> v; - g[u][v] = g[v][u] = f[u][v]; - } - for (int k = 1; k <= n; k++) { - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - g[i][j] = min(g[i][j], g[i][k] + g[k][j]); - } - } + g[u][v] = f[u][v]; + g[v][u] = f[v][u]; } cin >> a >> b; - cout << g[a][b] << endl; + dijkstra(); + cout << dist[b] << endl; return 0; }