感受
思路
#include<bits/stdc++.h> #define ls o << 1 #define rs o << 1 | 1 using namespace std; typedef long long ll; const int maxn = 1e6 + 10; const int MAXN = 1e5 + 10; const int inf = 0x3f3f3f3f;//1061109567 大约1e9 且inf + inf < int最大值 const ll INF = 0x3f3f3f3f3f3f3f3f;//4557430888798830399 大约4e18 且INF + INF < long long最大值 int n, q, s, tot; int id[MAXN << 2][2]; struct Edge{ int from, to; ll dis; }; struct Node{ ll dis;///最短距离 int x;///顶点编号 bool operator < (const Node &a)const{ return dis > a.dis; } }; bool vis[maxn]; ll dis[maxn]; vector<Edge> G[maxn]; priority_queue<Node> que; void add_edge(int u, int v, ll w){ G[u].push_back((Edge){u, v, w}); //cout << "(" << u << "," << v << ") = " << w << endl; } void dijkstra(int s){ for(int i = 1; i <= tot; i++){ dis[i] = INF; vis[i] = false; } dis[s] = 0; que.push((Node){0, s}); while(!que.empty()){ Node tmp = que.top(); que.pop(); int u = tmp.x; if(vis[u]) continue; vis[u] = true; for(int i = 0; i < G[u].size(); i++){ Edge &e = G[u][i]; if(dis[e.to] > dis[e.from] + e.dis){ dis[e.to] = dis[e.from] + e.dis; que.push((Node){dis[e.to], e.to}); } } } } void build(int o, int l, int r, int opt){ id[o][opt] = ++tot; if(l == r){ add_edge(id[o][opt], l, 0); add_edge(l, id[o][opt], 0); return ; } int mid = (l + r) / 2; build(ls, l, mid, opt); build(rs, mid + 1, r, opt); if(!opt){ add_edge(id[o][opt], id[ls][opt], 0); add_edge(id[o][opt], id[rs][opt], 0); } else{ add_edge(id[ls][opt], id[o][opt], 0); add_edge(id[rs][opt], id[o][opt], 0); } } void update(int o, int l, int r, int x, int y, int opt, int u, ll w){ if(l >= x && y >= r){ if(!opt){ add_edge(u, id[o][opt], w); } else{ add_edge(id[o][opt], u, w); } return ; } int mid = (l + r) / 2; if(mid >= x) update(ls, l, mid, x, y, opt, u, w); if(y > mid) update(rs, mid + 1, r, x, y, opt, u, w); } int main(){ scanf("%d%d%d", &n, &q, &s); tot = n; build(1, 1, n, 0); build(1, 1, n, 1); int opt; while(q--){ scanf("%d", &opt); if(opt == 1){ int v, u; ll w; scanf("%d%d%lld", &v, &u, &w); add_edge(v, u, w); } else{ int v, l, r; ll w; scanf("%d%d%d%lld", &v, &l, &r, &w); update(1, 1, n, l, r, opt - 2, v, w); } } dijkstra(s); for(int i = 1; i <= n; i++){ printf("%lld%c", dis[i] == INF ? -1 : dis[i], i == n ? '\n' : ' '); } return 0; }