【图】多源最短路径floyd
来源:互联网 发布:英语好的软件 编辑:程序博客网 时间:2024/05/22 03:03
O(n^3),不过如果是优先队列优化过的dijkstra n次好像才O(N^2logn)
貌似是编程挑战的……
/*floyd.cCompute all-pairs shortest paths in weighted graphs.by: Steven Skienabegun: March 26, 2002*//*Copyright 2003 by Steven S. Skiena; all rights reserved. Permission is granted for use in non-commerical applicationsprovided this copyright notice remains intact and unchanged.This program appears in my book:"Programming Challenges: The Programming Contest Training Manual"by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.See our website www.programming-challenges.com for additional information.This book can be ordered from Amazon.com athttp://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/*/#include <stdlib.h>#include "stdbool.h"/************************************************************/#define TRUE 1#define FALSE 0#defineMAXV100/* maximum number of vertices */#define MAXDEGREE50/* maximum outdegree of a vertex */#define MAXINT100007typedef struct {int v;/* neighboring vertex */int weight;/* edge weight */bool in;/* is the edge "in" the solution? */} edge;typedef struct {edge edges[MAXV][MAXDEGREE];/* adjacency info */int degree[MAXV];/* outdegree of each vertex */int nvertices;/* number of vertices in the graph */int nedges;/* number of edges in the graph */} graph;typedef struct { int weight[MAXV+1][MAXV+1]; /* adjacency/weight info */ int nvertices; /* number of vertices in the graph */} adjacency_matrix;initialize_adjacency_matrix(adjacency_matrix *g)//初始化{int i,j;/* counters */g -> nvertices = 0;for (i=1; i<=MAXV; i++)for (j=1; j<=MAXV; j++)g->weight[i][j] = MAXINT;//将每条边的权值设为最大}read_adjacency_matrix(adjacency_matrix *g, bool directed)//输入图{int i;/* counter */int m;/* number of edges */int x,y,w;/* placeholder for edge and weight */initialize_adjacency_matrix(g);scanf("%d %d\n",&(g->nvertices),&m);for (i=1; i<=m; i++) {scanf("%d %d %d\n",&x,&y,&w);g->weight[x][y] = w;if (directed==FALSE) g->weight[y][x] = w;}}print_graph(adjacency_matrix *g)//输出图{int i,j;/* counters */for (i=1; i<=g->nvertices; i++) {printf("%d: ",i);for (j=1; j<=g->nvertices; j++)if (g->weight[i][j] < MAXINT)printf(" %d",j);printf("\n");}}print_adjacency_matrix(adjacency_matrix *g)//输出带权图{ int i,j; /* counters */ for (i=1; i<=g->nvertices; i++) { printf("%3d: ",i); for (j=1; j<=g->nvertices; j++) printf(" %3d",g->weight[i][j]); printf("\n"); }}floyd(adjacency_matrix *g)//floyd主算法{int i,j;/* dimension counters */int k;/* intermediate vertex counter */int through_k;/* distance through vertex k */for (k=1; k<=g->nvertices; k++)//节点设置for (i=1; i<=g->nvertices; i++)for (j=1; j<=g->nvertices; j++) {through_k = g->weight[i][k]+g->weight[k][j];if (through_k < g->weight[i][j])g->weight[i][j] = through_k;}} for(k=1;k<=g->nvertices;k++) { }int main(void){adjacency_matrix g;read_adjacency_matrix(&g,FALSE);print_graph(&g);floyd(&g);print_adjacency_matrix(&g); return 0;}
0 0
- 【图】多源最短路径floyd
- 图Floyd 多源最短路径求解 C++实现
- Floyd-Warshall多源最短路径
- 多源最短路径floyd算法
- 多源最短路径 Floyd
- Floyd 多源最短路径算法
- 多源最短路径Floyd算法
- Floyd算法--多源最短路径
- Floyd多源最短路径算法
- 多源最短路径--floyd算法
- floyd算法 多源最短路径
- 多源最短路径--Floyd-Warshall算法
- 多源最短路径问题-floyd warshall
- 多源最短路径算法---Floyd-Warshall
- 多源最短路径之Floyd算法
- Floyd最短路径(多源最短路径)
- 多源最短路径 Floyd 算法(有向图) C实现 ~
- 多源最短路径Floyd、Floyd求最小环【模板】
- [LeetCode] Longest Consecutive Sequence
- 用java实现单链表
- 图像处理中的傅立叶变换
- Linux下Opengrok安装部署与使用
- 基于Taintdroid思想的android ssl\tsl保密通信抓包研究
- 【图】多源最短路径floyd
- 编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
- Fragments碎片的生命周期
- 各类IP地址的范围
- 在weka中配置自己的算法
- 图像傅立叶变换的原理和物理意义
- shell 整型变量自增(加1)的几种方法
- 表空间碎片的发现
- TCP协议疑难杂症全景解析