1111 Online Map (30point(s)) - C語言 PAT 甲級

1111 Online Map (30point(s))

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2≤N≤500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> … -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> … -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> … -> destination

Sample Input:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output:

Distance = 3; Time = 4: 3 -> 2 -> 5

題目大意:

輸入 N 個節點,M 條路徑,路徑的參數分別爲:

  • 節點 1,節點 2,0 雙向 1 單向,長度,花費時間

輸出最短路徑,若相同,則要求時間最短
接着輸出最快路徑,若相同,則要求節點數最小

設計思路:

按照題目要求,用兩個 Dijkstra 算法分別求最短路徑和最快路徑

編譯器:C (gcc)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int n, m, start, end, shortest[2];
int vt[510][510], vw[510][510];

int visit[510], dis[510], predis[510], time[510], pretime[510];
int wshortest[510], wcnt, tshortest[510], tcnt;
void dijkstra_w()
{
        int i, j, u, v, min;
        for (j = 0; j < n; j++) {
                visit[j] = 0;
                dis[j] = INT_MAX;
                //time[j] = INT_MAX;
        }
        dis[start] = 0;
        for (i = 0; i < n; i++) {
                u = -1;
                min = INT_MAX;
                for (j = 0; j < n; j++) {
                        if (visit[j] == 0 && dis[j] < min) {
                                u = j;
                                min = dis[j];
                        }
                }
                if (u == -1)
                        break;
                visit[u] = 1;
                for (v = 0; v < n; v++) {
                        if (visit[v] == 0 && vw[u][v]) {
                                if (vw[u][v] + dis[u] < dis[v]) {
                                        dis[v] = vw[u][v] + dis[u];
                                        predis[v] = u;
                                        time[v] = time[u] + vt[u][v];
                                } else if (vw[u][v] + dis[u] == dis[v] && time[v] > time[u] + vt[u][v]) {
                                        time[v] = time[u] + vt[u][v];
                                        predis[v] = u;
                                }
                        }
                }
        }
}

void dijkstra_t()
{
        int num[510];
        int i, j, u, v, min;
        for (j = 0; j < n; j++) {
                visit[j] = 0;
                time[j] = INT_MAX;
        }
        time[start] = 0;
        for (i = 0; i < n; i++) {
                u = -1;
                min = INT_MAX;
                for (j = 0; j < n; j++) {
                        if (visit[j] == 0 && time[j] < min) {
                                u = j;
                                min = time[j];
                        }
                }
                if (u == -1)
                        break;
                visit[u] = 1;
                for (v = 0; v < n; v++) {
                        if (visit[v] == 0 && vt[u][v]) {
                                if (vt[u][v] + time[u] < time[v]) {
                                        time[v] = vt[u][v] + time[u];
                                        pretime[v] = u;
                                        num[v] = num[u] + 1;
                                } else if (vt[u][v] + time[u] == time[v] && num[v] > num[u] + 1) {
                                        num[v] = num[u] + 1;
                                        pretime[v] = u;
                                }
                        }
                }
        }
}

void dfs_w(int v)
{
        wshortest[wcnt++] = v;
        if (v == start)
                return ;
        dfs_w(predis[v]);
}

void dfs_t(int v)
{
        tshortest[tcnt++] = v;
        if (v == start)
                return ;
        dfs_t(pretime[v]);
}

int comp()
{
        if (wcnt != tcnt)
                return 0;
        int i;
        for (i = 0; i < wcnt; i++)
                if (wshortest[i] != tshortest[i])
                        return 0;
        return 1;
}

void out_printf(int *a, int cnt)
{
        int i;
        printf("%d", a[cnt - 1]);
        for (i = cnt - 2; i >= 0; i--)
                printf(" -> %d", a[i]);
}

int main(void)
{
        int a, b, c, d, e;
        scanf("%d%d", &n, &m);
        while (m--) {
                scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
                vw[a][b] = d;
                vt[a][b] = e;
                if (c == 0) {
                        vw[b][a] = d;
                        vt[b][a] = e;
                }
        }
        scanf("%d%d", &start, &end);

        dijkstra_w();
        dfs_w(end);
        dijkstra_t();
        dfs_t(end);
        if (comp()) {
                printf("Distance = %d; Time = %d: ", dis[end], time[end]);
                out_printf(wshortest, wcnt);
        } else {
                printf("Distance = %d: ", dis[end]);
                out_printf(wshortest, wcnt);
                puts("");
                printf("Time = %d: ", time[end]);
                out_printf(tshortest, tcnt);
        }

        return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章