鄰接表版本
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <limits.h>
const int INF = 0x3f3f3f3f;
const int MAXN = 100010;
const int MAXM = 200020;
using namespace std;
struct Vertex {
int pos;
int dist;
Vertex * next;
Vertex(const int & _pos = int(), const int & _dist = int(), Vertex * _next = nullptr) : pos(_pos), dist(_dist), next(_next) {}
} vtx[MAXN];
struct Node {
int dist;
int pos;
Node(const int & _dist = int(), const int & _pos = int()) :dist(_dist), pos(_pos) {}
bool operator<(const Node & another) const {
return another.dist < dist;
}
};
int dist[MAXN];
int visit[MAXN];
void dijkstra(const int & st) {
dist[st] = 0;
priority_queue<Node> q;
q.push(Node(0, st));
while (!q.empty()) {
Node tmp = q.top();
q.pop();
int x = tmp.pos;
if (visit[x]) {
continue;
}
visit[x] = 1;
for (Vertex * v = vtx[x].next; v; v = v->next) {
int y = v->pos;
if (dist[y] > dist[x] + v->dist) {
dist[y] = dist[x] + v->dist;
if (!visit[y]) {
q.push(Node(dist[y], y));
}
}
}
}
}
inline void addEdge(const int & u, const int & v, const int & w) {
Vertex * newVtx = new Vertex(v, w);
newVtx->next = vtx[u].next;
vtx[u].next = newVtx;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, s;
cin >> n >> m >> s;
memset(dist, INF, sizeof(dist));
for (register int i = 1; i <= m; ++i) {
register int u, v, w;
cin >> u >> v >> w;
addEdge(u, v, w);
}
dijkstra(s);
for (int i = 1; i <= n; ++i) {
if (!visit[i]) {
cout << INT_MAX << ' ';
}
else {
cout << dist[i] << ' ';
}
}
return 0;
}
鏈式前向星版本
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <limits.h>
const int INF = 0x3f3f3f3f;
const int MAXN = 100010;
const int MAXM = 200020;
using namespace std;
struct Edge {
int to, next, w;
Edge(const int & _to = int(), const int & _next = int(), const int & _w = int()) : to(_to), next(_next), w(_w) {}
} edge[MAXM];
struct Node {
int dist;
int pos;
Node(const int & _dist = int(), const int & _pos = int()) :dist(_dist), pos(_pos) {}
bool operator<(const Node & another) const {
return another.dist < dist;
}
};
int dist[MAXN];
int visit[MAXN];
int head[MAXN];
int cnt;
void dijkstra(const int & st) {
dist[st] = 0;
priority_queue<Node> q;
q.push(Node(0, st));
while (!q.empty()) {
Node tmp = q.top();
q.pop();
int x = tmp.pos;
if (visit[x]) {
continue;
}
visit[x] = 1;
for (int i = head[x]; i;i = edge[i].next) {
int y = edge[i].to;
if (dist[y] > dist[x] + edge[i].w) {
dist[y] = dist[x] + edge[i].w;
if (!visit[y]) {
q.push(Node(dist[y], y));
}
}
}
}
}
inline void addEdge(const int & u, const int & v, const int & w) {
++cnt;
edge[cnt].to = v;
edge[cnt].next = head[u];
edge[cnt].w = w;
head[u] = cnt;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, s;
cin >> n >> m >> s;
memset(dist, INF, sizeof(dist));
for (register int i = 1; i <= m; ++i) {
register int u, v, w;
cin >> u >> v >> w;
addEdge(u, v, w);
}
dijkstra(s);
for (int i = 1; i <= n; ++i) {
if (!visit[i]) {
cout << INT_MAX << ' ';
}
else {
cout << dist[i] << ' ';
}
}
return 0;
}