int cnt, head[maxn]; structEdge { int next, to, dis; }edge[maxm]; voidadd(int from, int to, int dis) { edge[++cnt].next = head[from]; edge[cnt].to = to; edge[cnt].dis = dis; head[from] = cnt; }
voidSPFA() { queue<int> q; for (int i = 1; i <= n; ++i) { dist[i] = INT_MAX; } q.push(s); dist[s] = 0; vis[s] = true; while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for (int i = head[u]; i; i = edge[i].next) { int v = edge[i].to; if (dist[v] > dist[u] + edge[i].dis) { dist[v] = dist[u] + edge[i].dis; if (!vis[v]) { vis[v] = true; q.push(v); } } } } return; }