【PAT数据结构与算法题目集】 旅游规划(单源最短路径,长度+路径查找)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hh66__66hh/article/details/83822673

【PAT数据结构与算法题目集】 旅游规划(单源最短路径,长度+路径查找)

题目

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

输入格式:
输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);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

思路

这道题的难点在于两个结点之间的最短路径可能不止一条,这时需要将所有的最短路径记录下来,并从中选取花费最少的最短路径。这题的关键有两个:

1. 对dijkstra算法的修改
因为可能有不止一条最短路径,每个结点的最短路径前驱结点也可能不止一个,因此需要对传统的dijkstra算法进行修改:
(1)用个vector类型的parent来存储某个结点的前驱结点集合, p a r e n t [ i ] parent[i] 存储了i的最短路径前驱结点集合;
(2)边的松弛需要做调整:假设源点为S,从源点S到结点u的最短路径已经确定后,在对一条边(u, w)进行松弛时,将从S到u之间的最短路径长度加上从结点u到结点w之间的距离,与当前的从S到w的最短路径估计进行比较:
1) 如果前者更小,则将后者更新为前者,清空 p a r e n t [ w ] parent[w] , 并将结点u加入 p a r e n t [ w ] parent[w]
2)如果两者相等,则将结点u加入 p a r e n t [ w ] parent[w]
该过程伪代码如下:

// 对边(u, w)进行松弛
// sw[k]表示从源点S到结点k的最短路径估计
//matrix[i][j]表示图中从结点i到结点j的距离
//vector<int>parent, parent[i]表示结点i的最短路径前驱结点集合

void rexlax(int u, int w) {
	if(sw[w] > sw[u] + matrix[u][w]) {
		sw[w] = sw[u] + matrix[u][w];
		parent[w].clear();
		parent[w].push_back(u);
	}
	else if(sw[w] == sw[u] + matrix[u][w]) {
		parent[w].push_back(u);
	}
}

2. 最短路径路径查找
为了找出花费最少的最短路径,所以要将每条最短路径找出来,这里可以用递归,其伪代码如下:

// s是源点,i是当前的结点
//parent[i]存储了结点i的最短路径前驱结点集合
//用vector<int>temp存储当前找出的这条最短路径上的每条边的长度
//mini_cost目前得到的最小花费,初始值为无穷大
//pay[i][j]表示城市i和城市j之间的收费
void cost(int i, int s) {
	if(i == s) {
		ans = temp中的每条边长度之和;
		if(ans < mini_cost) {
			mini_cost = ans;
		}
	}
	else {
		for(j=0; j<parent[i].size(); j++) {
			temp.push_back(pay[i][parent[i][j]);
			cost(parent[i][j], s);
			temp.pop_back();
		}
	}
}

代码

#include <iostream>
#include <stdio.h>
#include <string>
#include<string.h>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;

int graph[505][505];
int path[505][505];
int tip[505];
int shortest[505];
vector<int>parent[505];
vector<int>temp;
int paths;

#define Biggest 1000

void cost(int i, int s) {
    int j, k, a, b;

    if(i == s) {
        k = 0;
        for(j=0; j<temp.size(); j++) {
            k = k + temp[j];
        }
        if(k < paths) {
            paths = k;
        }
    }
    else {
        for(j=0; j<parent[i].size(); j++) {
            temp.push_back(path[i][parent[i][j]]);
            cost(parent[i][j], s);
            temp.pop_back();
        }
    }
}

void dijkstra(int s, int d, int n) {
    int i, j, k, this_node;
    memset(tip, 0, sizeof(tip));

    j = Biggest;
    shortest[s] = 0;
    parent[s].push_back(s);

    for(k=0; k<n; k++) {
        j = Biggest;

        for(i=0; i<n; i++) {
            if(tip[i] ==0 && j > shortest[i]) {
                j = shortest[i];
                this_node = i;
            }
        }

        tip[this_node] = 1;
        if(this_node == d) {
            break;
        }

        for(i=0; i<n; i++) {
            if(tip[i] == 0) {
                if(shortest[i] > shortest[this_node] + graph[this_node][i]) {
                    shortest[i] = shortest[this_node] + graph[this_node][i];
                    parent[i].clear();
                    parent[i].push_back(this_node);
                }
                else if(shortest[i] == shortest[this_node] + graph[this_node][i]) {
                    shortest[i] = shortest[this_node] + graph[this_node][i];
                    parent[i].push_back(this_node);
                }
            }
        }
    }

}

int main() {
    int i, j, k, l, n, m, s, d, ans;

    while(~scanf("%d %d %d %d", &n, &m, &s, &d)) {

        for(i=0; i<n; i++) {
            shortest[i] = Biggest;
            parent[i].clear();
            for(j=i; j<n; j++) {
                if(i == j) {
                    graph[i][j] = 0;
                }
                else {
                    graph[i][j] = graph[j][i] = Biggest;
                }
            }
        }

        while(m--) {
            scanf("%d %d %d %d", &i, &j, &k, &l);
            graph[i][j] = graph[j][i] = k;
            path[i][j] = path[j][i] = l;
        }

        dijkstra(s, d, n);

        i = d;
        ans = 0;
        while(i != s) {
            ans = ans + graph[i][parent[i][0]];
            i = parent[i][0];
        }

        temp.clear();
        paths = 300000;
        cost(d, s);
        cout<<ans<<" "<<paths<<endl;

    }



    return 0;
}

猜你喜欢

转载自blog.csdn.net/hh66__66hh/article/details/83822673
今日推荐