PAT A1030 Travel Plan (30分)

PAT甲级:A1030 Travel Plan (30分)

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 Specification:

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 Specification:

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
  • 题意:旅行者的地图上标明了各个城市间的距离和过路费,要求给定任意起点和终点,在地图上找到一条距离最短的路径,如果最短距离不只是一条,则选择花销更短的那条,题目保证最小花销是唯一的。
  • 分析:这是图的单源最短路径问题,使用Dijsktra最短路径算法求解,题目里给出了距离和花销两个尺标,在满足第一尺标最优的情况下,使得第二尺标最优。这里可以选择 DFS 的方式计算多个最短路径的总花销,但是因为只有两个尺标,所以可以另设int c[N]数组存储起点到某点的花销,直接计算得到最优解。(只用Dijkstra算法也需要用到DFS,但是这里的DFS只是递归输出路径而已)。

Dijkstra:

#include <bits/stdc++.h>
using namespace std;
const int N = 510;
const int INF = 0x7fffffff;
int n, m, st, dest, e[N][N], cost[N][N] = {0}, d[N], c[N];
vector<int> pre(N);
int minCost = INF;
void dfs(int u) {
    if (u == st) {
        printf("%d ", u);
        return;
    }
    dfs(pre[u]);
    printf("%d ", u);
}
void dijkstra(int s) {
    fill(d, d + N, INF);
    fill(c, c + N, INF);
    d[s] = c[s] = 0;
    bool vis[N] = {false};
    while (true) {
        int u = -1, MIN = INF;
        for (int i = 0; i < n; i++) {
            if (!vis[i] && d[i] < MIN) {
                MIN = d[i];
                u = i;
            }
        }
        if (u == -1) return;
        vis[u] = true;
        for (int v = 0; v < n; v++) {
            if (!vis[v] && e[u][v] != INF) {
                if (d[u] + e[u][v] < d[v]) {
                    d[v] = d[u] + e[u][v];
                    c[v] = c[u] + cost[u][v];
                    pre[v] = u;
                } else if (d[u] + e[u][v] == d[v] && c[u] + cost[u][v] < c[v]) {
                    c[v] = c[u] + cost[u][v];
                    pre[v] = u;
                }
            }
        }
    }
}
int main() {
    scanf("%d%d%d%d", &n, &m, &st, &dest);
    int u, v, tdist, tcost;
    fill(e[0], e[0] + N * N, INF);
    for (int i = 0; i < m; i++) {
        scanf("%d%d%d%d", &u, &v, &tdist, &tcost);
        e[u][v] = e[v][u] = tdist;
        cost[u][v] = cost[v][u] = tcost;
    }
    dijkstra(st);
    dfs(dest);
    printf("%d %d", d[dest], c[dest]);
    return 0;
}

Dijkstra + DFS:

#include <bits/stdc++.h>
using namespace std;
const int N = 510;
const int INF = 0x7fffffff;
int n, m, st, dest, e[N][N], cost[N][N] = {0}, d[N], minCost = INF;
vector<int> path, tempPath, pre[N];
void dfs(int u) {
    tempPath.push_back(u);
    if (u == st) {
        int tempCost = 0;
        for (int i = 0; i < tempPath.size() - 1; i++)
            tempCost += cost[tempPath[i]][tempPath[i + 1]];
        if (tempCost < minCost) {
            minCost = tempCost;
            path = tempPath;
        }
        tempPath.pop_back();
        return;
    }
    for (auto it : pre[u]) dfs(it);
    tempPath.pop_back();
}
void dijkstra(int s) {
    fill(d, d + N, INF);
    d[s] = 0;
    bool vis[N] = {false};
    while (true) {
        int u = -1, MIN = INF;
        for (int i = 0; i < n; i++) {
            if (!vis[i] && d[i] < MIN) {
                MIN = d[i];
                u = i;
            }
        }
        if (u == -1) return;
        vis[u] = true;
        for (int v = 0; v < n; v++) {
            if (!vis[v] && e[u][v] != INF) {
                if (d[u] + e[u][v] < d[v]) {
                    d[v] = d[u] + e[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                } else if (d[u] + e[u][v] == d[v]) {
                    pre[v].push_back(u);
                }
            }
        }
    }
}
int main() {
    scanf("%d%d%d%d", &n, &m, &st, &dest);
    int u, v, tdist, tcost;
    fill(e[0], e[0] + N * N, INF);
    for (int i = 0; i < m; i++) {
        scanf("%d%d%d%d", &u, &v, &tdist, &tcost);
        e[u][v] = e[v][u] = tdist;
        cost[u][v] = cost[v][u] = tcost;
    }
    dijkstra(st);
    dfs(dest);
    for (int i = path.size() - 1; i >= 0; i--) printf("%d ", path[i]);
    printf("%d %d", d[dest], minCost);
    return 0;
}
发布了27 篇原创文章 · 获赞 0 · 访问量 100

猜你喜欢

转载自blog.csdn.net/charjindev/article/details/104311828
今日推荐