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

這道題就是兩次Dij算法,比較複雜,需要耐心 

#include <iostream>
#include <vector>
#include <cstring>
#include <climits>
#include <map>
#include <algorithm>
using namespace std;
int graph1[505][505];
int graph2[505][505];
int pre[505];
int N,M;
int s,d;
void init(){
    for (int i = 0; i < 505; ++i) {
        for (int j = 0; j < 505; ++j) {
            graph1[i][j]=INT_MAX;
            graph2[i][j]=INT_MAX;
        }
    }
}
int Dij1(){
    bool visited[505];
    int dist[505];
    int times[505];
    fill(pre,pre+505,-1);
    fill(dist,dist+505,INT_MAX);
    fill(times,times+505,INT_MAX);
    memset(visited, false, sizeof(visited));
    dist[s]=0;
    times[s]=0;
    for (int k = 0; k < N; ++k) {
        int minVal=INT_MAX;
        int index=0;
        for (int i = 0; i < N; ++i) {
            if(!visited[i]&&dist[i]<minVal){
                index=i;
                minVal=dist[i];
            }
        }
        visited[index]= true;
        if(index==d){
            return dist[index];
        }
        for (int j = 0; j < N; ++j) {
            if(!visited[j]&&graph1[index][j]!=INT_MAX){
                if(dist[j]>dist[index]+graph1[index][j]){
                    pre[j]=index;
                    dist[j]=dist[index]+graph1[index][j];
                    times[j]=times[index]+graph2[index][j];
                }else if(dist[j]==(dist[index]+graph1[index][j])&&times[j]>(times[index]+graph2[index][j])){
                    pre[j]=index;
                    times[j]=times[index]+graph2[index][j];
                }
            }
        }
    }
    return dist[d];
}
void findWay(vector<int>& vector1,int x){
    if(x==-1){
        return;
    }
    findWay(vector1,pre[x]);
    vector1.push_back(x);
}
int Dij2(){
    bool visited[505];
    int nums[505];
    int times[505];
    fill(pre,pre+505,-1);
    fill(nums,nums+505,0);
    fill(times,times+505,INT_MAX);
    memset(visited, false, sizeof(visited));
    nums[s]=0;
    times[s]=0;
    for (int k = 0; k < N; ++k) {
        int minVal=INT_MAX;
        int index=0;
        for (int i = 0; i < N; ++i) {
            if(!visited[i]&&times[i]<minVal){
                minVal=times[i];
                index=i;
            }
        }
        visited[index]= true;
        if(index==d){
            return times[index];
        }
        for (int j = 0; j < N; ++j) {
            if(!visited[j]&&graph2[index][j]!=INT_MAX){
                if(times[j]>times[index]+graph2[index][j]){
                    pre[j]=index;
                    times[j]=times[index]+graph2[index][j];
                    nums[j]=nums[index]+1;
                } else if(times[j]==(times[index]+graph2[index][j])&&nums[j]>(nums[index]+1)){
                    nums[j]=nums[index]+1;
                    pre[j]=index;
                }
            }
        }
    }
    return times[d];
}
bool judge(vector<int>& vector1,vector<int>& vector2){
    if(vector1.size()!=vector2.size()){
        return false;
    }
    for (int i = 0; i < vector1.size(); ++i) {
        if(vector1[i]!=vector2[i]){
            return false;
        }
    }
    return true;
}
int main(){
    cin>>N>>M;
    init();
    for (int i = 0; i < M; ++i) {
        int v1,v2,one,len,time;
        cin>>v1>>v2>>one>>len>>time;
        graph1[v1][v2]=len;
        graph2[v1][v2]=time;
        if(one==0){
            graph1[v2][v1]=len;
            graph2[v2][v1]=time;
        }
    }
    cin>>s>>d;
    vector<int> vector1;
    vector<int> vector2;
    int distance=Dij1();
    findWay(vector1,d);
    int time=Dij2();
    findWay(vector2,d);
    if(judge(vector1,vector2)){
        cout<<"Distance = "<<distance<<"; "<<"Time = "<<time<<": ";
        cout<<vector1[0];
        for (int i = 1; i < vector1.size(); ++i) {
            cout<<" -> "<<vector1[i];
        }
        cout<<endl;
        return 0;
    }
    cout<<"Distance = "<<distance<<": ";
    cout<<vector1[0];
    for (int i = 1; i < vector1.size(); ++i) {
        cout<<" -> "<<vector1[i];
    }
    cout<<endl;
    cout<<"Time = "<<time<<": ";
    cout<<vector2[0];
    for (int i = 1; i < vector2.size(); ++i) {
        cout<<" -> "<<vector2[i];
    }
    cout<<endl;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章