07-图6 旅游规划

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

输入格式:

输入说明:输入数据的第1行给出4个正整数NMSD,其中N2N500)是城市的个数,顺便假设城市的编号为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
//
//  main.cpp
//  7-3 旅游规划
//
//  Created by 佘一夫 on 11/8/15.
//  Copyright © 2015 佘一夫. All rights reserved.
//

#include <iostream>
using namespace std;
#define MaxVertexNum 1000
#define Maxe 130000

typedef struct Graph *MGraph;
struct Graph{
    int A[MaxVertexNum][MaxVertexNum];
    int Cost[MaxVertexNum][MaxVertexNum];
    int Nv;
    int Ne;
};

MGraph CreateGraph(int Nv,int Ne)
{
    MGraph G=new(Graph);
    G->Ne=Ne;
    G->Nv=Nv;
    for (int i=0; i<Nv; ++i)
    {
        for (int k=0; k<Nv; ++k)
        {
            if (i==k)
            {
                G->A[i][k]=0;
            }
            G->A[i][k]=Maxe;
        }
    }
    return  G;
}

MGraph BuildGraph(MGraph Graph,int B[4][Maxe])
{
    for (int i=0; i<Graph->Ne; ++i)
    {
        Graph->A[(B[0][i])][(B[1][i])]=B[2][i];
        Graph->A[(B[1][i])][(B[0][i])]=B[2][i];
        Graph->Cost[(B[0][i])][(B[1][i])]=B[3][i];
        Graph->Cost[(B[1][i])][(B[0][i])]=B[3][i];
        
    }
    return Graph;
}



void Dijkstra(MGraph G,int V0,int P[],int D[],int Cost_1[])
{
    int Final[MaxVertexNum],v,w,i,min;
    for (v=0; v<G->Nv; ++v)
    {
        Final[v]=0;
        D[v]=G->A[V0][v];
        for (w=0; w<G->Nv; ++w)
        {
            if (D[w]<Maxe)
            {
                P[w]=V0;
                Cost_1[w]=G->Cost[V0][w];
            }
        }
    }
    D[V0]=0;
    Final[V0]=1;
    
    for (i=1; i<G->Nv; ++i)
    {
        min=Maxe;
        for (w=0; w<G->Nv; ++w)
        {
            if (!Final[w])
            {
                if (D[w]<min)
                {
                    min=D[w];
                    v=w;
                }
            }
        }
        
        if (min<Maxe)
        {
            Final[v]=1;
        }else
        {
            break;
        }
        
        for (w=0; w<G->Nv; ++w)
        {
            if (!Final[w]&&(min+(G->A[v][w])<D[w]))
            {
                D[w]=(min+G->A[v][w]);
                P[w]=v;
                Cost_1[w]=Cost_1[v]+G->Cost[v][w];
            }else if(!Final[w]&&(min+(G->A[v][w])==D[w])&&(Cost_1[v]+G->Cost[v][w]<Cost_1[w]))
            {
                P[w]=v;
                Cost_1[w]=Cost_1[v]+G->Cost[v][w];
            }
        }
    }
}


int main()
{
    int N=0,M=0,S=0,D=0;
    cin>>N>>M>>S>>D;
    int A[4][Maxe];
    for (int i=0; i<M; ++i)
    {
        cin>>A[0][i]>>A[1][i]>>A[2][i]>>A[3][i];
    }
    MGraph Graph=CreateGraph(N, M);
    BuildGraph(Graph, A);
    int Cost_1[MaxVertexNum]={};
    int R[MaxVertexNum]={};
    int P[MaxVertexNum]={};
    
    for (int i=0; i<=Graph->Nv; ++i)
    {
        R[i]=Maxe;
        Cost_1[i]=Maxe;
    }
    
    Dijkstra(Graph, S, P, R, Cost_1);
    cout<<R[D]<<' '<<Cost_1[D]<<endl;
    
   
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值