-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11779.cpp
More file actions
65 lines (52 loc) · 1.23 KB
/
11779.cpp
File metadata and controls
65 lines (52 loc) · 1.23 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
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<stack>
#define N 1005
using namespace std;
typedef pair<int, int> pii;
int n, d[N], p[N];
vector<pii> bus[N];
priority_queue<pii> q;
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int i, j, m, a, b, c, s, e;
cin >> n >> m;
for(i=1; i<=m; i++){
cin >> a >> b >> c;
bus[a].push_back({b, c});
}
cin >> s >> e;
for(i=1; i<=n; i++) p[i]=i;
memset(d, -1, sizeof(d));
d[s]=0;
q.push({s, 0});
while(!q.empty()){
pii head = q.top();
q.pop();
if(d[head.first]<-head.second) continue;
for(auto i : bus[head.first]){
if(d[i.first]==-1 || d[i.first]>-head.second+i.second){
d[i.first]=-head.second+i.second;
p[i.first]=head.first;
q.push({i.first, -d[i.first]});
}
}
}
cout << d[e] << endl;
stack<int> route;
route.push(e);
while(route.top()!=s){
route.push(p[route.top()]);
}
cout << route.size() << endl;
while(!route.empty()){
cout << route.top() << " ";
route.pop();
}
return 0;
}