您的位置:首页 > 其它

有向图的传递闭包,两点间最短路径的Floyd算法的变化.

2010-05-20 11:33 791 查看
#include <stdlib.h>
#include <stdio.h>
#define No -1

struct Graph
{
int **edge;
int numV;
};

void Floyd_Warshall(Graph &g,bool **t)
{
for(int i=0;i<g.numV;++i)
{
for(int j=0;j<g.numV;++j)
{
if(g.edge[i][j]!=No)
{
t[i][j]=true;
}
else
{
t[i][j]=false;
}
}
}

for(int k=0;k<g.numV;++k)
{
for(int m=0;m<g.numV;++m)
{
for(int n=0;n<g.numV;++n)
{

t[m]
=(t[m][k]&&t[k]
)||t[m]
;
}
}
}
}

int main()
{
Graph g;
scanf("%d",&g.numV);
bool **t;
t=(bool**)malloc(sizeof(bool*)*g.numV);
g.edge=(int**)malloc(sizeof(int*)*g.numV);
for(int i=0;i<g.numV;++i)
{
t[i]=(bool*)malloc(sizeof(bool)*g.numV);
g.edge[i]=(int*)malloc(sizeof(int)*g.numV);
for(int j=0;j<g.numV;++j)
{
printf("%d,%d的dist:",i,j);
scanf("%d",&g.edge[i][j]);
}
}
Floyd_Warshall(g,t);
for(int m=0;m<g.numV;++m)
{
for(int n=0;n<g.numV;++n)
{
if(t[m]
==true)
{
printf("%d,%d,可以连通!/n",m,n);
}
}
}
return 0;
}


 

 

每次加入一个新的中间点,判断能否连通.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 graph include struct