Pat_a, 1111. Online Map (30)

Source: Internet
Author: User
Tags bool min prev
1111. Online Map (+)

Input position and a destination, an online map can recommend several paths. Now your job are to recommend, paths to your user:one are the shortest, and the other is the fastest. It is guaranteed, a path exists for any request.

Input Specification:

Each input file contains the one test case. For each case, the first line gives-positive integers n (2 <= n <=), and M, being the total number of the street s 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 is the indices (from 0 to N-1) of the "The" of the street; One-way is 1 if the street was 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 on the next line print the fastest path with total time T:

Time = T:source, W1---destination

In case the shortest path isn't unique, output the fastest one among the shortest paths, which is guaranteed to be unique . In case the fastest path isn't unique, output the one that passes through the fewest intersections, which is guaranteed t o be unique.

In case the shortest and the fastest paths is 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 = 8, 4, 6:3, 5
Time = 5 3:3, 1

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 = 5 4:3, 2, analysis
is to use the shortest path, but used two times only, the idea is very clear, the code is long the first time to do with DFS, the last test point timeout 27 now with Dijkstra do, test point 2 did not 23 points (also drunk) but this problem, Every time I do it, I have a deep understanding of Dijkstra. The test point 2 has not solved the Dijkstra algorithm on Wikipedia


1 function Dijkstra (Graph, source):
2
3 Create vertex set Q
4
5 for each vertex v in Graph://initialization
6 dist[v]←infinity//Unknown distance from source to V
7 prev[v]←undefined//Previous node in optimal path from source
8 Add V to Q//all nodes initially in Q (unvisited nodes)
9
Dist[source]←0//Distance from source to source
11
While Q is not empty:
U←vertex in Q with min dist[u]//Source node would be selected first
Remove U from Q
15
Neighbor V of U://where V is still in Q.
Alt←dist[u] + length (U, v)
If Alt < DIST[V]://A shorter path to V have been found
Dist[v]←alt
Prev[v]←u
21st
Return dist[], prev[]
Code

#include <iostream> #include <climits> #include <stack> #define INF Int_max using namespace std;
int a[510][510];
int b[510][510]; The shortest path is the select parameter a b//min time parameter B A///front node saved in a[i][i] int sp[510];//record the current shortest int st[510];//record the current shortest void Dijkstra (int (*a) [510],in
    T (*b) [510],int start,int end,int N) {bool visit[510];
        Initialize, pre-order for (int i=0;i<n;i++) {visit[i]=false;
        Sp[i]=inf;
    St[i]=inf;
    } A[start][start]=start;
    sp[start]=0;
    st[start]=0;

    Visit[start]=true;
    int Begin=start; while (true) {//update for (int i=0;i<n;i++) {if (a[start][i]==-1) con
            Tinue;
                if (Sp[i]>a[start][i]+sp[start]) {Sp[i]=a[start][i]+sp[start];
                St[i]=b[start][i]+st[start];
            start=i;//This is not all a[i][i]=start; }else if (Sp[i]==a[start][i]+sp[start]) {if (st[i]>b[start][I]+st[start]) {St[i]=b[start][i]+st[start];
                A[i][i]=start;
        }}} int min=inf;
        int begin=-1;
            Select the starting point, must be from the global, so greedy is the most for (int i=0;i<n;i++) {if (!visit[i]&&sp[i]<min)
                {Min=sp[i];
            Begin=i;
        }} if (Begin==-1) break;
        Start=begin;

        Visit[start]=true;
    if (start==end) break;
            }}//dijstra algorithm int main () {for (int. i=0;i<510;i++) {for (int j=0;j<510;j++) {
            A[i][j]=-1;
        B[i][j]=-1;
    }} int n,m;
    cin>>n>>m;
    int v1,v2,one_way,length,_time;
        for (int i=0;i<m;i++) {cin>>v1>>v2>>one_way;
        cin>>a[v1][v2];
        cin>>b[v1][v2]; if (one_way==0) {A[V2][V1]=A[V1][v2];
        B[V2][V1]=B[V1][V2];
    }} int s,d;

    cin>>s>>d;
    BOOL Issame=true;
    Stack<int> St1,st2;

    int dis,time;
    Dijkstra (A,b,s,d,n);
    DIS=SP[D];
    int T=d;

    St1.push (d);
        while (true) {if (a[t][t]==t) break;
        St1.push (A[t][t]);
    T=A[T][T];
    } Dijkstra (B,a,s,d,n);

    TIME=SP[D];
    T=d;
    St2.push (d);
        while (true) {if (b[t][t]==t) break;
        if (b[t][t]!=a[t][t]) Issame=false;
        St2.push (B[t][t]);
    T=B[T][T]; } if (issame) {cout<< "Distance =" <<Dis<< ";
        Time = "<<Time<<": ";
            while (St1.empty () ==false) {if (St1.top () ==d) cout<<st1.top () <<endl;
            else Cout<<st1.top () << ",";
        St1.pop (); }}else {cout<< "Distance =" <<Dis<< ":";
        while (St1.empty () ==false) {if (St1.top () ==d) cout<<st1.top () <<endl;
            else Cout<<st1.top () << ",";
        St1.pop ();
        } cout<< "time =" <<Time<< ":";
            while (St2.empty () ==false) {if (St2.top () ==d) cout<<st2.top () <<endl;
            else Cout<<st2.top () << ",";
        St2.pop ();
}} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.