From 004deb5af9bfaf1d031a508e7ef9c9f08e300a06 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 28 Oct 2022 21:42:31 +0800 Subject: [PATCH] =?UTF-8?q?P4779=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?=E6=A0=87=E5=87=86=E7=89=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/91959107 --- Luogu/P4779/P4779.cpp | 68 ++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/Luogu/P4779/P4779.cpp b/Luogu/P4779/P4779.cpp index ea5b2fe6..601f1bb4 100644 --- a/Luogu/P4779/P4779.cpp +++ b/Luogu/P4779/P4779.cpp @@ -1,43 +1,71 @@ -#include +#include +#include +#include +#include +#include +#include -using namespace std; +using std::cin; +using std::cout; +const char endl = '\n'; -int n, m, s, u, v, w, dist[200005]; -vector> g[200005]; -bool st[200005]; +const int N = 1e5 + 5; +const int INF = 0x3f3f3f3f; + +int n, m, s, dist[N]; +std::vector> g[N]; +bool vis[N]; void dijkstra() { - memset(dist, 0x3f, sizeof(dist)); + std::fill_n(dist, N, INF); + + std::priority_queue< + std::pair, + std::vector>, + std::greater>> + q; + + q.emplace(0, s); dist[s] = 0; - priority_queue, vector>, greater>> q; - q.push(make_pair(0, s)); + while (!q.empty()) { - auto t = q.top(); + auto u = q.top().second; 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)); + + if (vis[u]) continue; + vis[u] = true; + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (dist[v] > dist[u] + w) { + dist[v] = dist[u] + w; + q.emplace(dist[v], v); } } - st[tw] = true; } } int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + cin >> n >> m >> s; - for (int i = 1; i <= m; i++) { + + for (int i = 1, u, v, w; i <= m; i++) { cin >> u >> v >> w; - g[u].push_back(make_pair(v, w)); + + g[u].emplace_back(v, w); } + dijkstra(); + for (int i = 1; i <= n; i++) { cout << dist[i] << ' '; } + cout << endl; + return 0; }