1111 Online Map

题目

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 input1

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

解题思路

  1. 两次 Dijkstra 算法求出距离和时间的路径,存储在 pre_time, pre_dis 二维数组中
  2. DFS 根据距离和时间路径求出代价最小的唯一路径
  3. one-way 的意思是如果是 1 的话就是单向,0 的话是双向

代码

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

const int maxn = 510;
const int INF = 1000000000;

int n,m;
int st, ed;
int g_cost_length[maxn][maxn];
int g_cost_time[maxn][maxn];

int d[maxn];
int t[maxn];
bool vis[maxn] = {false};

int n_min_time_cost =  INF;
int n_min_length_cost = INF;

vector<int> pre_length[maxn], pre_time[maxn];

vector<int> path_dis, path_time ,temp;

void Dijkstra_Dis(int s);
void DFS_Dis(int v);
void Dijkstra_Time(int s);
void DFS_Time(int v);

int main()
{
    fill(g_cost_length[0], g_cost_length[0]+maxn*maxn, INF);
    fill(g_cost_time[0], g_cost_time[0]+maxn*maxn, INF);

    scanf("%d%d",&n, &m);
    int v1, v2;
    int n_one_way;
    for(int i = 0 ; i < m; ++i) {
        scanf("%d %d", &v1, &v2);
        scanf("%d %d %d", &n_one_way, &g_cost_length[v1][v2], &g_cost_time[v1][v2]);
        if(0 == n_one_way) {
            g_cost_length[v2][v1] = g_cost_length[v1][v2];
            g_cost_time[v2][v1] = g_cost_time[v1][v2];
        }
    }

    scanf("%d%d",&st, &ed);

    Dijkstra_Dis(st);
    DFS_Dis(ed);

    temp.clear();

    Dijkstra_Time(st);
    DFS_Time(ed);

    if(path_dis == path_time) {
        printf("Distance = %d; Time = %d: ", d[ed], t[ed]);
        for(int i = path_dis.size()-1; i >= 0; --i){
            printf("%d", path_dis[i]);
            if( i > 0) {
                printf(" -> ");
            }
        }
        printf("\n");        
    }
    else {
        printf("Distance = %d: ", d[ed]);
        for(int i = path_dis.size()-1; i >= 0; --i){
            printf("%d", path_dis[i]);
            if( i > 0) {
                printf(" -> ");
            }
        }
        printf("\n");
        printf("Time = %d: ", t[ed]);
        for(int i = path_time.size()-1; i >= 0; --i){
            printf("%d", path_time[i]);
            if( i > 0) {
                printf(" -> ");
            }
        }
        printf("\n");
    }

    return 0;

}

void Dijkstra_Dis(int s)
{
    fill(d,d+maxn, INF);
    memset(vis, 0, sizeof(vis));

    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(-1 == u) return;
        vis[u] = true;

        for(int v = 0; v < n; v++) {
            if(vis[v] == false && g_cost_length[u][v] != INF) {
                if(d[v] > d[u] + g_cost_length[u][v]) {
                    d[v] = d[u] + g_cost_length[u][v];
                    pre_length[v].clear();
                    pre_length[v].push_back(u);
                }
                else if(d[v] == d[u] + g_cost_length[u][v]) {
                    pre_length[v].push_back(u);
                }
            }
        }
    }
}

void DFS_Dis(int v)
{
    if(v == st) {
        temp.push_back(v);
        int n_tmp_time_cost = 0;
        for(int i = temp.size() - 1; i > 0; --i) {
            int id = temp[i];
            int id_next = temp[i-1];
            n_tmp_time_cost += g_cost_time[id][id_next];
        }

        if(n_min_time_cost > n_tmp_time_cost) {
            n_min_time_cost = n_tmp_time_cost;
            path_dis = temp;
        }
        temp.pop_back();
        return;
    }

    temp.push_back(v);
    for(int i = 0; i < pre_length[v].size(); ++i) {
        DFS_Dis(pre_length[v][i]);
    }

    temp.pop_back();
}

void Dijkstra_Time(int s)
{
    fill(t,t+maxn, INF);
    memset(vis, 0, sizeof(vis));

    t[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 && t[j] < min) {
                u = j;
                min = d[j];
            }
        }

        if(-1 == u) return;
        vis[u] = true;

        for(int v = 0; v < n; v++) {
            if(vis[v] == false && g_cost_time[u][v] != INF) {
                if(t[v] > t[u] + g_cost_time[u][v]) {
                    t[v] = t[u] + g_cost_time[u][v];
                    pre_time[v].clear();
                    pre_time[v].push_back(u);
                }
                else if(t[v] == t[u] + g_cost_time[u][v]) {
                    pre_time[v].push_back(u);
                }
            }
        }
    }    
}
void DFS_Time(int v)
{
    if(v == st) {
        temp.push_back(v);
        if(path_time.size() == 0 || temp.size() < path_time.size()){
            path_time = temp;
        }
        temp.pop_back();
        return;
    }

    temp.push_back(v);
    for(int i = 0; i < pre_time[v].size(); ++i) {
        DFS_Time(pre_time[v][i]);
    }

    temp.pop_back();
}
posted @ 2024-01-31 17:08  王清河  阅读(2)  评论(0编辑  收藏  举报