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 <algorithm> #include <queue> #include <array> #include <map> #define maxn 100010 #define INF 1000000000 #define ll long long using namespace std;
int n, m, S, a, b, u[maxn], v[maxn], du[maxn]; vector<int> G[maxn];
map<array<int, 3>, int> mp;
struct Edge { int to, next; } e[maxn * 2]; int c1, head[maxn]; inline void add_edge(int u, int v) { e[c1].to = v; e[c1].next = head[u]; head[u] = c1++; }
struct Queue { int k, d;
friend bool operator < (const Queue &u, const Queue &v) { return u.d > v.d; } }; int vis[maxn]; int dis[maxn]; void dijkstra(int s) { for (int i = 1; i <= n; ++i) vis[i] = 0; for (int i = 1; i <= n; ++i) dis[i] = INF; dis[s] = 0; priority_queue<Queue> Q; Q.push({ s, 0 }); while (!Q.empty()) { int u = Q.top().k; Q.pop(); if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; vector<int> tmp; for (auto w : G[v]) { array<int, 3> a = { u, v, w }; sort(a.begin(), a.end()); if (mp.count(a)) tmp.push_back(w); else if (dis[w] > dis[u] + b) dis[w] = dis[u] + b, Q.push({ w, dis[w] }); } swap(G[v], tmp); if (dis[v] > dis[u] + a) { dis[v] = dis[u] + a; Q.push({ v, dis[v] }); } } } }
int main() { fill(head, head + maxn, -1); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m >> S >> a >> b; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; u[i] = x, v[i] = y; ++du[x], ++du[y]; add_edge(x, y), add_edge(y, x); } for (int i = 1; i <= m; ++i) { if (du[u[i]] > du[v[i]] || du[u[i]] == du[v[i]] && u[i] > v[i]) swap(u[i], v[i]); G[u[i]].push_back(v[i]); } for (int u = 1; u <= n; ++u) { for (auto v : G[u]) vis[v] = u; for (auto v : G[u]) for (auto w : G[v]) if (vis[w] == u) { array<int, 3> a = { u, v, w }; sort(a.begin(), a.end()); mp[a] = 1; } } for (int i = 1; i <= n; ++i) G[i].clear(); for (int i = 1; i <= m; ++i) G[u[i]].push_back(v[i]), G[v[i]].push_back(u[i]); dijkstra(S); for (int i = 1; i <= n; ++i) cout << dis[i] << "\n"; return 0; }
|