5-9 旅游规划

点击打开链接

有权图单源路径最小值

迪杰斯特拉算法改一下

在路径长度最小的情况下选择费用最少的边

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<vector>
#include<cstring>
#include<math.h>
#include<stack>
#include <string>
using namespace std;
const int  MAXINT = 32767;

int N,M,S,D;
int dist[505],cost[505],money[505][505],prevd[505];
int c[505][505];
int s[505]={0};
int main()
{
    cin>>N>>M>>S>>D;
    int a,b,x,y;
    for(int i=0;i<N;i++)
    {
        dist[i]= MAXINT;
        cost[i]= 0;
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            c[i][j]= MAXINT;
        }
    }
    for(int i=0;i<M;i++)
    {
        cin>>a>>b>>x>>y;
        c[a][b]= x;
        c[b][a]= x;
        money[a][b]= y;
        money[b][a]= y;
    }
    for(int i=0;i<N;i++)
    {
        dist[i]= c[S][i];
        cost[i]= money[S][i];
        if(dist[i]== MAXINT)
            prevd[i]=0;
        else
            prevd[i]=S;
    }
    dist[S]=0;
    s[S]=1;
    for(int i=1;i<N;i++)
    {
        int temp=MAXINT;
        int u=S;
        for(int j=0;j<N;j++)
        {
            if(!s[j] && dist[j]<temp)
            {
                temp=dist[j];
                u=j;
            }
        }
        s[u]=1;
        for(int j=0; j<N; j++)
        {
            if( !s[j] && c[u][j]<MAXINT)
            {
                int newdist=dist[u] +c[u][j];
                if(newdist < dist[j] )
                {
                    dist[j]= newdist;
                    prevd[j]= u;
                    cost[j]= cost[u]+money[u][j];
                }
                else if(newdist == dist[j] &&  cost[u]+money[u][j]< cost[j])
                {
                    cost[j]= cost[u]+money[u][j];
                }
            }
        }

    }

    cout<<dist[D]<<" "<<cost[D];
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值