void dijkstra(int src, IV &dis) { IIS q; dis = IV(n, INF); BV flg(n); dis[src] = 0; q.insert(II(0, src)); while (! q.empty()) { int v = q.begin()->second; q.erase(q.begin()); if (flg[v]) continue; flg[v] = true; cFor (EL, e, adj[v]) { int d = dis[v] + e->w; if (!flg[e->v] && d < dis[e->v]) { if (dis[e->v] != INF) q.erase(II(dis[e->v], e->v)); dis[e->v] = d; q.insert(II(dis[e->v], e->v)); } } }
void prim_min(ELV &g, int src) { IIS q; IV dis(n, INF); BV flg(n); dis[src] = 0; q.insert(II(0, src)); while (! q.empty()) { int d = q.begin()->first; int v = q.begin()->second; q.erase(q.begin()); if (flg[v]) continue; flg[v] = true; cFor (EL, e, g[v]) if (!flg[e->v] && e->w < dis[e->v]) { if (dis[e->v] != INF) q.erase(II(dis[e->v], e->v)); dis[e->v] = e->w; q.insert(II(dis[e->v], e->v)); } } }
int prim_mst(int src) { IIS q; IV dis(n, INF); BV flg(n); dis[src] = 0; q.insert(II(0, src)); int mst = 0; while (! q.empty()) { int d = q.begin()->first; int v = q.begin()->second; q.erase(q.begin()); if (flg[v]) continue; flg[v] = true; mst += d; For (EL, e, adj[v]) if (!flg[e->v] && e->w < dis[e->v]) { dis[e->v] = e->w; q.insert(II(dis[e->v], e->v)); } } return mst; }