单源最短路径
来源:互联网 发布:华为端口聚合配置 编辑:程序博客网 时间:2024/06/12 18:59
#include <iostream>using namespace std;const static int MAX = 100;const static int INFIT = 1000;const static int WHITE = 0;const static int GRAY = 1;const static int BLACK = 2;//用来表示图规模int graph[MAX][MAX];int n;void dijkstra() { int minv; int d[MAX]; int color[MAX]; int u; for(int i=0; i<n; i++) { d[i] = INFIT; color[i] = WHITE; } d[0] = 0; color[0] = GRAY; //找到当前最小点 while(1) { minv = INFIT; u = -1; for(int i=0; i<n; i++) { if(color[i] != BLACK && d[i] < minv ){ minv = d[i]; u = i; } } //点孤立或点循环完 if(u==-1) break; color[u] = BLACK; //更新周围点 for(int i=0; i<n; i++) { if(color[i] != BLACK) { if(d[i] > d[u] + graph[u][i]) { d[i] = d[u] + graph[u][i]; color[i] = GRAY; } } } } for(int i=0; i<n; i++) { printf("%d %d\n", i, (d[i] == INFIT)? -1 :d[i]); }}int main() { cin >> n; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { graph[i][j] = INFIT; } } int n1, n2, n3, n4; for(int i=0; i<n; i++) { cin >> n1 >> n2; for(int i=0; i<n2; i++) { cin >> n3 >> n4; graph[n1][n3] = n4; } } dijkstra(); return 0;}
0 0
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- 单源最短路径
- pistache(C++ REST 工具) 使用
- maven应用(二)
- 剑指offer-31.整数中1出现的次数(从1到n整数中1出现的次数)
- PHP接口类interface使用方法
- hdu 5978 · To begin or not to begin【概率题】【手速题】
- 单源最短路径
- Map集合及HashMap源码分析
- 二分贪心 G
- Codeforces Round #408(Div.2)
- C笔记
- pzhu 1080: 修电脑 (Dijkstra 算法)
- Simpsons' Hidden Talents hdu2594 kmp
- [LeetCode]566. Reshape the Matrix
- Node.js建站之安装express(一)