Dijkstra算法 pat1030 Travel Plan

1 篇文章 0 订阅

Description

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input

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

Sample Output

0 2 3 3 40

Source

pat 1030 Travel Plan 

 

  • Dijkstra用于求单源最短路径问题(求从源点到其他各个点的最短距离),且路径的权值不能为负数(负值情况用Bellman-Ford算法)。

  • 算法核心思路:每次1.从选择槽中选出能到达的最近的未占领的点(共选n次,n为总点数),2.占领该点,3.同时计算并更新选择槽,(此过程中选择槽要么多出几个可以到达的未占领的点,要么更新能到达的未占领点更短的距离),重复1、2、 3。

  • 数据结构:vis[]来区分是否占领过,d[]来保存从源点到各个点的最短距离(选择槽所记录的值),G[][]存图(各点之间的距离)。

  • 此类问题变法往往是在最短路径的基础上再多出些其他权衡标准,也就是当有多条最短路径时,从中选出在其他标准下最优那条路径。

  • 对于这些变法题有两种解决方向,一是在上面的操作3中求最短路径的同时直接讨论其他权衡标准(如下面解法一)。二是先记录下所有的最短路径再对这些路径进行讨论(如下面解法二)。

解法一:直接Dijkstra。DFS是用来打印路径。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXV = 500;
const int INF = 1000000000;

int n;
int G[MAXV][MAXV], cost[MAXV][MAXV], pre[MAXV];
int d[MAXV], c[MAXV];
bool vis[MAXV] = {false};

//G为距离矩阵,cost为开销矩阵,pre记录最优路径上某点的前一项,d[]记录最短距离,c[]记录最小花销。

void Dijkstra(int st){
    fill(d, d+MAXV, INF);
    fill(c, c+MAXV, INF);//要初始化为无穷大。
    for(int i = 0; i < n; i++) pre[i] = i;//初始化不能漏!
    d[st] = 0;
    c[st] = 0;
    for(int i = 0; i < n; i++){
        int min = INF, u = -1;
        for(int j = 0; j < n; j++){
            if(!vis[j] && d[j] < min){
                min = d[j];
                u = j;
            }
        }

        if(u == -1)
            return;
        vis[u] = true;

        for(int j = 0; j < n; j++){
            if(!vis[j] && G[u][j] != INF){
                if(d[u]+G[u][j] < d[j]){
                    d[j] = d[u]+G[u][j];
                    c[j] = c[u]+cost[u][j];
                    pre[j] = u;
                } else if(d[u]+G[u][j] == d[j] && c[u] + cost[u][j] < c[j]){
                    pre[j] = u;
                    c[j] = c[u] + cost[u][j];
                }
            }
        }
    }

}

void DFS(int st, int de){
    if(st == de){
        printf("%d ", st);
        return;
    }
    else{
        DFS(st, pre[de]);
        printf("%d ", de);
    }
}

int main(){
    int m, st, de;
    scanf("%d%d%d%d",&n, &m, &st, &de);
    fill(G[0], G[0]+MAXV*MAXV, INF);
    fill(cost[0], cost[0]+MAXV*MAXV, INF);
    int u, v;
    for(int i = 0; i < m; i++){
        scanf("%d%d", &u, &v);
        scanf("%d%d", &G[u][v], &cost[u][v]);
        G[v][u] = G[u][v];
        cost[v][u] = cost[u][v];
    }
    Dijkstra(st);
    DFS(st, de);
    printf("%d %d", d[de], c[de]);
    return 0;
}

解法二:Dijkstra+DFS。分开求解,更好处理复杂的多维尺度。

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

const int MAXV = 500;
const int INF = 1000000000;


bool vis[MAXV] = {false};
int n, m, st, de, G[MAXV][MAXV], cost[MAXV][MAXV];
vector<int> pre[MAXV], tempPath, path;//pre无需初始化。
int d[MAXV], optCost = INF;

//G为距离矩阵,cost为开销矩阵,pre记录最优路径上某点的前一项,
//d[]记录最短距离,optCost记录最小花销, tempPath为临时路径,path为最优路径。

void Dijkstra(int s){
    fill(d, d+MAXV, INF);
    d[s] = 0;
    for(int i = 0; i < n; i++){
        int min = INF, u = -1;
        for(int j = 0; j < n; j++){
            if(!vis[j] && d[j] < min){
                min = d[j];
                u = j;
            }
        }
        if(u == -1) return;
        vis[u] = true;
        for(int v = 0; v < n; v++){
            if(!vis[v] && G[u][v] != INF){
                if(d[v] > d[u]+G[u][v]){
                    pre[v].clear();
                    pre[v].push_back(u);
                    d[v] = d[u]+G[u][v];
                }
                else if(d[v] == d[u]+G[u][v])
                    pre[v].push_back(u);
            }
        }
    }
}


void DFS(int v){
   if(st == v){
       tempPath.push_back(v);
       int totalCost = 0;
       for(int i = tempPath.size()-1; i > 0; i--){
           int id = tempPath[i], idNext = tempPath[i-1];
           totalCost += cost[id][idNext];
       }
       if(totalCost < optCost){
           optCost = totalCost;
           path = tempPath;
       }
       tempPath.pop_back();
       return;
   }
   tempPath.push_back(v);
   for(int i = 0; i < pre[v].size(); i++){
       DFS(pre[v][i]);
   }
   tempPath.pop_back();
}

int main(){
    scanf("%d%d%d%d",&n, &m, &st, &de);
    fill(G[0], G[0]+MAXV*MAXV, INF);
    fill(cost[0], cost[0]+MAXV*MAXV, INF);
    int u, v;
    for(int i = 0; i < m; i++){
        scanf("%d%d", &u, &v);
        scanf("%d%d", &G[u][v], &cost[u][v]);
        G[v][u] = G[u][v];
        cost[v][u] = cost[u][v];
    }
    Dijkstra(st);
    DFS(de);
    for(int i = path.size()-1; i >= 0; i--){
        printf("%d ", path[i]);
    }
    printf("%d %d", d[de], optCost);
    return 0;
}

tip:

  • 有向图和无向图初始化问题;源点的初始化不能漏;
  • int最大约为2*10^9,一般设无穷大INF为10^9;
  • memset()是以字节为单位作初始化;
  • fill()对图初始化是G[0]开始而不是G;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值