PAT甲级刷题记录——1111 Online Map (30分)

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 V1and V2are the indices (from 0 to N−1) of the two ends of the street; one-wayis 1 if the street is one-way from V1to V2, or 0 if not; lengthis the length of the street; and timeis 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 Din 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

思路

这题其实还是比较简单的,但是因为我将近一个月没碰图论的题目(学高数去了T T),导致手生了不少,各种小错误小漏洞频出,昨天晚上做的一直到今天早上起来才发现问题所在。

总体上来说,这题就是两遍Dijkstra+DFS,第一遍Dijkstra要找最短路,然后再在DFS中寻找时间最短的路径,第二遍Dijkstra要找时间最短路(可以这么理解,第一遍找的“最短路”是以距离为边权来寻找的,那么这次我们要以时间为边权来寻找),然后再在DFS中寻找路径中节点数最少的路径(这个很简单,直接就是tempPath.size())。

下面说一下注意点:

  • 两次Dijkstra和DFS所要用到的东西最好不要用同一个(比如pre数组、tempPath等等),我的建议就是干脆重新开一个,每对Dijkstra和DFS用自己的,互不打扰,这样就不会出错了;
  • DFS的时候,tempPath中存储的路径应当是:终点->起点的顺序,因此倒序遍历的时候,就是从起点到终点,所以加上的时间应该是t[tempPath[i]][tempPath[i-1]](即:应该是i->i-1,而不是i-1->i,这是我太久没做图论题的缘故……);
  • 另外,用二维数组存储边权的时候,如果是无向边,记得两边都要存储,而且千万不要输错(我就是在这里输成了t[tmpV2][tmpV2]导致半天没找到问题,说到底还是手太生了~(逃))。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 510;
const int INF = 123123123;
struct node{
    int v;//目标点
    int dis;
    int time;
};
vector<node> G[maxn];
vector<int> pre[maxn], pre_T[maxn];
vector<int> Shortest_Path, Fastest_Path, tempPath, tempPath_T;
bool vis[maxn] = {false};
bool vis_T[maxn] = {false};
int d[maxn] = {0};
int tt[maxn] = {0};//记录某点的最短时间
int t[maxn][maxn] = {0};//记录一条路所花时间的边权
int minTime = INF, minPoint = INF;
int N, M;
void Dijkstra(int s){
    fill(d, d+maxn, INF);
    d[s] = 0;
    for(int i=0;i<N;i++){
        int u = -1, MIN = INF;
        for(int j=0;j<N;j++){
            if(vis[j]==false&&d[j]<MIN){
                u = j;
                MIN = d[j];
            }
        }
        if(u==-1) return;
        vis[u] = true;
        for(int j=0;j<G[u].size();j++){
            int v = G[u][j].v;
            if(vis[v]==false){
                if(d[u]+G[u][j].dis<d[v]){
                    d[v] = d[u]+G[u][j].dis;
                    pre[v].clear();
                    pre[v].push_back(u);
                }
                else if(d[u]+G[u][j].dis==d[v]){
                    pre[v].push_back(u);
                }
            }
        }
    }
}
void Dijkstra_T(int s){
    fill(tt, tt+maxn, INF);
    tt[s] = 0;
    for(int i=0;i<N;i++){
        int u = -1, MIN = INF;
        for(int j=0;j<N;j++){
            if(vis_T[j]==false&&tt[j]<MIN){
                u = j;
                MIN = tt[j];
            }
        }
        if(u==-1) return;
        vis_T[u] = true;
        for(int j=0;j<G[u].size();j++){
            int v = G[u][j].v;
            if(vis_T[v]==false){
                if(tt[u]+G[u][j].time<tt[v]){
                    tt[v] = tt[u]+G[u][j].time;
                    pre_T[v].clear();
                    pre_T[v].push_back(u);
                }
                else if(tt[u]+G[u][j].time==tt[v]){
                    pre_T[v].push_back(u);
                }
            }
        }
    }
}
void dfs(int v, int s){
    if(v==s){
        tempPath.push_back(v);
        int tmpTime = 0;
        for(int i=tempPath.size()-1;i>0;i--){
            tmpTime += t[tempPath[i]][tempPath[i-1]];
        }
        if(tmpTime<minTime){
            minTime = tmpTime;
            Shortest_Path = tempPath;
        }
        return;
    }
    tempPath.push_back(v);
    for(int i=0;i<pre[v].size();i++){
        dfs(pre[v][i], s);
        tempPath.pop_back();
    }
}
void dfs_T(int v, int s){
    if(v==s){
        tempPath_T.push_back(v);
        int tmpPoint = tempPath_T.size();
        if(tmpPoint<minPoint){
            minPoint = tmpPoint;
            Fastest_Path = tempPath_T;
        }
        return;
    }
    tempPath_T.push_back(v);
    for(int i=0;i<pre_T[v].size();i++){
        dfs_T(pre_T[v][i], s);
        tempPath_T.pop_back();
    }
}
int main()
{
    scanf("%d%d", &N, &M);
    for(int i=0;i<M;i++){
        int tmpV1, tmpV2, isone_way, tmpLen, tmpTime;
        scanf("%d%d%d%d%d", &tmpV1, &tmpV2, &isone_way, &tmpLen, &tmpTime);
        if(isone_way==1){
            t[tmpV1][tmpV2] = tmpTime;
            node tmp;
            tmp.dis = tmpLen;
            tmp.v = tmpV2;
            tmp.time = tmpTime;
            G[tmpV1].push_back(tmp);
        }
        else{
            t[tmpV1][tmpV2] = tmpTime;
            t[tmpV2][tmpV1] = tmpTime;
            node tmp;
            tmp.dis = tmpLen;
            tmp.v = tmpV2;
            tmp.time = tmpTime;
            G[tmpV1].push_back(tmp);
            tmp.v = tmpV1;
            G[tmpV2].push_back(tmp);
        }
    }
    int s, e;
    scanf("%d%d", &s, &e);
    Dijkstra(s);
    dfs(e, s);
    Dijkstra_T(s);
    dfs_T(e, s);
    if(Shortest_Path==Fastest_Path){
        printf("Distance = %d; Time = %d: ", d[e], tt[e]);
        for(int i=Shortest_Path.size()-1;i>=0;i--){
            if(i==Shortest_Path.size()-1) printf("%d", Shortest_Path[i]);
            else printf(" -> %d", Shortest_Path[i]);
        }
    }
    else{
        printf("Distance = %d: ", d[e]);
        for(int i=Shortest_Path.size()-1;i>=0;i--){
            if(i==Shortest_Path.size()-1) printf("%d", Shortest_Path[i]);
            else printf(" -> %d", Shortest_Path[i]);
        }
        printf("\n");
        printf("Time = %d: ", tt[e]);
        for(int i=Fastest_Path.size()-1;i>=0;i--){
            if(i==Fastest_Path.size()-1) printf("%d", Fastest_Path[i]);
            else printf(" -> %d", Fastest_Path[i]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值