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
| const int maxn = 100010; struct Edge { int next, to, w; } edges[maxn * 2]; int heads[maxn], pos = 0;
void addEdge(int a, int b, int w) { edges[++pos].next = heads[a]; edges[pos].to = b; edges[pos].w = w; heads[a] = pos; }
bool vis[maxn]; int dist[maxn]; void dijkstra(int s, int n) { memset(vis, 0, sizeof(vis)); memset(dist, 63, sizeof(dist)); dist[s] = 0; using pi = pair<int, int>; priority_queue<pi, vector<pi>, greater<>> q; q.emplace(0, s); while (!q.empty()) { pi t = q.top(); q.pop(); if (vis[t.second]) continue; vis[t.second] = true; for (int i = heads[t.second]; ~i; i = edges[i].next) { int v = edges[i].to, w = edges[i].w; if (!vis[v] && w + t.first < dist[v]) q.emplace(dist[v] = w + t.first, v); } } }
|