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
| #include <cstdio> #include <algorithm> #include <queue> using namespace std; typedef long long LL; const LL inf = 3e18; const int N = 1e5 + 10, M = 2e5 + 10; bool vis[N]; int top, stk[N]; int n, st, ed; LL wt[M], d[N]; int idx, hd[N], nxt[M], edg[M]; struct Node { LL c, x; void input() { scanf("%lld%lld", &c, &x); } bool operator<(const Node &_) const { return x < _.x || (x == _.x && c < _.c); } } w[N]; void add(int a, int b) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; wt[idx] = abs(w[a].x - w[b].x) * w[a].c; } int main() { int s, t; scanf("%d%d%d", &n, &s, &t); for (int i = 1; i <= n; i++) w[i].input(); w[++n] = (Node){0, t}; sort(w + 1, w + n + 1); for (int i = 1; i <= n; i++) if (w[i].x == s) { st = i; break; } for (int i = 1; i <= n; i++) if (!w[i].c) { ed = i; break; } for (int i = 1; i <= n; i++) hd[i] = -1; for (int i = 1; i <= n; i++) { while (top && w[stk[top]].c > w[i].c) { add(stk[top], i); top--; } stk[++top] = i; } top = 0; for (int i = n; i; i--) { while (top && w[stk[top]].c > w[i].c) { add(stk[top], i); top--; } stk[++top] = i; } for (int i = 1; i <= n; i++) d[i] = inf; d[st] = 0; queue<int> q; q.push(st); vis[st] = true; while (!q.empty()) { int t = q.front(); q.pop(); vis[t] = false; for (int i = hd[t]; ~i; i = nxt[i]) if (d[t] + wt[i] < d[edg[i]]) { d[edg[i]] = d[t] + wt[i]; if (!vis[edg[i]]) { vis[edg[i]] = true; q.push(edg[i]); } } } printf("%lld", d[ed]); return 0; }
|