1111 Online Map (30 分)

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

Train Of Thinking

  • 一开始总有两个test WA 以为是格式错误,后来发现是看见题目最后的要求:
  • 如果最短路径有多条,输出最快的
  • 如果最快路径有多条,输出经过节点数最少的
    • 开始以为经过节点数最少即,当时间一样时,不经过u点即可
    • 其实我理解错了,经过节点u并不是增加了一个节点数,节点数有可能减少,因为u之前完全可能是另一条路径
    • 因此需要设立一个pass数组,表示每一个节点经过的节点数,当用时相同时,比较pass

code

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

const int maxn = 510;
const int INF = 0x3f3f3f3f;
int G[maxn][maxn], T[maxn][maxn];
int d[maxn], d_pre[maxn], t[maxn], t_pre[maxn], pass[maxn];
bool vis[maxn] = {false};
int n, m;

void Dijkstra_D(int s)
{
    fill(d, d + n, INF);
    fill(t, t + n, INF);
    for(int i = 0; i < n; ++i) d_pre[i] = i;
    d[s] = 0;
    for(int i = 0; i < n; ++i){
        int minn = INF, u = -1;
        for(int j = 0; j < n; ++j){
            if(!vis[j] && d[j] < minn){
                minn = d[j];
                u = j;
            }
        }
        if(u == -1) return;
        vis[u] = 1;
        for(int v = 0; v < n; ++v){
            if(!vis[v] && G[u][v] != INF){
                if(d[u] + G[u][v] < d[v]){
                    d[v] = d[u] + G[u][v];
                    t[v] = d[u] + T[u][v];
                    d_pre[v] = u;
                }
                else if(d[u] + G[u][v] == d[v]){
                    if(t[u] + T[u][v] < t[v]){
                        t[v] = d[u] + T[u][v];
                        d_pre[v] = u;//
                    }
                }
            }
        }
    }
}

void Dijkstra_T(int s)
{
    fill(vis, vis + n, false);
    fill(pass, pass + n, INF);
    pass[s] = 1;
    fill(t, t + n, INF);
    for(int i = 0; i < n; ++i) t_pre[i] = i;
    t[s] = 0;
    for(int i = 0; i < n; ++i){
        int minn = INF, u = -1;
        for(int j = 0; j < n; ++j){
            if(!vis[j] && t[j] < minn){
                minn = t[j];
                u = j;
            }
        }
        if(u == -1) return;
        vis[u] = 1;
        for(int v = 0; v < n; ++v){
            if(!vis[v] && T[u][v] != INF){
                if(t[u] + T[u][v] < t[v]){
                    t[v] = t[u] + T[u][v];
                    t_pre[v] = u;
                    pass[v] = pass[u] + 1;
                }
                else if(t[u] + T[u][v] == t[v]){
                    if(pass[u] + 1 < pass[v]){
                        pass[v] = pass[u] + 1;
                        t_pre[v] = u;
                    }
                }
            }
        }
    }
}

vector<int> pathD, pathT;

void DFS_D(int s, int v)
{
    if(v == s){
        pathD.push_back(v);
        return;
    }
    DFS_D(s, d_pre[v]);
    pathD.push_back(v);
}

void DFS_T(int s, int v)
{
    if(v == s){
        pathT.push_back(v);
        return;
    }
    DFS_T(s, t_pre[v]);
    pathT.push_back(v);
}

void print_path(const vector<int> &v)
{
    for(int i = 0; i < v.size(); ++i){
        if(i != 0) printf(" -> ");
        printf("%d",v[i]);
    }
    printf("\n");
}

int main()
{
    fill(G[0], G[0] + maxn * maxn, INF);
    fill(T[0], T[0] + maxn * maxn, INF);
    scanf("%d%d",&n,&m);
    for(int i = 0; i < m; ++i){
        int c1, c2, one_way, len, time;
        scanf("%d%d%d%d%d",&c1,&c2,&one_way,&len,&time);
        G[c1][c2] = len;
        T[c1][c2] = time;
        if(!one_way){
            G[c2][c1] = len;
            T[c2][c1] = time;
        }
    }
    int st, ed;
    scanf("%d%d",&st,&ed);
    Dijkstra_D(st); DFS_D(st, ed);
    Dijkstra_T(st); DFS_T(st, ed);
    if(pathD == pathT){
        printf("Distance = %d; Time = %d: ",d[ed],t[ed]);
        print_path(pathD);
    }
    else {
        printf("Distance = %d: ",d[ed]); print_path(pathD);
        printf("Time = %d: ",t[ed]); print_path(pathT);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值