标签:
#include<stdio.h> int main() { int m, n, i, j, k, t1, t2, t3, e[10][10]; scanf_s("%d %d", &n, &m); for (i = 1; i <= n;i++) for (j = 1; j <= n; j++)//Initialize the matrix { if (i == j) e[i][j] = 0; else e[i][j] = 99999; } for (i = 1; i <= m; i++)//read the vertex { scanf_s("%d %d %d", &t1, &t2, &t3); e[t1][t2] = t3; } for (k = 1; k <= n;k++) for (i = 1; i <= n;i++) for (j = 1; j <= n; j++) { if (e[i][j] > e[i][k] + e[k][j]) e[i][j] = e[i][k] + e[k][j]; } for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) printf_s("%d ", e[i][j]); printf_s("\n"); } getchar(); return 0; }
n代表节点数目,m代表边的数目。输入的时候t1 t2代表两个边,t3代表权值,k代表中间节点,最后就可以输出任意两点之间的最短路径(代码来源于纪磊)。
标签:
原文地址:http://blog.csdn.net/zyh920521/article/details/42064947