-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11779.cpp
More file actions
94 lines (79 loc) · 1.74 KB
/
11779.cpp
File metadata and controls
94 lines (79 loc) · 1.74 KB
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 <stdio.h>
#include <queue>
#include <vector>
#include <stack>
#define INF 987654321
using namespace std;
vector<vector<pair<int, int>>> adj;
vector<int> dist, pre;
stack<int> track;
typedef struct NODE
{
int index, distance;
} NODE;
struct compare
{
bool operator()(const NODE &a, const NODE &b)
{
return a.distance > b.distance;
}
};
priority_queue<NODE, vector<NODE>, compare> nodes;
int findDist(int start, int end)
{
dist[start] = 0;
nodes.push({start, 0});
while (!nodes.empty())
{
NODE now = nodes.top();
nodes.pop();
if (dist[now.index] < now.distance)
continue;
for (int i = 0; i < adj[now.index].size(); i++)
{
int next = adj[now.index][i].first;
int nextDist = adj[now.index][i].second;
if (dist[next] > now.distance + nextDist)
{
pre[next] = now.index;
dist[next] = now.distance + nextDist;
nodes.push({next, now.distance + nextDist});
}
}
}
return dist[end];
}
void findTrack(int end)
{
while (end)
{
track.push(end);
end = pre[end];
}
}
int main()
{
int N, M;
int s, e, d;
int start, end;
scanf("%d", &N);
scanf("%d", &M);
adj.assign(N + 1, vector<pair<int, int>>(0, {0, 0}));
dist.assign(N + 1, INF);
pre.assign(N + 1, 0);
while (M--)
{
scanf("%d %d %d", &s, &e, &d);
adj[s].push_back({e, d});
}
scanf("%d %d", &start, &end);
printf("%d\n", findDist(start, end));
findTrack(end);
printf("%lu\n", track.size());
while (!track.empty())
{
printf("%d ", track.top());
track.pop();
}
return 0;
}