HihoCoder-1093 little Hi and Little Ho (SPFA)

Source: Internet
Author: User

Description

On Halloween night, Little Hi and Little Ho had come to a huge haunted house after supper.

There are a total of n locations in the Haunted house, numbered 1, respectively. N, there are some road connections between these n locations, there may be multiple roads connected between the two locations, but there is no road where both ends are the same location.

However, although the haunted house is very large, but the road is not much, so little hi still want to know from the entrance to the exit of the shortest distance is how much.

"Hmm ... A lot of places, the road is very few, this haunted house is a sparse map, since this is specifically marked out, then want to have its role. "Little Ho Road."

"Yes, there is a shortest path algorithm, and its time complexity is only related to the number of edges, so it is particularly well suited to solve the short-circuit problem of such a small number of edges." "Little hi nodded:" It is the SPFA algorithm, that is, shortest Path Faster algorithm. ”

"It sounds great, but how do you actually do it?" Little Ho asked.

"Would you write this question with a width-first search?" "Little Hi," asked the counter.

"Of course it will, constructs a queue, at the beginning of the queue only (s, 0)-Indicates the current point S, from point S to reach the point of the distance of 0, and then each time from the team to take a node (i, L)-The current point I, from Point S to the point of the distance is L, then traverse all the edge from this node (I, J, L)--Indicates that I and J have a length of l between the sides, will (J, L+l) added to the end of the team, and finally see all the traversed (T, X) node x in the minimum value is the answer ~ "Small ho for the search is already familiar with the heart, mouth sidewalk."

"SPFA algorithm, in fact, in a sense is the width of the first search optimization-if you try to add (p, Q) to the end of the queue, you will find that there is already a (p, Q '), then you can compare Q and Q ': If Q>=q ', then (p, Q) This node is actually a necessity for not continuing the search-an optimization pruning bar. And if Q&ltq ', then (p, Q ') there is no need to continue searching-but it already exists in the queue. It's easy to change the queue (p, Q ') to (p, Q). ”

"Then how do I know if there is a (p, Q ') in the queue?" "<, maintain an array of POSITION[1..N] is OK, if not in the queue is 1, otherwise is the location. "< p>

"So that's essentially the pruning of the breadth-first search." Little Ho asked.

Little hi laughed: "How could." The SPFA algorithm is actually an optimized version of the Bellman-ford algorithm, but can be understood to be a width-first search after forming. This question, we'll talk about it later. The input

Each test point (input file) has and has only one set of test data.

In a set of test data:

The 1th Act is 4 integers n, M, S, T, respectively, indicating the number of places in the haunted house and the number of roads, the number of the entrance (also a location), and the number of the exit (also a location).

The next M-line, each line describes a road: where the I behavior of three integers u_i, v_i, Length_i, indicates that there is a road length v_i between the place numbered U_i and the place numbered length_i.

For 100% of data, meet n<=10^5,m<=10^6, 1 <= length_i <= 10^3, 1 <= S, T <= N, and S not equal to T.

For 100% of the data, meeting little Hi and Little ho is always a way to get to the exit via the road marked on the map from the entrance. Output

For each set of test data, output an integer ans, which means that little hi and Little ho are going to walk out of the haunted house at least. Sample Input

5 3 5
1 2 997
2 3 505
3 4 118 4 5 3
5 480
3 4 796
5 2 794
2 5 146
5 4 604
2 5 63
Sample Output
172
really SPFA ....

#include #include #include #include #include using namespace std;
#define MP (x, y) make_pair (x, y) typedef pair PR;
const int N = 100000 + 5;
const int INF = 0X3F3F3F3F;
int N, m, S, T, Dist[n];
BOOL In[n];
Vector V[n];
Queue Q;
    int SPFA () {for (int i = 1; I <= n; i++) dist[i] = INF, in[i] = false;
    Dist[s] = 0;
    In[s] = true;
    Q.push (s); while (! Q.empty ()) {int u = q.front ();
        Q.pop ();
        In[u] = false;
            for (int i = V[u].size ()-1; I >= 0; i--) {int j = V[u][i].first;
                if (Dist[j] > Dist[u] + v[u][i].second) {Dist[j] = Dist[u] + v[u][i].second;
                    if (!in[j]) {Q.push (j);
                IN[J] = true;
}}}} return dist[t];
    } void Input_data () {int x, y, C;
        for (int i = 1; I <= m; i++) {scanf ("%d%d%d", &x, &y, &c);
        V[x].push_back (MP (Y, c));
    V[y].push_back (MP (x, c)); }} int Main () {scanf ("%d%d%d%d", &n, &m, &s, &t);
    Input_data ();
printf ("%d\n", SPFA ());

 }




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.