美文网首页
PAT 甲级 刷题日记|A 1111 Online Map (3

PAT 甲级 刷题日记|A 1111 Online Map (3

作者: 九除以三还是三哦 | 来源:发表于2021-08-24 11:39 被阅读0次

单词积累

one-way 单程 单行道

题目

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

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

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5结尾无空行

Sample Input 2:

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

Distance = 3; Time = 4: 3 -> 2 -> 5结尾无空行

思路:

典型的最短路径,两次迪杰斯特拉算法即可

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn = 502;
const int inf = 1e9;
int gra[maxn][maxn];
int spend[maxn][maxn];
int visit[maxn];
int dis[maxn];
int distime[maxn];
int times[maxn];
int bynum[maxn];
int pred[maxn];
int pret[maxn];
vector<int> disans;
vector<int> timeans; 
int n, m, s, t;

int main() {
    fill(gra[0], gra[0] + maxn * maxn, inf);
    fill(spend[0], spend[0] + maxn * maxn, inf);
    fill(dis, dis + maxn, inf);
    cin>>n>>m;
    for (int i = 0; i < m; i++) {
        int v1, v2, one_way, len, time;
        cin>>v1>>v2>>one_way>>len>>time; 
        if (one_way == 1) {
            gra[v1][v2] = len;
            spend[v1][v2] = time;
        } else {
            gra[v1][v2] = gra[v2][v1] = len;
            spend[v1][v2] = spend[v2][v1] = time;
        }
    }
    cin>>s>>t;
    dis[s] = 0;
    pred[s] = -1;
    distime[s] = 0;
    for (int i = 0; i < n; i++) {
        int u = -1, mindis = inf;
        for (int j = 0; j < n; j++) {
            if (visit[j] == 0 && dis[j] < mindis) {
                u = j; 
                mindis = dis[j];
            }
        }
        if (u == -1) break;
        visit[u] = 1;
        for (int v = 0; v < n; v++) {
            if (visit[v] == 0 && gra[u][v] != inf) {
                if (dis[v] > dis[u] + gra[u][v]) {
                    dis[v] = dis[u] + gra[u][v];
                    pred[v] = u;
                    distime[v] = distime[u] + spend[u][v];
                } else if (dis[v] == dis[u] + gra[u][v]) {
                    if (distime[v] > distime[u] + spend[u][v]) {
                        pred[v] = u;
                        distime[v] = distime[u] + spend[u][v];
                    }
                }
            }
        }
    }
    int id = t;
    while (1) {
        disans.push_back(id);
        id = pred[id];
        if (id == -1) break;
    }
    
    fill(visit, visit + maxn, 0);
    fill(times, times + maxn, inf);
    fill(bynum, bynum + maxn, inf);
    times[s] = 0;
    bynum[s] = 0;
    pret[s] = -1;
    for (int i = 0; i < n; i++) {
        int u = -1, mintime = inf;
        for (int j = 0; j < n; j++) {
            if (visit[j] == 0 && times[j] < mintime) {
                u = j;
                mintime = times[j];
            }
        }
        if (u == -1) break;
//      cout<<u<<endl;
        visit[u] = 1;
        for (int v = 0; v < n; v++) {
            if (visit[v] == 0 && spend[u][v] != inf) {
                if (times[v] > times[u] + spend[u][v]) {
                    times[v] = times[u] + spend[u][v];
                    pret[v] = u;
                    bynum[v] = bynum[u] + 1;
                } else if (times[v] == times[u] + spend[u][v]) {
                    if (bynum[v] > bynum[u] + 1) {
                        pret[v] = u;
                        bynum[v] = bynum[u] + 1;
                    }
                }
            }
        }
    }
    int id2 = t;
    while (1) {
        timeans.push_back(id2);
        id2 = pret[id2];
        if (id2 == -1) break;
    }
    
    if (timeans == disans) {
        cout<<"Distance = "<<dis[t]<<"; ";
        cout<<"Time = "<<times[t]<<": ";
        for (int i = timeans.size() - 1; i >= 0; i--) {
            cout<<timeans[i];
            if (i != 0) cout<<" -> ";
            else cout<<endl;
        }
        return 0;
    }
    
    
    
    cout<<"Distance = "<<dis[t]<<": ";
    for (int i = disans.size() - 1; i >= 0; i--) {
        cout<<disans[i];
        if (i != 0) cout<<" -> ";
        else cout<<endl;
    }
    cout<<"Time = "<<times[t]<<": ";
    for (int i = timeans.size() - 1; i >= 0; i--) {
        cout<<timeans[i];
        if (i != 0) cout<<" -> ";
        else cout<<endl;
    }
}

相关文章

网友评论

      本文标题:PAT 甲级 刷题日记|A 1111 Online Map (3

      本文链接:https://www.haomeiwen.com/subject/okvgbltx.html