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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| #include <iostream> #include <cstring> #include <algorithm> #include <queue> #include <unordered_map> #define endl '\n' #define x first #define y second #define mk make_pair #define int long long #define NO puts("NO") #define YES puts("YES") #define umap unordered_map #define INF 0x3f3f3f3f3f3f3f3f #define All(x) (x).begin(),(x).end() #pragma GCC optimize(3,"Ofast","inline") #define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0) using namespace std; const int N = 2e6 + 10; using PII = pair<int,int>; int n,m,start,last; int dist[N]; int tree[N]; bool st[N]; vector<int>path;
int h[N],e[N],w[N],ne[N],idx; inline void Add(int a,int b,int c) { e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx++; } inline void Dijkstra() { memset(tree,-1,sizeof tree); memset(dist,INF,sizeof dist); dist[start] = 0; priority_queue<PII,vector<PII>,greater<PII>>q; q.push(mk(0,start)); while(q.size()) { PII now = q.top(); q.pop(); int a = now.y; int dis = now.x; if(st[a]) continue; st[a] = true; for(int i = h[a];i != -1;i = ne[i]) { int j = e[i]; if(dist[j] > dis + w[i]) { dist[j] = dis + w[i]; tree[j] = a; } q.push(mk(dist[j],j)); } } return ; } void getPath(int now) { if(now == start) { path.emplace_back(now); return ; } getPath(tree[now]); path.emplace_back(now); } inline void solve() { memset(h,-1,sizeof h); cin >> n >> m >> start >> last; while(m--) { int a,b,c; cin >> a >> b >> c; Add(a,b,c); Add(b,a,c); } Dijkstra(); getPath(last); cout << dist[last] << ' '; bool rem = false; for(int i : path) { if(rem) cout << "->"; cout << i; rem = true; } return ; } signed main() {
___G; int _t = 1;
while (_t--) { solve(); } return 0; }
|