PAT(A) - 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

思路分析:考查Dijkstra + DFS 第二标尺判断。题目不难,就是神烦。要使用两次Dijkstra + DFS


// 这道题不错哦
// 时间允许的话在做一遍

#include <cstdio>
#include <vector>

#define MAX 500 + 10
#define INF 0x3fffffff

using namespace std;

typedef struct  {
    int len;
    int cost;
} Edge;

// 输入
Edge MGraph[MAX][MAX];
int N, M;
int S, D;

// 最短路
int distL[MAX];
int distC[MAX];
int visit[MAX];
vector<int> preL[MAX];
vector<int> preC[MAX];
int minC = INF;
int minL = INF;
vector<int> temp;
vector<int> pathL;
vector<int> pathC;

void initMGraph() {
    for( int i = 0; i < MAX; i++ ) {
        for( int j = 0; j < MAX; j++ ) {
            if( i == j ) {
                MGraph[i][j].len = 0;
                MGraph[i][j].cost = 0;
            }
            else {
                MGraph[i][j].len = INF;
                MGraph[i][j].cost = INF;
            }
        }
    }
}

void DijkstraL( int s ) {
    fill( distL, distL + MAX, INF );
    distL[s] = 0;

    for( int i = 0; i < N; i++ ) {
        int u = -1, MIN = INF;
        for( int j = 0; j < N; j++ ) {
            if( !visit[j] && distL[j] < MIN ) {
                MIN = distL[j];
                u = j;
            }
        }

        if( u == -1 ) return;
        visit[u] = 1;

        for( int v = 0; v < N; v++ ) {
            if( !visit[v] && MGraph[u][v].len != INF && MGraph[u][v].len != 0 ) {
                if( distL[v] > distL[u] + MGraph[u][v].len ) {
                    distL[v] = distL[u] + MGraph[u][v].len;
                    preL[v].clear();
                    preL[v].push_back( u );
                }
                else if( distL[v] == distL[u] + MGraph[u][v].len ) {
                    preL[v].push_back( u );
                }
            }
        }
    }
}

void DijkstraC( int s ) {
    fill( distC, distC + MAX, INF );
    fill( visit, visit + MAX, 0 );
    distC[s] = 0;

    for( int i = 0; i < N; i++ ) {
        int u = -1, MIN = INF;
        for( int j = 0; j < N; j++ ) {
            if( !visit[j] && distC[j] < MIN ) {
                MIN = distC[j];
                u = j;
            }
        }

        if( u == -1 ) return;
        visit[u] = 1;

        for( int v = 0; v < N; v++ ) {
            if( !visit[v] && MGraph[u][v].cost != INF && MGraph[u][v].cost != 0 ) {
                if( distC[v] > distC[u] + MGraph[u][v].cost ) {
                    distC[v] = distC[u] + MGraph[u][v].cost;
                    preC[v].clear();
                    preC[v].push_back( u );
                }
                else if( distC[v] == distC[u] + MGraph[u][v].cost ) {
                    preC[v].push_back( u );
                }
            }
        }
    }
}

void dfsL( int v ) {
    if( v == S ) {
        temp.push_back( v );
        int sumC = 0;
        int size = temp.size();
        for( int i = size - 1; i > 0; i-- ) {
            int cur = temp[i];
            int next = temp[i - 1];
            if( MGraph[cur][next].cost != 0 && MGraph[cur][next].cost != INF ) {
                sumC += MGraph[cur][next].cost;
            }
            else if( MGraph[next][cur].cost != 0 && MGraph[next][cur].cost != INF ) {
                sumC += MGraph[next][cur].cost;
            }
        }

        if( sumC < minC ) {
            minC = sumC;
            pathL = temp;
        }
        temp.pop_back();
        return;
    }
    temp.push_back( v );
    for( int i = 0; i < preL[v].size(); i++ ) {
        dfsL( preL[v][i] );
    }
    temp.pop_back();
}

void dfsC( int v ) {
    if( v == S ) {
        temp.push_back( v );
        int size = temp.size();
        if( size < minL ) {
            minL = size;
            pathC = temp;
        }
        temp.pop_back();
        return;
    }

    temp.push_back( v );
    for( int i = 0; i < preC[v].size(); i++ ) {
        dfsC( preC[v][i] );
    }
    temp.pop_back();
}

int main() {
    initMGraph();
    //freopen( "123.txt", "r", stdin );

    scanf( "%d%d", &N, &M );

    int a, b, tag, l, c;
    for( int i = 0; i < M; i++ ) {
        scanf( "%d%d%d%d%d", &a, &b, &tag, &l, &c );
        if( tag ) {
            MGraph[a][b].len = l;
            MGraph[a][b].cost = c;
        }
        else {
            MGraph[a][b].len = MGraph[b][a].len = l;
            MGraph[a][b].cost = MGraph[b][a].cost = c;
        }
    }

    scanf( "%d%d", &S, &D );

    DijkstraL( S );
    DijkstraC( S );
    dfsL( D );
    temp.clear();
    dfsC( D );

    if( pathL != pathC ) {
        printf( "Distance = %d: ", distL[D] );
        for( int i = pathL.size() - 1; i >= 0; i-- ) {
            if( i == pathL.size() - 1 ) printf( "%d", pathL[i] );
            else printf( " -> %d", pathL[i] );
        }
        printf( "\n" );

        printf( "Time = %d: ", distC[D] );
        for( int i = pathC.size() - 1; i >= 0; i-- ) {
            if( i == pathC.size() - 1 ) printf( "%d", pathC[i] );
            else printf( " -> %d", pathC[i] );
        }
        printf( "\n" );
    }
    else {
        printf( "Distance = %d; Time = %d: ", distL[D], distC[D] );
        for( int i = pathC.size() - 1; i >= 0; i-- ) {
            if( i == pathC.size() - 1 ) printf( "%d", pathC[i] );
            else printf( " -> %d", pathC[i] );
        }
        printf( "\n" );
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值