This repository has been archived by the owner on Aug 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
G.cpp
94 lines (79 loc) · 1.6 KB
/
G.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define MAX 2010
#define INF 0x3F3F3F3F
using namespace std;
struct edge
{
int node, dist;
edge(int node, int dist) : node(node), dist(dist) {}
};
int i, j, v, d, to, cost, n, m, s, f, b, e, w;
int used[MAX], dist[MAX], parent[MAX];
vector<vector<edge>> g;
priority_queue<edge> pq;
bool operator<(edge a, edge b)
{
return a.dist > b.dist;
}
void Relax(int v, int to, int cost)
{
if (dist[to] > dist[v] + cost)
{
dist[to] = dist[v] + cost;
pq.push(edge(to, dist[to]));
parent[to] = v;
}
}
void path(int v)
{
if (v == -1)
return;
path(parent[v]);
if (parent[v] != -1)
printf(" ");
printf("%d", v);
}
int main()
{
scanf("%d%d", &n, &m);
scanf("%d%d", &s, &f);
g.resize(n + 1);
for (i = 0; i < m; i++)
{
scanf("%d%d%d", &b, &e, &w);
g[b].push_back(edge(e, w));
g[e].push_back(edge(b, w));
}
memset(dist, 0x3F, sizeof(dist));
dist[s] = 0;
memset(parent, -1, sizeof(parent));
memset(used, 0, sizeof(used));
pq.push(edge(s, 0));
while (!pq.empty())
{
edge e = pq.top();
pq.pop();
v = e.node;
if (e.dist > dist[v])
continue;
for (j = 0; j < g[v].size(); j++)
{
to = g[v][j].node;
cost = g[v][j].dist;
if (!used[to])
Relax(v, to, cost);
}
used[v] = 1;
}
if (dist[f] == INF)
printf("-1\n");
else
{
path(f);
printf("\n");
}
return 0;
}