From fc8414509831e6efabbdba1e8860bb9c28cf9449 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sun, 18 Jul 2021 22:14:12 +0800 Subject: [PATCH] =?UTF-8?q?P3371=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E5=8D=95=E6=BA=90=E6=9C=80=E7=9F=AD=E8=B7=AF=E5=BE=84=EF=BC=88?= =?UTF-8?q?=E5=BC=B1=E5=8C=96=E7=89=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R53521521 --- Luogu/problem/P3371/P3371.cpp | 41 ++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Luogu/problem/P3371/P3371.cpp b/Luogu/problem/P3371/P3371.cpp index db4e4f54..682264a3 100644 --- a/Luogu/problem/P3371/P3371.cpp +++ b/Luogu/problem/P3371/P3371.cpp @@ -2,38 +2,39 @@ using namespace std; -int n, m, s, x, y, z, g[10005][10005], dist[10005]; -bool st[10005]; +int n, m, s, u, v, w, dist[200005]; +vector> g[200005]; +bool st[200005]; -void diskstra() { +void dijkstra() { memset(dist, 0x3f, sizeof(dist)); dist[s] = 0; - for (int i = 0; i < n - 1; i++) { - int t = -1; - for (int j = 1; j <= n; j++) { - if (!st[j] && (t == -1 || dist[t] > dist[j])) { - t = j; + priority_queue, vector>, greater>> q; + q.push(make_pair(0, s)); + while (!q.empty()) { + auto t = q.top(); + q.pop(); + int tw = t.second; + int td = t.first; + if (st[tw]) continue; + for (auto i : g[tw]) { + int j = i.first; + if (dist[j] > td + i.second) { + dist[j] = td + i.second; + q.push(make_pair(dist[j], j)); } } - for (int j = 1; j <= n; j++) { - dist[j] = min(dist[j], dist[t] + g[t][j]); - } - st[t] = true; + st[tw] = true; } } int main() { cin >> n >> m >> s; - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - g[i][j] = i == j ? 0 : 0x3f3f3f3f; - } - } for (int i = 1; i <= m; i++) { - cin >> x >> y >> z; - g[x][y] = min(g[x][y], z); + cin >> u >> v >> w; + g[u].push_back(make_pair(v, w)); } - diskstra(); + dijkstra(); for (int i = 1; i <= n; i++) { cout << (dist[i] == 0x3f3f3f3f ? INT_MAX : dist[i]) << ' '; }