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 147 148 149 150 151 152 153 154 155 156 157 158
| #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,k,start,last; int dist[N]; bool st[N];
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++; } vector<int>tree[N]; inline void Dijkstra() { 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].clear(); tree[j].emplace_back(a); }else if(dist[j] == dis + w[i]) tree[j].emplace_back(a); q.push(mk(dist[j],j)); } } return ; } vector<vector<int>>paths; vector<int>tempPath; void getPath(int now) { if(now == start) { tempPath.emplace_back(now); paths.emplace_back(tempPath); tempPath.pop_back(); return ; } tempPath.emplace_back(now); for(auto i : tree[now]) { getPath(i); } tempPath.pop_back(); return ; } inline void solve() { memset(h,-1,sizeof h); cin >> n >> k >> start >> last; while(k--) { int a,b,c; cin >> a >> b >> c; Add(a,b,c); Add(b,a,c); } Dijkstra(); getPath(last); int sum = paths.size(); for(int i = 0;i < sum;++i) { reverse(All(paths[i])); } sort(All(paths)); cout << sum << endl; for(int i = 0;i < sum;++i) { bool rem = false; for(int j : paths[i]) { if(rem) cout << "->"; cout << j; rem = true; } cout << endl; } } signed main() {
___G; int _t = 1;
while (_t--) { solve(); } return 0; }
|