Пример #1
0
/*弗洛伊德算法求最短路径*/
 void Floyd( MGraph * mg )
 {
	 int i, j, k;
	 int A[MAXV][MAXV], path[MAXV][MAXV];
	 for( i = 0; i < mg->n; i ++ )	/*初始化*/
		 for( j = 0; j < mg->n; j ++ )
		 {
			 A[i][j] = mg->edges[i][j];
			 path[i][j] = -1;
		 }

	 for( k = 0; k < mg->n; k ++ )
		 for( i = 0; i < mg->n; i ++ )
			 for( j = 0; j < mg->n; j ++ )
				 if( A[i][j] > A[i][k] + A[k][j] )
				 {
					 A[i][j] = A[i][k] + A[k][j];
					 path[i][j] = k;
				 }
	

	Dispath( A, path, mg->n );
 }
Пример #2
0
int main()
{
    int ncity,nrode;  
    while(scanf("%d%d",&ncity,&nrode)!=EOF)
    { 
    int a[4000][5];
    char b[4000];
    char c[4000];    
    int i,j;    
    for(i=0;i<nrode;i++)
    {
          for(j=0;j<5;j++)
          {
                  scanf("%d",&a[i][j]);
          } 
          scanf("%s",&b[i]);
    }
    int pl,sl;                
    scanf("%d%d",&pl,&sl);          
    int n,i1,j1,k1;
    int G[100][100];
    int A[100][100],path[100][100];
    for(i1=0;i1<ncity;i1++)
        for(j1=0;j1<ncity;j1++)
        { 
              A[i1][j1]=INF; 
              G[i1][j1]=INF; 
        } 
    for(n=0;n<nrode;n++)
    {      
        int p,q;
        p=a[n][0];
        q=a[n][1]; 
        G[p][q]=a[n][4]; 
        str[p][q]=b[n]; 
    }                        
    for(i1=0;i1<ncity;i1++)
        for(j1=0;j1<ncity;j1++)
        {
              A[i1][j1]=G[i1][j1];
              path[i1][j1]=-1;
        }
    for(n=0;n<nrode;n++)
    {                    
        int p,q;
        p=a[n][0];
        q=a[n][1]; 
        if(a[n][2]>pl||a[n][3]>sl)
        {
              A[p][q]=INF;
        }
    }
    for(k1=0;k1<ncity;k1++)
    {
        for(i1=0;i1<ncity;i1++)
              for(j1=0;j1<ncity;j1++)
                  if(A[i1][j1]>A[i1][k1]+A[k1][j1])
                  {                                                              
                          A[i1][j1]=A[i1][k1]+A[k1][j1];
                          path[i1][j1]=k1;                                                            
                  }
    }
    Dispath(A,path,ncity); 
    } 
    return 0;
}