[数据结构]求两点之间的最短路径 解题报告

Problem Description

最短路径问题是经典图论问题之一。从工程意义上讲,最短路径问题是对大量工程问题的直观抽象。
最典型的例子是在地图上寻找最短驾车路径。

《[数据结构]求两点之间的最短路径 解题报告》 short

寻找从A到D的最短路径。

测试输入

5,7
A,B,C,E,D
<0,3,30>,<0,1,10>,<0,2,20>,<1,3,10>,<1,2,5>,<2,4,30>,<3,4,20>

测试输出

A-B-E-D

AcCode

//
//  main.cpp
//  求两点之间的最短路径
//
//  Created by jetviper on 2017/3/26.
//  Copyright © 2017年 jetviper. All rights reserved.
//

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define INFINITY 99999
int n,b,k,t;
struct{
    char name[10];
    int bnum;
    int linkto[5000];
    int value[5000];
}node[600];
char temp[5000],road[5000][5000];
int D[5000];
void scanfb(char *temp,int f,int *get){
    char s1[20],s2[20],s3[20];
    int p,s,c;
    int len = strlen(temp);
    if(temp[f]=='<'){
        p=0;
        for(int j=f+1;j<len;j++){
            if(temp[j]!=','){
                s1[p++]=temp[j];
            }
            else{
                s1[p]='\0';
                s=j;
                break;
            }
        }
        p=0;
        for(int j=s+1;j<len;j++){
            if(temp[j]!=','){
                s2[p++]=temp[j];
            }
            else{
                s2[p]='\0';
                c=j;
                break;
            }
        }
        p=0;
        for(int j=c+1,p=0;j<len;j++){
            if(temp[j]!=','){
                s3[p++]=temp[j];
            }
            else{
                s3[p]='\0';
                break;
            }
        }
    }
    get[0]=atoi(s1);
    get[1]=atoi(s2);
    get[2]=atoi(s3);
}


void Dij(int now){
    int list[500];
    int count=0;
    
    for(int i=0;i<node[now].bnum;i++){
        
        if(D[node[now].linkto[i]]>D[now]+node[now].value[i]){
            char tmpstr[500];
            strcpy(tmpstr,road[now]);
            strcat(tmpstr,node[now].name);
            strcat(tmpstr,"-");
            strcpy(road[node[now].linkto[i]],tmpstr);
            D[node[now].linkto[i]] = D[now] + node[now].value[i];
            list[count++] = node[now].linkto[i];
        }
    }
    for(int i=0;i<count;i++){
        Dij(list[i]);
    }
}



int main() {
    char str[500];
    int get[6];
    
    scanf("%d,%d",&n,&b);
    
    for(int i=0;i<n;i++){
        D[i]=INFINITY;
        node[i].bnum=0;
    }
    
    
    scanf("%s",str);
    k=0;
    int q=0;
    for(int i=0;i<strlen(str);i++){
        if(str[i]!=','){
            node[k].name[q++]=str[i];
        }
        else {
            node[k].name[q]='\0';
            k++;
            q=0;
        }
        
    }
    
    scanf("%s",temp);
    for(int i=0;i<strlen(temp);i++){
        if(temp[i]=='<'){
            scanfb(temp,i,get);
            node[get[0]].value[node[get[0]].bnum] = get[2];
            node[get[0]].linkto[node[get[0]].bnum++] = get[1];
        }
    }
    //startSearching
    
    D[0]=0;
    Dij(0);
    
    if(D[n-1]<INFINITY){
        
        printf("%s",road[n-1]);
        
        printf("%s\n",node[n-1].name);
    }
    
    return 0;
}
    原文作者:monigo
    原文地址: https://www.jianshu.com/p/195b4b946f88
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞