0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 12:58:48 +00:00

P4779 【模板】单源最短路径(标准版)

https://www.luogu.com.cn/record/91959107
This commit is contained in:
Baoshuo Ren 2022-10-28 21:42:31 +08:00
parent 68329e8113
commit 004deb5af9
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1,43 +1,71 @@
#include <bits/stdc++.h> #include <iostream>
#include <algorithm>
#include <functional>
#include <queue>
#include <utility>
#include <vector>
using namespace std; using std::cin;
using std::cout;
const char endl = '\n';
int n, m, s, u, v, w, dist[200005]; const int N = 1e5 + 5;
vector<pair<int, int>> g[200005]; const int INF = 0x3f3f3f3f;
bool st[200005];
int n, m, s, dist[N];
std::vector<std::pair<int, int>> g[N];
bool vis[N];
void dijkstra() { void dijkstra() {
memset(dist, 0x3f, sizeof(dist)); std::fill_n(dist, N, INF);
std::priority_queue<
std::pair<int, int>,
std::vector<std::pair<int, int>>,
std::greater<std::pair<int, int>>>
q;
q.emplace(0, s);
dist[s] = 0; dist[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push(make_pair(0, s));
while (!q.empty()) { while (!q.empty()) {
auto t = q.top(); auto u = q.top().second;
q.pop(); q.pop();
int tw = t.second;
int td = t.first; if (vis[u]) continue;
if (st[tw]) continue; vis[u] = true;
for (auto i : g[tw]) {
int j = i.first; for (auto e : g[u]) {
if (dist[j] > td + i.second) { int v = e.first,
dist[j] = td + i.second; w = e.second;
q.push(make_pair(dist[j], j));
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
q.emplace(dist[v], v);
} }
} }
st[tw] = true;
} }
} }
int main() { int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> s; 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; cin >> u >> v >> w;
g[u].push_back(make_pair(v, w));
g[u].emplace_back(v, w);
} }
dijkstra(); dijkstra();
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
cout << dist[i] << ' '; cout << dist[i] << ' ';
} }
cout << endl; cout << endl;
return 0; return 0;
} }