最短路径模板
spfa:
void add( int u, int v, int w) { g[cnt].v = v; g[cnt].w = w; g[cnt].next = pre[u]; pre[u] = cnt++; } bool spfa( int s) { int i; queue< int >q; for (i = 0; i < maxn; ++i) dis[i] = -inf; dis[s] = 0; q.push(s); inq[s] = true ; while (!q.empty()) { int u = q.front(); q.pop(); if (++ind[u] > 24) return false ; inq[u] = false ; for (i = pre[u]; i != -1; i = g[i].next) { int v = g[i].v, w = g[i].w; if (dis[v] < dis[u] + w) { dis[v] = dis[u] + w; if (!inq[v]) { inq[v] = true ; q.push(v); } } } } return true ; } |
Dijkstra
void Dijkstra( int n, int v, int *dist, int *prev, int c[maxnum][maxnum]) { bool s[maxnum]; // 判断是否已存入该点到S集合中 for ( int i=1; i<=n; ++i) { dist[i] = c[v][i]; s[i] = 0; // 初始都未用过该点 if (dist[i] == maxint) prev[i] = 0; else prev[i] = v; } dist[v] = 0; s[v] = 1; // 依次将未放入S集合的结点中,取dist[]最小值的结点,放入结合S中 // 一旦S包含了所有V中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度 // 注意是从第二个节点开始,第一个为源点 for ( int i=2; i<=n; ++i) { int tmp = maxint; int u = v; // 找出当前未使用的点j的dist[j]最小值 for ( int j=1; j<=n; ++j) if ((!s[j]) && dist[j]<tmp) { u = j; // u保存当前邻接点中距离最小的点的号码 tmp = dist[j]; } s[u] = 1; // 表示u点已存入S集合中 // 更新dist for ( int j=1; j<=n; ++j) if ((!s[j]) && c[u][j]<maxint) { int newdist = dist[u] + c[u][j]; if (newdist < dist[j]) { dist[j] = newdist; prev[j] = u; } } } } // 查找从源点v到终点u的路径,并输出 void searchPath( int *prev, int v, int u) { int que[maxnum]; int tot = 1; que[tot] = u; tot++; int tmp = prev[u]; while (tmp != v) { que[tot] = tmp; tot++; tmp = prev[tmp]; } que[tot] = v; for ( int i=tot; i>=1; --i) if (i != 1) cout << que[i] << " -> " ; else cout << que[i] << endl; } |
bellman_ford
void relax( int u, int v, int weight) { if (dist[v] > dist[u] + weight) dist[v] = dist[u] + weight; } bool Bellman_Ford() { for ( int i=1; i<=nodenum-1; ++i) for ( int j=1; j<=edgenum; ++j) relax(edge[j].u, edge[j].v, edge[j].weight); bool flag = 1; // 判断是否有负环路 for ( int i=1; i<=edgenum; ++i) if (dist[edge[i].v] > dist[edge[i].u] + edge[i].weight) { flag = 0; break ; } return flag; } |
floyd
void floyd() { int i,j,k; for (k = 0; k < n; ++k) { for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { if (map[i][k] != inf && map[k][j] != inf && map[i][k] + map[k][j] < map[i][j]) map[i][j] = map[i][k]*map[k][j]; } } } } |