-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback_11779.cpp
More file actions
79 lines (74 loc) · 1.54 KB
/
back_11779.cpp
File metadata and controls
79 lines (74 loc) · 1.54 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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#define INF 2111111111
using namespace std;
int a, b;
int st, ed;
vector<pair<int,int> > map[1001];
int dist[1001];
int track[1001];
void djikstra()
{
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
pq.push(make_pair(0,st));
while(!pq.empty())
{
int node = pq.top().second;
int cost = pq.top().first;
pq.pop();
if (dist[node] < cost)
continue;
for (int i = 0; i < map[node].size(); i++)
{
int nextnode = map[node][i].first;
int nextcost = map[node][i].second + cost;
if (dist[nextnode] > nextcost)
{
track[nextnode] = node;
dist[nextnode] = nextcost;
pq.push(make_pair(dist[nextnode],nextnode));
}
}
}
}
void clear()
{
for (int i = 1; i <= a; i++)
{
dist[i] = INF;
}
}
int main()
{
cin >> a >> b;
for (int i = 1; i <= b; i++)
{
int a1, a2, a3;
cin >> a1 >> a2 >> a3;
map[a1].push_back(make_pair(a2,a3));
}
cin >> st >> ed;
clear();
djikstra();
cout << dist[ed] << "\n";
int c = ed;
int cot = 0;
stack<int> track_s;
while (1)
{
cot++;
track_s.push(c);
if (c == st)
break;
c = track[c];
}
cout << cot << "\n";
while (!track_s.empty())
{
cout << track_s.top() << " ";
track_s.pop();
}
return 0;
}