| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 856 人关注过本帖
标题:[求助]请教一个问题.->.图论!!!
只看楼主 加入收藏
tary
Rank: 1
等 级:新手上路
帖 子:780
专家分:0
注 册:2004-10-5
收藏
 问题点数:0 回复次数:4 
[求助]请教一个问题.->.图论!!!

题目:用弗洛伊德算法, 计算出每一对顶点之间的最短路径. #define MaxVertexNum 100 #define INFITY 200 typedef int EdgeType; typedef char VertexType; typedef int pathMatrix; typedef int distancMatrix; typedef struct{ VertexType vexs[MaxVertexNum]; EdgeType edges[MaxVertexNum][MaxVertexNum]; int n,e; }MGraph; void shortest_path(MGraph *G,pathMatrix p[MaxVertexNum][MaxVertexNum],distancMatrix D[MaxVertexNum][MaxVertexNum]) /* 最短路径主算法*/ { int v,w,u,i,j; for(v=0;v<G->n;++v) for(w=0;w<G->n;++w) {D[v][w]=G->edges[v][w];/*初始化D数组*/ if(D[v][w]<INFITY&&D[v][w]!=0) p[v][w]=v; else if(v!=w) p[v][w]=-2; if(v==w) p[v][w]=-1;/*初始化P数组*/ } for(u=0;u<G->n;++u) for(v=0;v<G->n;++v) for(w=0;w<G->n;++w) if(D[v][u]+D[u][w]<D[v][w]) {D[v][w]=D[v][u]+D[u][w]; p[v][w]=u; } /*弗洛依德算法*/ for(i=0;i<G->n;i++) for(j=0;j<G->n;j++) { if(p[i][j]==i) printf("%c-->%c\n",G->vexs[i],G->vexs[j]); else if(p[i][j]>=0) printf("%c-->%c-->%c\n",G->vexs[i],G->vexs[p[i][j]],G->vexs[j]); /*输出*/ } } main() {MGraph *G; pathMatrix p[MaxVertexNum][MaxVertexNum]; distancMatrix D[MaxVertexNum][MaxVertexNum]; int i,j,k,w,m;clrscr(); G=(MGraph *)malloc(sizeof(MGraph));

printf("please input the numbers of the n and e:\n"); scanf("%d,%d",&(G->n),&(G->e));/*输入边数跟顶点数*/

printf("please input vertex:\n"); for(i=0;i<G->n;i++) scanf("\n%c",&(G->vexs[i]));/*输入顶点信息*/

for(i=0;i<G->n;i++) for(j=0;j<G->n;j++) {if(i==j)G->edges[i][j]=0; else G->edges[i][j]=INFITY;}

for(k=0;k<G->e;k++) {printf("Input Vertex(e.g:i,j):\n"); scanf("%d,%d",&i,&j); printf("Input the edges of(%d,%d):\n",i,j); scanf("%d",&m); G->edges[i][j]=m; } /*初始化邻接矩阵*/

shortest_path(G,p,D); getch(); } 这是个图论问题, 整个程序都可以运行, 就是最后输出的那部分,我做不出来... 我那种输出好像错了. 就是只能输出两点或者是三点的最短路径, 无法输出四点,或者是五点.. 寻求解决之道...

[此贴子已经被作者于2005-5-22 11:16:49编辑过]

搜索更多相关主题的帖子: 弗洛伊德 int 图论 typedef pathMatrix 
2005-05-21 14:22
牛虻
Rank: 1
等 级:新手上路
威 望:1
帖 子:472
专家分:0
注 册:2004-10-1
收藏
得分:0 
给你个建议输出函数另外做,有可能要用递归

土冒
2005-05-21 20:57
tary
Rank: 1
等 级:新手上路
帖 子:780
专家分:0
注 册:2004-10-5
收藏
得分:0 

┌→¨ ≮我可以学会对你很冷落≯¨←┐ │  <却学不╓══╦══╖会将爱> │ │¨←┐ ╭╩╮哭‖哭╭╩╮ ┌→¨│ └──┘收 ╲╱ ◇‖◇ ╲╱回└──┘
2005-05-22 10:36
tary
Rank: 1
等 级:新手上路
帖 子:780
专家分:0
注 册:2004-10-5
收藏
得分:0 
UP

┌→¨ ≮我可以学会对你很冷落≯¨←┐ │  <却学不╓══╦══╖会将爱> │ │¨←┐ ╭╩╮哭‖哭╭╩╮ ┌→¨│ └──┘收 ╲╱ ◇‖◇ ╲╱回└──┘
2005-05-22 22:51
tary
Rank: 1
等 级:新手上路
帖 子:780
专家分:0
注 册:2004-10-5
收藏
得分:0 
此悬赏帖已经发了几天了, 没人回,看来我自己赚了..
因为偶已经得出来了.
将上面的/*输出*/
改为:
for(i=0;i&lt;G-&gt;n;i++)
for(j=0;j&lt;G-&gt;n;j++)
{if(p[i][j]&gt;=0)
  {w=j;
  printf("%-3d",D[i][j]);      printf("%c&lt;--",G-&gt;vexs[j]);
  while(p[i][w]!=i)
  {
  printf("%c&lt;--",G-&gt;vexs[p[i][w]]);w=p[i][w];
  }
 printf("%c\n",G-&gt;vexs[i]);
  }
}

┌→¨ ≮我可以学会对你很冷落≯¨←┐ │  <却学不╓══╦══╖会将爱> │ │¨←┐ ╭╩╮哭‖哭╭╩╮ ┌→¨│ └──┘收 ╲╱ ◇‖◇ ╲╱回└──┘
2005-05-23 17:12
快速回复:[求助]请教一个问题.->.图论!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012043 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved