声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2231|回复: 0

[经典算法] [求助]有没蝈蝈能帮我找一下用C或C++编的最短路加权算法啊。

[复制链接]
发表于 2006-3-23 16:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
<P ><B ><FONT size=3><FONT face=宋体>Floyd-Warshall算法:<BR> <BR>其源代码为:<p></p></FONT></FONT></B></P>
<P ><FONT face=宋体 size=3>#include&lt;stdio.h&gt;</FONT></P>
<P ><FONT face=宋体 size=3>#include&lt;stdlib.h&gt;</FONT></P>
<P ><FONT face=宋体 size=3>#include&lt;limits.h&gt;</FONT></P>
<P ><FONT face=宋体 size=3>#define  MAXSIZE 20</FONT></P>
<P ><FONT face=宋体 size=3>void  Floyd(int [][MAXSIZE],int [][MAXSIZE],int);</FONT></P>
<P ><FONT face=宋体 size=3>void  display_path(int [][MAXSIZE],int [][MAXSIZE],int);</FONT></P>
<P ><FONT face=宋体 size=3>void  reverse(int [],int);</FONT></P>
<P ><FONT face=宋体 size=3>void  readin(int [][MAXSIZE],int *);</FONT></P>
<P ><FONT face=宋体 size=3>#define MAXSUM(a,b) (((a)!=INT_MAX&amp;&amp;(b)!=INT_MAX)?((a)+(b)):INT_MAX)</FONT></P>
<P ><FONT face=宋体 size=3>void Floyd(int dist[][MAXSIZE],int path[][MAXSIZE],int n)</FONT></P>
<P ><FONT face=宋体 size=3>{ </FONT></P>
<P ><FONT face=宋体 size=3>int i,j,k;</FONT></P>
<P ><FONT face=宋体 size=3>for(i=0;i&lt;n;i++)</FONT></P>
<P ><FONT face=宋体 size=3>for(j=0;j&lt;n;j++)</FONT></P>
<P ><FONT face=宋体 size=3>path[j]=i;</FONT></P>
<P ><FONT face=宋体 size=3>for(k=0;k&lt;n;k++)</FONT></P>
<P ><FONT face=宋体 size=3>for(i=0;i&lt;n;i++)</FONT></P>
<P ><FONT face=宋体 size=3>for(j=0;j&lt;n;j++)</FONT></P>
<P ><FONT face=宋体 size=3>if(dist[j]&gt;MAXSUM(dist[k],dist[k][j]))</FONT></P>
<P ><FONT face=宋体 size=3>{</FONT></P>
<P ><FONT face=宋体 size=3>path[j]=path[k][j];</FONT></P>
<P ><FONT face=宋体 size=3>dist[j]=MAXSUM(dist[k],dist[k][j]);</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>
<P ><FONT face=宋体 size=3>void disp_path(int dist[][MAXSIZE],int path[][MAXSIZE],int n)</FONT></P>
<P ><FONT face=宋体 size=3>{</FONT></P>
<P ><FONT face=宋体 size=3>int *chain;</FONT></P>
<P ><FONT face=宋体 size=3>int count;</FONT></P>
<P ><FONT face=宋体 size=3>int i,j,k;</FONT></P>
<P ><FONT face=宋体 size=3>printf(“\n\norigin-&gt;Dest  Dist  Path”);</FONT></P>
<P ><FONT face=宋体 size=3>printf(“\n--------------  ----  ----”);</FONT></P>
<P ><FONT face=宋体 size=3>chain=(int *)malloc(sizeof(int)*n);</FONT></P>
<P ><FONT face=宋体 size=3>for(i=0;i&lt;n;i++)</FONT></P>
<P ><FONT face=宋体 size=3>for(j=0;j&lt;n;j++)</FONT></P>
<P ><FONT face=宋体 size=3>if(i!=j)</FONT></P>
<P ><FONT face=宋体 size=3>{</FONT></P>
<P ><FONT face=宋体 size=3>printf(“\n%6d-&gt;%4d”,i+1,j+1);</FONT></P>
<P ><FONT face=宋体 size=3>if(dist[j]==INT_MAX)</FONT></P>
<P ><FONT face=宋体 size=3>printf(“NA”);</FONT></P>
<P ><FONT face=宋体 size=3>else</FONT></P>
<P ><FONT face=宋体 size=3>{</FONT></P>
<P ><FONT face=宋体 size=3>printf(“%4d”,dist[j]);</FONT></P>
<P ><FONT face=宋体 size=3>count=0;</FONT></P>
<P ><FONT face=宋体 size=3>k=j;</FONT></P>
<P ><FONT face=宋体 size=3>do{ k=chain[count++]=path[k];}</FONT></P>
<P ><FONT face=宋体 size=3>while(i!=k);</FONT></P>
<P ><FONT face=宋体 size=3>reverse(chain,count);</FONT></P>
<P ><FONT face=宋体 size=3>printf(“%d”,chain[0]+1);</FONT></P>
<P ><FONT face=宋体 size=3>for(k=1;k&lt;count;k++)</FONT></P>
<P ><FONT face=宋体 size=3>printf(“-&gt;%d”,chain[k]+1);</FONT></P>
<P ><FONT face=宋体 size=3>printf(“-&gt;%d”,j+1);</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>
<P ><FONT face=宋体 size=3>free(chain);</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>
<P ><FONT face=宋体 size=3>#define SWAP(a,b)</FONT></P>
<P ><FONT face=宋体 size=3>{temp=a;a=b;b=temp;}</FONT></P>
<P ><FONT face=宋体 size=3>void reverse(int x[],int n)</FONT></P>
<P ><FONT face=宋体 size=3>{</FONT></P>
<P ><FONT face=宋体 size=3>int i,j,temp;</FONT></P>
<P ><FONT face=宋体 size=3>for(i=0,j=n-1;i&lt;j;i++,j--)</FONT></P>
<P ><FONT face=宋体 size=3>SWAP(x,x[j]);</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>
<P ><FONT face=宋体 size=3>void readin(int dist[][MAXSIZE],int *number)</FONT></P>
<P ><FONT face=宋体 size=3>{</FONT></P>
<P ><FONT face=宋体 size=3>int origin,dest,length,n;</FONT></P>
<P ><FONT face=宋体 size=3>int i,j;</FONT></P>
<P ><FONT face=宋体 size=3>char line[100];</FONT></P>
<P ><FONT face=宋体 size=3>gets(line);</FONT></P>
<P ><FONT face=宋体 size=3>sscanf(line,”%d”,&amp;n);</FONT></P>
<P ><FONT face=宋体 size=3>*number=n;</FONT></P>
<P ><FONT face=宋体 size=3>for(i=0;i&lt;n;i++)</FONT></P>
<P ><FONT face=宋体 size=3>{</FONT></P>
<P ><FONT face=宋体 size=3>for(j=0;j&lt;n;j++)</FONT></P>
<P ><FONT face=宋体 size=3>dist[j]=INT_MAX;</FONT></P>
<P ><FONT face=宋体 size=3>dist=0;</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>
<P ><FONT face=宋体 size=3>gets(line);</FONT></P>
<P ><FONT face=宋体 size=3>sscanf(line,”%d%d%d”,&amp;origin,&amp;dest,&amp;length);</FONT></P>
<P ><FONT face=宋体 size=3>while(origin!=0&amp;&amp;dest!=0&amp;&amp;length!=0)</FONT></P>
<P ><FONT face=宋体 size=3>{</FONT></P>
<P ><FONT face=宋体 size=3>dist[origin-1][dest-1]=length;</FONT></P>
<P ><FONT face=宋体 size=3>gets(line);</FONT></P>
<P ><FONT face=宋体 size=3>sscanf(line,”%d%d%d”,&amp;origin,&amp;dest,&amp;length);</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>
<P ><FONT face=宋体 size=3>}</FONT></P>}<BR><BR>谁能帮我写一个主函数,这是没加权的,不知道加权的怎么修改。<BR>谢谢哈
回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-7 12:44 , Processed in 0.136864 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表