Hihocoder #1081 Shortest Path One Dijkstra

Source: Internet
Author: User

#1081: Shortest Path • time limit:10000msSingle Point time limit:1000msMemory Limit:256MBDescribe

On Halloween morning, Little Hi and Little ho, after a one-hour debate, finally decided how to spend a meaningful day-they decided to go to the haunted house!

After a number of hours in front of the haunted house, Little Hi and Little Ho, who had just entered the haunted house, were hungry, so they decided to use the map they had received before the door to find the shortest route to the end.

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. So little hi and Little ho at least how many miles to walk out of the haunted house to eat things?

/*

Little Ho thought to himself and said, "Hmm ... I think dynamic planning can be done, but I can't find the order of calculation, if I use f[i] to indicate the shortest distance from S to the node numbered I, I'm not able to know f[1]. F[n] In the order of calculation. ”

"So the problem doesn't need a complicated algorithm, so I'll tell you a little bit about it!" "Little Hi Way:" The length of the road can not be negative right? ”

"That is nature, after all, man has not invented time machine ..." small ho nodded.

So little hi asked: "So if you look at all the nodes adjacent to S and s nearest the S ', and from S to s ' distance is L, then there may be another way to make from S to s ' distance less than L?" ”

"Cannot, because s ' is the node with s closest to all nodes adjacent to S, then the distance from S to other neighboring points must be no less than l, that is, no matter how the next step, return to the L point when the total distance must be greater than L." Little Ho thought for a while, Tao.

"That means you already know the shortest path from S to S", right? "Little hi continued to ask."

"Yes, the shortest path is the length of L." Little Ho replied.

Little hi continued: "So now, we might as well think of s as a new node?" Called S1, and then I calculate the node that is adjacent to s or with S ' nearest to S '. Note that in this process, the distance to s of the node adjacent to S is already obtained in the previous step, so I ask only those nodes that are adjacent to s ' and s ' distance-this distance is equal to the distance between S and S ', and the distance between S ' and these nodes, and for the nodes that repeat in them--and those adjacent to S and S Take a smaller value in the two path. ”

Small ho nodded: "So as before, with S1 (that is, s and s ' node) adjacent to the node with S ' distance nearest node if s ', and this distance is L2, then we can know that s to S" The shortest path length is L2, because there is no other road than this shorter. ”

So small hi summed up: "The next question is not very simple, and so on, and so on, each time will be adjacent to the current collection (that is, with any element in the current set) in all nodes away from s nearest node (these distances can be deduced from the last calculation) to add to the current collection, I can guarantee that when each node is added to the collection, the distance from S is the shortest path between it and s! ”

"That's it!" But my belly is even more hungry! "Say, Little Ho's belly cooing up."

*/


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^3,m<=10^4, 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 23 5 41 2 7082 3 1123 4 7214 5 3395 4 9601 5 8492 5 981 4 992 4 252 1 2003 1 1463 2 1061 4 8604 1 7955 4 4795 4 2803 4 3 411 4 6224 2 3622 3 4154 1 9042 1 7162 5 575
Sample output
123
//Dijstra//hiho23#include <algorithm>#include<cassert>#include<cstdio>#include<cmath>#include<ctime>#include<cstring>using namespacestd;intS;intT;intn,m,i,j,k,m[1024x768][1024x768];BOOLv[1024x768];intMain () {scanf ("%u%u%u%u",&n,&m,&s,&T); {memset (M, the,sizeof(m));  while(m--) {scanf ("%u%u%u",&i,&k,&j); if(j<M[k][i]) {M[k][i]=m[i][k]=J; }} memset (&v[1],0, N); V[s]=true;  for(;;) {k=0;  for(i=1; i<=n;i++){                if(v[i]==0&&m[S][i]<M[s][k]) {k=i; }            }            if(k==T) {printf ("%u\n", m[s][t]);  Break; } V[k]=1;  for(i=1; i<=n;i++){                if(v[i]==0&&m[S][i]> (m[k][i]+M[s][k])) {M[s][i]= (m[k][i]+M[s][k]); }            }        }    }    return 0;}

Hihocoder #1081 Shortest Path One Dijkstra

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.