pat--1111. Online Map

Source: Internet
Author: User
Tags printf

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 = 2--5 4:3

Shortest path, each edge has two weights, distance and time.
Taking distance as the measure, the shortest path with shortest distance is obtained.
Taking time as a measure, we find the shortest path with the shortest time through the least node.

#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <

Cstring> using namespace std;
const int MAXN = 500 + 10;
const int inf = 0X3F3F3F3F;
int N, M, S, E;
int _DIS[MAXN][MAXN], _TIME[MAXN][MAXN];
int DIS[MAXN], TIME_DIS[MAXN], DISTIME[MAXN], CNT[MAXN];
int VIS[MAXN];
int DISPATH[MAXN], TIMEPATH[MAXN];

Vector<int> Ans_dis_path, Ans_time_path;
    void Distancedij () {memset (Vis, 0, sizeof (VIS));
    for (int i = 0; i < N; ++i) dis[i] = (i = = S? 0:inf);
        for (int i = 0; i < N; ++i) {int x, m = inf;
        for (int y = 0; y < N; ++y) if (!vis[y] && dis[y] <= m) m = dis[x=y];
        VIS[X] = 1; for (int y = 0; y < N; ++y) {if (!vis[y] && _dis[x][y]) {if (Dis[y] > dis[x] + _dis [x] [y])
                    {Dis[y] = Dis[x] + _dis[x][y];
                    Dispath[y] = x;
               Distime[y] = Distime[x] + _time[x][y]; }else if (dis[y] = = Dis[x] + _dis[x][y] && distime[y] > Distime[x] + _time[x][y]) {distime[
                    Y] = distime[x] + _time[x][y];
                Dispath[y] = x;
    }}}}} void Timedij () {memset (Vis, 0, sizeof (VIS));
    for (int i = 0; i < N; ++i) time_dis[i] = (i = = S? 0:inf);
    Vis[s] = 1;
        for (int i = 0; i < N; ++i) {int x, m = inf;
        for (int y = 0; y < N; ++y) if (!vis[y] && time_dis[y] <= m) m = time_dis[x=y];
        VIS[X] = 1; for (int y = 0; y < N; ++y) {if (!vis[y] && _time[x][y]) {if (Time_dis[y] > Time_di
                    S[X] + _time[x][y]) {time_dis[y] = Time_dis[x] + _time[x][y];
                    Timepath[y] = x;
                Cnt[y] = cnt[x] + 1; 
                 }else if (time_dis[y] = = Time_dis[x] + _time[x][y] && cnt[y] > Cnt[x] + 1) {timepath[y] = x;   Cnt[y] = cnt[x] + 1;
    }}}}} void Getdispath () {int tmp = E;
        while (tmp! = S) {ans_dis_path.push_back (TMP);
    TMP = dispath[tmp];
} ans_dis_path.push_back (S);
    } void Gettimepath () {int tmp = E;
        while (tmp! = S) {ans_time_path.push_back (TMP);
    TMP = timepath[tmp];
} ans_time_path.push_back (S);
    } int main () {scanf ("%d%d", &n, &m);
        for (int i = 0; i < M; ++i) {int u, V, O, L, T;
        scanf ("%d%d%d%d", &u, &v, &o, &l, &t);
        _DIS[U][V] = l, _time[u][v] = t;
    if (!o) _dis[v][u] = l, _time[v][u] = t;

    } scanf ("%d%d", &s, &e);    
    Distancedij ();

    Timedij ();
    Getdispath ();

    Gettimepath (); if (Ans_dis_path = = Ans_time_path) {printf ("Distance =%d;
        Time =%d: ", Dis[e], time_dis[e]);
        int i = 0; for (Auto it = Ans_dis_path.rbegin (); It! = Ans_dis_path.rend (); ++it) {
            if (i++) printf ("-");
        printf ("%d", *it);
        }}else{printf ("Distance =%d:", dis[e]);
        int i = 0;
            for (Auto it = Ans_dis_path.rbegin (); It! = Ans_dis_path.rend (); ++it) {if (i++) printf ("-");
        printf ("%d", *it);
        } printf ("\ntime =%d:", time_dis[e]);
        i = 0;
            for (Auto it = Ans_time_path.rbegin (); It! = Ans_time_path.rend (); ++it) {if (i++) printf ("-");
        printf ("%d", *it);
}} 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.