bool vis[maxn][2], use[maxn]; ll dis[maxn][2]; int s; voiddijkstra(){ priority_queue<node> Q; Q.push({ s, 0 }); for (int i = 1; i <= n; ++i) dis[i][0] = dis[i][1] = INF; dis[s][0] = 0; for (int i = 1; i <= n; ++i) vis[i][0] = vis[i][1] = 0; vector<int> vec; for (int i = 1; i <= n; ++i) vec.push_back(i); while (!Q.empty()) { int u = Q.top().k, o = Q.top().t; Q.pop(); if (vis[u][o]) continue; vis[u][o] = 1; for (int i = head[u]; ~i; i = e[i].next) use[e[i].to] = 1; if (o) { vector<int> tmp; for (auto v : vec) { if (use[v]) { tmp.push_back(v); continue; } if (dis[v][0] > dis[u][o]) { dis[v][0] = dis[u][o]; Q.push({ v, 0, dis[v][0] }); } } vec = tmp; } for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to, w = e[i].w - o * k, p = e[i].fi; if (dis[v][p] > dis[u][o] + w) { dis[v][p] = dis[u][o] + w; Q.push({ v, p, dis[v][p] }); } } for (int i = head[u]; ~i; i = e[i].next) use[e[i].to] = 0; } }
voidwork(){ cin >> n >> m >> s >> k; for (int i = 1; i <= n; ++i) head[i] = -1; c1 = 0; for (int i = 1; i <= m; ++i) { int x, y, z, w; cin >> x >> y >> z >> w; add_edge(x, y, z, w); } dijkstra(); for (int i = 1; i <= n; ++i) { ll v = min(dis[i][0], dis[i][1]); cout << (v == INF ? -1 : v) << " "; } cout << "\n"; }