5-9 旅游规划 (25分)

5-9 旅游规划 (25分)

有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式:

输入说明:输入数据的第1行给出4个正整数NMSD,其中NNN( 2N500 )是城市的个数,顺便假设城市的编号为0~( N1 );M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。
输出格式:

在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。
输入样例:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

输出样例:

3 40

思路
关键词:单源最短路
点击访问 PTA-测验

#include <stdio.h>
#include<stdlib.h>
#define FULL 250001
/* 评测结果 
   时间               结果  得分  题目  编译器     用时(ms)  内存(MB)  用户
2016-04-15 16:15    答案正确    25  07-图6   gcc     77  3   569985011
测试点结果    
测试点           结果        得分/满分   用时(ms)内存(MB)    要点提示
测试点1    答案正确      12/12         2       1       sample 最便宜的路不是最短路;输出2条最短路中最便宜的
测试点2    答案正确       5/5          1       1       最小N和M
测试点3    答案正确       4/4          4       3       最大N,有多条等长等价的道路
测试点4    答案正确       4/4          77      3       最大N和M, 随机完全图
*/
typedef struct LNode *Vertex;
typedef struct data {
    int Lenth_Data,Coast_Data;
} DataType;

void Floyd_Warshall(DataType V[][500],int n);
void dijkstra(DataType V[][500],int from,int to ,int n);

int main() {
    int n,m,from,to;
    DataType V[500][500];
    scanf("%d%d%d%d",&n,&m,&from,&to);
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
//      V[i][j].Coast_Data =FULL;
            V[i][j].Lenth_Data =FULL;
        }
    }
    int disk1,disk2;

    for(int i=0; i<m; i++) {
        scanf("%d%d",&disk1,&disk2);
        scanf("%d%d",&V[disk1][disk2].Lenth_Data ,&V[disk1][disk2].Coast_Data );
        V[disk2][disk1].Lenth_Data =V[disk1][disk2].Lenth_Data;
        V[disk2][disk1].Coast_Data =V[disk1][disk2].Coast_Data;
//  if(disk1==from||disk2==from){
//      dis[right++]=disk1+disk2-from;
//  }
    }
//  Floyd_Warshall(V,n);
    dijkstra( V,from, to ,n);





    if(V[from][to].Lenth_Data <FULL)printf("%d %d",V[from][to].Lenth_Data ,V[from][to].Coast_Data );
    else printf("0 0");
    return 0;
}
void dijkstra(DataType V[][500],int from,int to ,int n) {
    int book[500]= {0};
    book[from]=1;
    while(1) {
        int flag=0,find;
        for(int i=0; i<n; i++) {
            if(V[from][i].Lenth_Data<FULL&&!book[i]) {
                if(flag) {
                    int lenth=V[from][i].Lenth_Data;
                    if(lenth<V[from][find].Lenth_Data||lenth==V[from][find].Lenth_Data&&
                            V[from][i].Coast_Data<V[from][find].Coast_Data)find=i;
                } else {
                    find=i;
                    flag=1;
                }
            }
        }
        if(!flag||find==to)break;

        for(int i=0; i<n; i++) {
            if(V [find][i].Lenth_Data<FULL) {
                int lenth=V[from][find].Lenth_Data+V[find][i].Lenth_Data;
                if(V[from][i].Lenth_Data>lenth||V[from][i].Lenth_Data==lenth
                        &&V[from][i].Coast_Data>V[find][i].Coast_Data+V[from][find].Coast_Data
                  ) {
                    V[from][i].Lenth_Data=lenth;
                    V[from][i].Coast_Data=V[from][find].Coast_Data+V[find][i].Coast_Data;
                }
            }
        }
        book[find]=1;
    }

}
void Floyd_Warshall(DataType V[][500],int n) {
    for(int k=0; k<n; k++) {
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                int lenth=V[i][k].Lenth_Data +V[k][j].Lenth_Data;
                int coast=V[i][k].Coast_Data +V[k][j].Coast_Data ;
                if(V[i][j].Lenth_Data > lenth||V[i][j].Lenth_Data ==lenth&&V[i][j].Coast_Data >coast) {
                    V[i][j].Lenth_Data =lenth;
                    V[i][j].Coast_Data =coast;
                }
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值