UVa 1599 Ideal Path (two times BFS)

Source: Internet
Author: User



Test instructions: Give a graph of n points, m edges, each edge has a color, the shortest path from node 1 to node n color dictionary order.



Analysis: First of all this is a shortest path problem, it should be BFS, because to ensure that the shortest path, but also to consider the dictionary sequence, feel very troublesome, and not good to do, the fact with two times BFS,



The first is the reverse BFS, the goal is to get from node I to node n the shortest distance, and then from the first point to the last, to ensure that at the time of the search, every passing point to the D value is reduced by 1,



Until the end, this is also a BFS, because this dictionary sequence in a node is the same, so is two bfs, I timed out several times, because less write a vis, must be careful,



It is said that you can only use a BFS, reverse, if interested can try, anyway, I do not want to come out, after all, inverted dictionary is not good to find.



And then this diagram is not good structure, can not use a two-dimensional array, I use a vector to exist, with a 2*n length, even under the subscript node, odd color, the traversal should pay attention to +2, not + +.



The code is as follows:


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

using namespace std;
const int maxn = 200000 + 5;
const int INF = 0x3f3f3f3f;
int d [maxn], vis [maxn], ans [maxn];
vector <int> v [maxn];
int Min (int a, int b) {return a <b? a: b;}

void bfs1 (int n) {// Reverse BFS
    memset (d, 0, sizeof (d));
    queue <int> q;
    memset (vis, 0, sizeof (vis));
    q.push (n);
    vis [n] = 1;

    while (! q.empty ()) {
        int u = q.front (); q.pop ();
        for (int i = 0; i <v [u] .size (); i + = 2) {
            int t = v [u] [i];
            if (t == 1) {d [1] = d [u] + 1; return;} // Small optimization, just find 1
            if (vis [t]) continue;
            d [t] = d [u] + 1;
            vis [t] = 1;
            q.push (t);
        }
    }
}

void bfs2 (int n) {
    memset (vis, 0, sizeof (vis));
    queue <int> q;
    q.push (n);
    vis [n] = 1;
    fill (ans, ans + d [1] +1, INF);

    while (! q.empty ()) {
        int u = q.front (); q.pop ();
        if (! d [u]) return;
        int m = INF;
        for (int i = 1; i <v [u] .size (); i + = 2)
            if (d [u] == d [v [u] [i-1]] + 1) m = Min (m, v [u] [i]);

        ans [d [1] -d [u]] = Min (ans [d [1] -d [u]], m);
        for (int i = 0; i <v [u] .size (); i + = 2)
            if (! vis [v [u] [i]] && d [u] == d [v [u] [i]] + 1 && v [u] [i + 1] == m) {
                vis [v [u] [i]] = 1;
                q.push (v [u] [i]);
            }
    }
}

int main () {
// freopen ("in.txt", "r", stdin);
    int n, m;
    while (scanf ("% d% d", & n, & m) == 2) {
        for (int i = 1; i <= n; ++ i) v [i] .clear ();
        int x, y, w;
        for (int i = 0; i <m; ++ i) {
            scanf ("% d% d% d", & x, & y, & w);
            v [x] .push_back (y);
            v [x] .push_back (w);
            v [y] .push_back (x);
            v [y] .push_back (w);
        }

        bfs1 (n);
        bfs2 (1);
        printf ("% d \ n", d [1]);
        for (int i = 0; i <d [1]; ++ i)
            if (! i) printf ("% d", ans [i]);
            else printf ("% d", ans [i]);
        printf ("\ n");
    }
    return 0;
} 





UVa 1599 Ideal Path (two times BFS)


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.