| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1973 人关注过本帖
标题:最短路径!!(帮忙改一下,有一个错误,我改不出来!!)
只看楼主 加入收藏
101101a
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-12-21
收藏
 问题点数:0 回复次数:11 
最短路径!!(帮忙改一下,有一个错误,我改不出来!!)
#define INFINITY 10000
#define MAX_VERTEX_NUM 32
#define MAX_ARC_NUM 50
#include<stdio.h>
#include<string.h>

typedef struct{
    int v;
    char city[20];
}VertexType;

typedef struct {
    VertexType vex[MAX_VERTEX_NUM];
    int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
    int vexnum,arcnum;
}MGraph;

typedef int ShortPathTable[MAX_VERTEX_NUM];
typedef int PathMatrix[MAX_VERTEX_NUM];


void CreateDG(MGraph &G){
        int i,j;
        char string[][20]={"Beijing","Haerbin","Changchun","Shenyang","Tianjin","Dalian","HHHT","Yinchuan",
                       "WLMQ","Xining","Lanzhou","Xi'an","Taiyuan","Zhengzhou","Xuzhou","Shanghai",
                       "Nanjing","Hefei","Wuchang","Changsha","Chongqinh","Chengdu","Kunming","Guiyang",
                       "Zhuzhou","Nanchang","Hangzhou","Fuzhou","Shenzhen","Guangzhou","Liuzhou","Nanning"
                      };
        G.vexnum=MAX_VERTEX_NUM;
        G.arcnum=MAX_ARC_NUM;

        for(i=0;i<G.vexnum;++i)
            for(j=0;j<G.vexnum;++j)
                G.arcs[i][j]=INFINITY;

       for(i=0;i<MAX_VERTEX_NUM;i++)strcpy(G.vex[i].city,string[i]);
       
    FILE *fp;
    char filename[10];
    int ch;
    fp=fopen("graph.dat","r");
    ch=fgetc(fp)-48;
    while(!feof(fp)){
        int i=0,j=0;
        
        if(ch!=0)i=10*ch;
        ch=fgetc(fp)-48;
        i=i+ch;

        ch=fgetc(fp)-48;
        if(ch!=0)j=10*ch;
        ch=fgetc(fp)-48;
        j=j+ch;

        G.arcs[i][j]=0;
        ch=fgetc(fp)-48;
        if(ch!=0)G.arcs[i][j]=1000*ch;
        ch=fgetc(fp)-48;
        G.arcs[i][j]+=100*ch;
        ch=fgetc(fp)-48;
        G.arcs[i][j]+=10*ch;
        ch=fgetc(fp)-48;
        G.arcs[i][j]+=ch;
        ch=fgetc(fp)-48;
    }

      
}

   

void ShortestPath_DIJ(MGraph G,int v0,PathMatrix &P,ShortPathTable &D){
    int v,w,i,min;
    bool final[MAX_VERTEX_NUM];    

    for(w=0;w<MAX_VERTEX_NUM;w++)P[w]=-1;
    for(v=0;v<G.vexnum;++v){
        final[v]=false;
        D[v]=G.arcs[v0][v];
        if(D[v]<INFINITY)P[v]=v0;    
    }

    D[v0]=0;
    final[v0]=true;
    for(i=1;i<G.vexnum;++i){
        min=INFINITY;
        for(w=0;w<G.vexnum;++w)
            if(!final[w])
                if(D[w]<min){
                    v=w;
                    min=D[w];
                }

         final[v]=true;

         for(w=0;w<G.vexnum;++w)
             if(!final[w]&&(min+G.arcs[v][w]<D[w])){
                 D[w]=min+G.arcs[v][w];
                 P[w]=v;
                
             }
    }
}

void Output_ShortestPath(MGraph G,int v0,int vn,PathMatrix P,ShortPathTable D){
    printf("from(%d)%sto(%d)%sthe shortest line%dkm。\n\t",v0,G.vex[v0].city,vn,G.vex[vn].city,D[vn]);
    printf("zhe shortest line is:");
    
    int t;
    int i;
    int a[MAX_VERTEX_NUM];
    printf("%s-->",G.vex[v0].city);
    for(i=0;i<MAX_VERTEX_NUM;i++)a[i]=-1;
    
    for(t=P[vn],i=MAX_VERTEX_NUM-1;(t!=-1)&&(t!=v0);i--){
        a[i]=t;
        t=P[t];
    }

    for(i=0;i<MAX_VERTEX_NUM;i++){
        if(a[i]==-1)i++;
        else printf("%s-->",G.vex[a[i]].city);
    }
        printf("%s",G.vex[vn].city);
}



void main()
{
    int v0,vn,ch=1;
    ShortPathTable D;
    PathMatrix P;
    MGraph G;
    printf("  \t\t\tmap is aready loaded graph relative city");
    printf("\n ****************************************************************************\n");
    printf("\t0Beijing\t1Haerbin\t2Changchun\t3Shenyang\n\t4Tianjin\t5Dalian\t6HHHT\t7Yinchuan\n\t8WLMQ\t9Xining\t10Lanzhou\t11Xian\n\t12Taiyuan\t13Zhengzhou\t14Xuzhou\t15Shanghai\n\t16Nanjing\t17Hefei\t18Wuchang\t19Changsha\n\t20Chongqinh\t21Chengdu\t22Kunming\t23Guiyang\n\t24Zhuzhou\t25Nanchang\t26Hangzhou\t27Fuzhou\n\t28Shenzhen\t29Guangzhou\t30Liuzhou\t31Nanning\n ");
       printf("****************************************************************************\n");
    printf("\n");
   
    CreateDG(G);
    
    while(ch){    
        printf("input zhe num of beginning city");  
        scanf("%d",&v0);
        printf("input zhe num of ending city");   
        scanf("%d",&vn);    
        ShortestPath_DIJ(G,v0,P,D);
        printf("\n****************************************************************************\n");
        Output_ShortestPath(G,v0,vn,P,D);
        printf("\n*****************************************************************************\n");

        printf("\n\n");   
        printf("1 for continue\t0 for exit");    
        printf("\n");    
        scanf("%d",&ch);
    
    }

    
}
搜索更多相关主题的帖子: int 路径 typedef MAX 
2008-01-12 12:46
101101a
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-12-21
收藏
得分:0 
错误是第17行的      void CreateDG(MGraph &G){

2008-01-12 13:33
101101a
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-12-21
收藏
得分:0 
大家帮帮忙啊!!!

2008-01-12 14:40
linsua
Rank: 1
等 级:新手上路
帖 子:275
专家分:0
注 册:2008-1-2
收藏
得分:0 
void CreateDG(MGraph &G){

怎么看起来像C呢?
C++里面才有引用

如果你对我的话感到再明白不过 那恐怕你是误解了我的意思
2008-01-12 15:59
101101a
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-12-21
收藏
得分:0 
是C啊!!!难道这样写不对吗!

2008-01-12 16:18
linsua
Rank: 1
等 级:新手上路
帖 子:275
专家分:0
注 册:2008-1-2
收藏
得分:0 
void CreateDG(MGraph &G){        -》》》   void CreateDG(MGraph *G)   
相应的再改改,

PS:难不成你用的是严蔚敏的数据结构?TNND,C语言里没有引用!

如果你对我的话感到再明白不过 那恐怕你是误解了我的意思
2008-01-12 16:23
101101a
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-12-21
收藏
得分:0 
我用TC2。0他显示就是那个地方错误!!不对吗写的??怎么改呢??

2008-01-12 16:27
linsua
Rank: 1
等 级:新手上路
帖 子:275
专家分:0
注 册:2008-1-2
收藏
得分:0 
我在再说一遍:void CreateDG(MGraph &G)      -》》》   void CreateDG(MGraph *G)

如果你对我的话感到再明白不过 那恐怕你是误解了我的意思
2008-01-12 16:31
101101a
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-12-21
收藏
得分:0 
OK!!谢谢!!!回去试试!!

2008-01-12 16:34
101101a
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2006-12-21
收藏
得分:0 
不行啊!!按照你说的也不对!!!

2008-01-13 12:43
快速回复:最短路径!!(帮忙改一下,有一个错误,我改不出来!!)
数据加载中...
 
   



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

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